views:

278

answers:

3

How do you call the default media player to play a wav file in Java?

A: 

Desktop.getDesktop().open(new File("path/to/file.wav")) ought to do it.

gustafc
i tried it but I got this error:java.io.IOException: Failed to open file:/c:/documents%20and%20settings/.../sound.wav. Error message: The parameter is incorrect.
blue
There is/was a bug in the Desktop API related to filenames containing spaces. Try to upgrade to the most recent Java VM or if you only need to play plain PCM encoded WAV files, you can also use the JavaSound API without bothering external media players.
jarnbjo
A: 

You can also play a wav-file in Java without mediaplayer.
Take a look at this page: AePlayWave.

If you realy want to open a mediaplayer, you can use the rundll utility.

// "file" is the filename of the data file 
//  ex. myresume.doc 
//  to start Word if the doc extension is associated with it.
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());

This util works only in Windows

Martijn Courteaux
A: 

go google for edu.stanford.ejalbert.BrowserLauncher - package for OS independent call. It opens file with default program regardless of you run your app on linux, mac or windows... so you don't have to worry about whether user has windows media player or would like better his winamp to play .WAV files...

ante.sabo