1

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?

jjnguy
  • 136,852
  • 53
  • 295
  • 323

6 Answers6

2

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
  • 136,852
  • 53
  • 295
  • 323
1

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();
    }
}
akarnokd
  • 69,132
  • 14
  • 157
  • 192
0

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
  • 1,744
  • 1
  • 14
  • 20
0

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

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • 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 – Powerlord May 04 '09 at 19:41
0

Old question, but for the record:

java.awt.Desktop.getDesktop().open(new java.io.File(my_filename));
greenoldman
  • 16,895
  • 26
  • 119
  • 185
-4

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.

Richard Walton
  • 4,789
  • 3
  • 38
  • 49
  • That is not right. The poster needs to run the appropriate program and then pass the file in as a parameter. – jjnguy May 02 '09 at 19:20