如何使用Java播放WAV文件?

时间:2021-01-16 19:44:52

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:

我使用客户端和服务器发送WAV文件,但是我希望在收到时播放WAV。我尝试这种方法,但它不起作用:

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"

“错误!它不起作用!java.io.IOException:无法运行程序”C:\ Documents“:CreateProcess error = 193,%1不是有效的Win32应用程序”

What am I doing wrong?

我究竟做错了什么?

6 个解决方案

#1


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:

您需要执行音频播放器程序(可能是Windows媒体播放器或类似的东西),然后将文件名(文件的完整路径)作为参数传递:

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

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

That should work.

这应该工作。

#2


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

Javas内置WAV播放支持有什么问题?您可以使用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();
    }
}

#3


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

是否必须使用默认音频播放器?如果不是,您可能想要查看Java的AudioSystem。

#4


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

不要指定要使用的媒体播放器,让windows为您查找:

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给你(start是cmd.exe的内置操作,而不是一个独立的程序,这就是为什么你必须运行cmd.exe而不是'start.exe'),

start /h

#5


Old question, but for the record:

老问题,但为了记录:

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

#6


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.

请注意额外的单引号。我甚至不确定你的方法是否会起作用,但是试一试。

#1


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:

您需要执行音频播放器程序(可能是Windows媒体播放器或类似的东西),然后将文件名(文件的完整路径)作为参数传递:

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

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

That should work.

这应该工作。

#2


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

Javas内置WAV播放支持有什么问题?您可以使用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();
    }
}

#3


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

是否必须使用默认音频播放器?如果不是,您可能想要查看Java的AudioSystem。

#4


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

不要指定要使用的媒体播放器,让windows为您查找:

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给你(start是cmd.exe的内置操作,而不是一个独立的程序,这就是为什么你必须运行cmd.exe而不是'start.exe'),

start /h

#5


Old question, but for the record:

老问题,但为了记录:

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

#6


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.

请注意额外的单引号。我甚至不确定你的方法是否会起作用,但是试一试。