How to continuous play audio files in Blackberry?
I wanted to create a Blackberry application that plays a few audio files continuous one after another. But the application I created can open all the audio files continuously but did not managed to play the whole audio files. Can anyone know a solution for this? Below is my codes:
package mypackage;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import java.lang.Class;
import javax.microedition.rms.RecordStore;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import net.rim.device.api.media.protocol.ByteArrayInputStreamDataSource;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.extension.container.*;
import net.rim.device.api.ui.UiApplication;
import java.io.IOException;
public class PlayMedia extends UiApplication{
/**
* Entry point for application
* @param args Command line arguments (not used)
开发者_StackOverflow */
public static void main(String[] args){
PlayMedia theApp = new PlayMedia();
theApp.enterEventDispatcher();
}
public PlayMedia()
{
pushScreen(new PlayMediaScreen());
}
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
final class PlayMediaScreen extends MainScreen
{
/**
* Creates a new PlayMediaScreen object
*/
PlayMediaScreen()
{
String test1 = "Test(2seconds).mp3";
String test2 = "Test(2seconds)2.mp3";
String test3 = "Test(2seconds)3.mp3";
String test4 = "Test(2seconds)4.mp3";
String test5 = "blind_willie.mp3";
String mp3 = null;
for(int i=0;i<5;i++){
if(i == 0){
mp3 = test1;
}
else if(i == 1){
mp3 = test2;
}
else if(i == 2){
mp3 = test3;
}
else if(i == 3){
mp3 = test4;
}
else if(i == 4){
mp3 = test5;
}
play(mp3);
}
}
private void play(String mp3){
Player p = null;
InputStream stream = (InputStream)this.getClass().getResourceAsStream("/" + mp3);
try {
//p = Manager.createPlayer(source);
p = Manager.createPlayer(stream, "audio/mpeg");
p.realize();
p.prefetch();
//testing
System.out.println("Creating Players!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("The mp3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("IO Exeception!!!!!!!!!!!!!!!!1 " + e);
//testing
System.out.println(p);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
/*
* Best practice is to invoke realize(), then prefetch(), then start().
* Following this sequence reduces delays in starting media playback.
*
* Invoking start() as shown below will cause start() to invoke prefetch(0),
* which invokes realize() before media playback is started.
*/
try {
p.start();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//testing
System.out.println("Media Exeception for starting!!!!!!!!!!!!!!!!1" + e);
//testing
System.out.println(p);
}
try {
p.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.deallocate();
p.close();
}
}
}
You can save the files that you want to play in a Vector for example, and make a queue that will start by playing the first one and when it finishes it will start by the second. So in ur vector you will have the test1, test2, test3, ... and you will play the test1. When this finished you will start by test2 ...
Vector v = new Vector();
v.addElement("Test(2seconds).mp3");
v.addElement("Test(2seconds)2.mp3");
v.addElement("Test(2seconds)3.mp3");
for (int i = 0 ; i < v.size() ; i ++) {
String address = (String) v.elementAt(i);
//play here the audio file
//You can use here thread.wait() and Thread.notify to handle the event of beggining of an audio file and the ending of it. Do not play them all in the same time. You have to wait for the audio file to end before playing the other one
}
精彩评论