tags:

views:

353

answers:

5

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?

+1  A: 

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.

jjnguy
thank you for the help it work now
A: 

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.

Richie_W
That is not right. The poster needs to run the appropriate program and then pass the file in as a parameter.
jjnguy
A: 

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

VolkerK
There's quite a bit more information on the Java Sound Programmer's Guide: http://java.sun.com/j2se/1.5.0/docs/guide/sound/programmer_guide/contents.html
R. Bemrose
A: 

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
Trevor Harrison
A: 

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();
    }
}
kd304