Playing sounds (MP3/WAV) in your BlackBerry application
Oct
18
Written by:
10/18/2011 9:01 AM
In this article we will explore what it takes to play an MP3/WAV file in a BlackBerry application. We will be showing you how to do this using the BlackBerry Java Development Environment (JDE) provided by Research In Motion. It will be a simple application that will contain one button, that when pressed will play an MP3/WAV file.
You will need to create a very basic application to follow along with us. So please refer to a previous blog posting that we have which shows you how to create a basic BlackBerry application.
Creating Your First BlackBerry application
We will extend this application by including a button. In the class we previously created called “MyFirstAppMainScreen.java” add the following code near the end of the constructor called “MyFirstMainScreen()”:
01.ButtonField btn = new ButtonField("Play",ButtonField.CONSUME_CLICK)
02.{
03.protected void fieldChangeNotify(int context)
04.{
05.// TODO Auto-generated method stub
06.Thread t = new Sound();
07.t.start();
08.super.fieldChangeNotify(context);
09.}
10.};
11.add(btn);
What this code does is create a button or more specifically a “ButtonField”. The text “Play” will be displayed on this button. It then indicates that it is interested in receiving notification when the user clicks on the button. The code following that creates an instance of another class which we will define shortly called “Sound”. This file contains the code to actually play the MP3 file. One more thing to note here is that we are going to create a new thread of execution every time someone clicks on the button. The reason we want to do this is because we want that if someone repeatedly clicks on the button very quickly, we want to do is play another copy of the MP3 and let the sounds overlap. We do not want to stop playing the MP3 if it was already playing. The last line we added in our code actually adds the button we created to the main screen for display. In other words it adds it to the visual interface of the application.
The next step is critical. Locate the MP3 file that you would like to play and copy it into the same Windows folder that contains the source code for this project. So wherever you have the files “MyFirstAppMainScreen.java” and “MyFirstAppMain.java”, copy the MP3 file to the same location. So in my example I have added an MP3 file called “audiocowbell.mp3” to my project. I added it to my project by right-clicking on the project called “MyFirstAppProject”.
From the subsequent screen, change the “Files of Type” drop-down to “All Files”, so that you can see the MP3 file that you added.
Your project should now look similar to the following:
Note that the MP3 file was correctly added to the project but the WAV file called “bomp-02.wav” has not been properly added to the project. The reason that the WAV file will not work in this example is because I left the WAV file on my Desktop instead of copying it to the same folder as my project.
Now let us create our “Sound” class that will actually play the sound. Create a new file in your project. Right-click on the project “MyFirstAppProject”. Select “Create New File In Project…”.
Give the following name to the file “Sound.java”. Click the “OK” button.
Replace the default code provided for you with the following:
01.package com.digitalbusiness.MyFirstApp;
02.
03.
04.import java.io.InputStream;
05.import javax.microedition.media.Manager;
06.import javax.microedition.media.Player;
07.import net.rim.device.api.ui.UiApplication;
08.import net.rim.device.api.ui.component.ButtonField;
09.import net.rim.device.api.ui.container.MainScreen;
10.
11./**
12.*
13.*/
14.public class Sound extends Thread
15.{
16.public void run()
17.{
18.try{
19.Class thisClass = null;
20.thisClass = Class.forName("com.digitalbusiness.MyFirstApp.MyFirstAppMain");
21.InputStream inputStream =thisClass.getResourceAsStream("/audiocowbell.mp3");
22.Player mp3Player = Manager.createPlayer(inputStream,"audio/mpeg");
23.mp3Player.prefetch();
24.mp3Player.start();
25.}
26.catch (Exception e) {
27.// TODO: handle exception
28.}
29.
30.}
31.}
Don’t forget to change the package to match your package name.
So the key elements here are:
- We extended from “Thread” because as we explained earlier we want to play the sound as many times as the user can click on the button and while the sound is playing we don’t want to disable the main application from receiving input from the user.
- We got a reference to our application’s main file. Do you remember that when we first created our application we started off with a java file called “MyFirstAppMain.java”. At the time we said that it is the main starting point for our application and it is from within this main file that we will launch the main screen of our application. In this file we had a method called:
1.public static void main(String[] args)
This is the starting point for our application. The BlackBerry operating system will look in our application to see if it has defined this method and will execute whatever is contained in that method.
In our “Sound.java” file we get a reference to that main file in our application:
1.thisClass = Class.forName("com.digitalbusiness.MyFirstApp.MyFirstAppMain");
it starts off with the same text we used in our package definition and then adds on the name of our main java file, the one that contains the method:
1.public static void main(String[] args)
Once you have placed the MP3 file in the same folder as the file “MyFirstAppMain.java” the line:
1.thisClass = Class.forName("com.digitalbusiness.MyFirstApp.MyFirstAppMain");
will work.
- The line:
1.InputStream inputStream =thisClass.getResourceAsStream("/audiocowbell.mp3");
Indicates what file to play. Note that it is preceded by a slash “/”. This is very important. Now if you decided to have an “Audio” folder in your project and you placed the MP3 file in that folder you would modify the line:
1.InputStream inputStream =thisClass.getResourceAsStream("/audiocowbell.mp3");
To:
1.InputStream inputStream =thisClass.getResourceAsStream("/Audio/audiocowbell.mp3");
DO NOT modify the line:
1.thisClass = Class.forName("com.digitalbusiness.MyFirstApp.MyFirstAppMain");
it is correct.
- The other significant line is:
1.Player mp3Player = Manager.createPlayer(inputStream,"audio/mpeg");
This line creates an instance of the BlackBerry audio player in memory and tells the player the MIME type of the audio to expect. In this case it is to expect “audio/mpeg” which is the MIME type for MP3 files. If for example you decided to play a WAV file instead, you would change the MIME type to “audio/x-wav”.
- One of the last lines in our code, calls the “mp3Player.play()” method which will play our MP3 file for us.
Run your code to hear the results.
Enjoy!