JMF provides a class MediaPlayer
which can be used as a full-featured player. However, I can't find a way to play an audio file. Some snippets of the code used.
import javax.media.bean.playerbean.MediaPlayer;
// ....
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(location of file); // <- syntax error!
mp.start();
But it doesn't work. Eclipse shows this error:
Syntax error on token "setMediaLocation", Identifier expected after this token
..on setMediaLocation()
method.
Can someone tell me how to use MediaPlayer
class to play audio file? and what is wrong in my coding?
You do not literally have
..setMediaLocation(location of file);
in the source do you? I thought that was paraphrased.In that case, the answer is to supply an argument to the
setMediaLocation()
method. The docs. specify aString
orMediaLocator
instance.I avoid using
String
instances to represent file or URL based sources. If a method needs aFile
orURL
- give it aFile
orURL
. That leaves the best option as aMediaLocator(URL)
. Here is how it might go. First we need aURL
- there are a number of different ways of getting one, depending on what the source of the URL is. E.G. the internet, a file on the local file-system, or an embedded resource in a Jar delivered with the app. The last two might be something like:File based URL
Embedded resource URL
Once there is a
mediaURL
instance, we can create a locator and use it in the method.Using the URL
General tips