Playing a Sound File in a Blackberry Java App

Playing a Sound File in a Blackberry Java App

Without a lot of undo fuss, here’s one way to play a sound file in a Blackberry Java app:

Class cl = null;
try {
cl = Class.forName("com.mycompany.myapp.MyAppMainClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (cl!=null) {
InputStream is = cl.getResourceAsStream("/someSoundFile.mp3");
try {
Player player = Manager.createPlayer(is, "audio/mpeg");
player.realize();
player.prefetch();
player.start();
is.close();
} catch (IOException e) {
e.printStackTrace();
} catch (MediaException e) {
e.printStackTrace();
}
}

I usually create a package under the res (resource) package, name it sounds, and plop my sound files there.
Easy Peasy.

Advertisements

2 thoughts on “Playing a Sound File in a Blackberry Java App

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s