Can't get audio file to play

戏子无情 提交于 2019-12-25 03:34:05

问题


I can't seem to figure out why my audio file won't play. The audio file is a wav file and is just. The error i am getting is javax.sound.sampled.UnsupportedAudioFileException.

public class MusicProgress {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame b = new JFrame();
    FileDialog fd = new FileDialog(b, "Pick a file: ", FileDialog.LOAD);
    fd.setVisible(true);
    final File file = new File(fd.getDirectory() + fd.getFile());
    //URI directory = new URI (fd.getDirectory() + fd.getFile());
    try {
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
        AudioFormat audioFormat = inputStream.getFormat();
        Clip clip = AudioSystem.getClip();
        clip.open(inputStream);
        clip.start();
    } catch (LineUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

}

回答1:


Java does not support all wav formats. The highest quality supported is a standard CD encoding format. With those files, you should be okay. That format has 16-bit encoding, 44100 fps, little endian, stereo. You should be able to inspect the properties of your wav file and learn if it matches or not.

It is getting more and more common for DAWs to produce wav files that have 24-bit or 32-bit encoding, or 48000fps or 92000fps.

It is possible to convert these to the "CD" encoding spec with a tool such as Audacity.




回答2:


I dug up some old code of mine to play an audio file. This might work:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;

public class SoundPlayer {

Clip clip;

public SoundPlayer(String file){

    //if(clip.isRunning()){clip.stop();}
    try{
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(SoundPlayer.class.getResource(file));
        clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
    }catch(Exception err){err.printStackTrace(); JOptionPane.showMessageDialog(null, "SoundPlayer: "+err,null,0);}
}

}

And to play an audio clip:

new SoundPlayer(filepath);

The path should look somthing like this: "/game/resources/sound/Explosion.wav" And as you stated, it must be a .wav file.




回答3:


You have to use external libraries to play files like .mp3

Java support only .wav

but that's enough.All you need is an external algorithm to play other music formats.All the other format's came originally from .wav they pass into an algorithm and then boom they become .ogg,.mp3,.whatever

1.A very impressive library to use which support .mp3 JLayer.jar You can import this jar into your project as an external library.

2.If you search more you will find JAudiotagger it's just amazing but difficult to use.

3.Also you can use Java Media FrameWork but whatever it doesnt support to much formats.

4.JavaZoom has also and other libraries to support .ogg,.speex,.flac,.mp3

Links to stackoverflow on How to play .wav files with java

And http://alvinalexander.com/java/java-audio-example-java-au-play-sound Not sure if that still works with java 8

This:

   import java.io.File;
   import java.io.IOException;

   import javax.sound.sampled.AudioFormat;
   import javax.sound.sampled.AudioInputStream;
   import javax.sound.sampled.AudioSystem;
   import javax.sound.sampled.Clip;
   import javax.sound.sampled.DataLine;
   import javax.sound.sampled.LineEvent;
   import javax.sound.sampled.LineListener;
   import javax.sound.sampled.LineUnavailableException;
   import javax.sound.sampled.UnsupportedAudioFileException;


 public class AudioPlayerExample1 implements LineListener {

/**
 * this flag indicates whether the playback completes or not.
 */
boolean playCompleted;

/**
 * Play a given audio file.
 * @param audioFilePath Path of the audio file.
 */
void play() {
    File audioFile = new File("C:/Users/Alex.hp/Desktop/Musc/audio.wav");

    try {
        AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

        AudioFormat format = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(Clip.class, format);

        Clip audioClip = (Clip) AudioSystem.getLine(info);

        audioClip.addLineListener(this);

        audioClip.open(audioStream);

        audioClip.start();

        while (!playCompleted) {
            // wait for the playback completes
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

        audioClip.close();

    } catch (UnsupportedAudioFileException ex) {
        System.out.println("The specified audio file is not supported.");
        ex.printStackTrace();
    } catch (LineUnavailableException ex) {
        System.out.println("Audio line for playing back is unavailable.");
        ex.printStackTrace();
    } catch (IOException ex) {
        System.out.println("Error playing the audio file.");
        ex.printStackTrace();
    } 
}

/**
 * Listens to the START and STOP events of the audio line.
 */
@Override
public void update(LineEvent event) {
    LineEvent.Type type = event.getType();

    if (type == LineEvent.Type.START) {
        System.out.println("Playback started.");

    } else if (type == LineEvent.Type.STOP) {
        playCompleted = true;
        System.out.println("Playback completed.");
    } 
}

public static void main(String[] args) {
    AudioPlayerExample1 player = new AudioPlayerExample1();
    player.play();
} 

}



来源:https://stackoverflow.com/questions/28570841/cant-get-audio-file-to-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!