Reputation: 799
try {
Player p = javax.microedition.media.Manager.createPlayer("abc.mp3");
p.realize();
VolumeControl volume = (VolumeControl)p.getControl("VolumeControl");
volume.setLevel(30);
p.prefetch();
p.start();
} catch(MediaException me) {
Dialog.alert(me.toString());
} catch(IOException ioe) {
Dialog.alert(ioe.toString());
}
I used above code to play an audio in blackberry. But it gives an exception error as mentioned in below.
javax.microedition.media.MediaException
Upvotes: 0
Views: 378
Reputation: 31045
If you look at the BlackBerry API docs, you see that for the version of createPlayer()
that you use:
MediaException - Thrown if a Player cannot be created for the given locator.
It also says:
locator - A locator string in URI syntax that describes the media content.
Your locator ("abc.mp3"
) doesn't look like it's in URI syntax. You could try changing that, to something like "file:///SDCard/some-folder/abc.mp3"
.
Or, if the mp3 file is an app resource, I usually use this code:
try {
java.io.InputStream is = getClass().getResourceAsStream("/sounds/abc.mp3");
javax.microedition.media.Player p =
javax.microedition.media.Manager.createPlayer(is, "audio/mpeg");
p.realize();
// get volume control for player and set volume to max
VolumeControl vc = (VolumeControl) p.getControl("VolumeControl");
if (vc != null) {
vc.setLevel(100);
}
// the player can start with the smallest latency
p.prefetch();
// non-blocking start
p.start();
} catch (javax.microedition.media.MediaException me) {
// do something?
} catch (java.io.IOException ioe) {
// do something?
} catch (java.lang.NullPointerException npe) {
// this happened on Tours without mp3 bugfix
}
Upvotes: 4