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.

Download and Play a WAV file… using Objective-C

Download and Play a WAV file… using Objective-C

A client had a question this morning about loading a WAV file over the web and playing it from within an iPhone app.

Turns out it’s not too difficult to do:

// Grab the sound we wish to play from across the web
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@”http://www.dailywav.com/0510/worstSunburn.wav”%5D%5D;

// Load the data into an AVAudioPlayer object
AVAudioPlayer * theAudio = [[AVAudioPlayer alloc] initWithData:data error:NULL];

// Play the sound file
[theAudio play];

Easy, Peasy.