简体   繁体   中英

How can I play a WAV file using Java?

I send WAV files using a client and server, but I want to play the WAV when it received. I try this method but it did not work:

Runtime.getRuntime().exec("C:\\Documents and   Settings\\Administratore\\Desktop\\gradpro\\test1\\s1.wav") ;

This the exception that I get:

"Error! It didn't work! java.io.IOException: Cannot run program "C:\\Documents": CreateProcess error=193, %1 is not a valid Win32 application"

What am I doing wrong?

You need to execute the audio player program (probably windows media player or something similar) and then pass the filename (the full path to the file) in as a parameter:

String wavPlayer = "/path/to/winmediaplayer.exe";
String fileToPlay = "/path/to/wav/file.wav";

Runtime.getRuntime().exec(wavPlayer, new String[]{fileToPlay}) ;

That should work.

What's wrong with Javas built in WAV playback support? You can play it back using AudioClip:

private void playBackClip(String fileName) {
    try {
        AudioInputStream soundStream = null;
        if (fileName.startsWith("res:")) {
            soundStream = AudioSystem.getAudioInputStream(
                Object.class.getResourceAsStream(fileName.substring(4)));
        } else {
            File audioFile = resMap.get(fileName);
            soundStream = AudioSystem.getAudioInputStream(audioFile);
        }
        AudioFormat streamFormat = soundStream.getFormat();
        DataLine.Info clipInfo = new DataLine.Info(Clip.class,
                streamFormat);

        Clip clip = (Clip) AudioSystem.getLine(clipInfo);
        soundClip = clip;
        clip.open(soundStream);
        clip.setLoopPoints(0, -1);
        clip.start();
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }
}

Instead of specifying the media player to use, let windows look it up for you:

String comspec = System.getenv().get("ComSpec");
String fileToPlay = "/path/to/wav/file.wav";

Runtime.getRuntime().exec(comspec, new String[]{"/c", "start", fileToPlay}) ;

You are basically doing something like:

cmd.exe /c start path_to_wav_file.wav

To see all the options start gives you (start is a built-in operation of cmd.exe, not a stand-alone program, which is why you have to run cmd.exe instead of a 'start.exe'), do

start /h

Is the use of the default audio player mandatory? If not you might want to look into Java's AudioSystem .

旧问题,但有记录:

java.awt.Desktop.getDesktop().open(new java.io.File(my_filename));

Try:

Runtime.getRuntime().exec("'C:\Documents and Settings\Administratore\Desktop\gradpro\test1\s1.wav'") ;

Note the extra single quotations. I'm not even sure if your method will work, but give that a go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM