How to play an audio file one after another in Blackberry?
I managed to create an application in Blackberry that plays audio file, but it will only plays one file. I try to use a for loop to play a few audio files.
I managed to play it, but it did not play the whole sound of the audio files, it just play the first audio files and second for a few seconds, and stop playing after that. The files that played also play the sound overlap each other which should not be happening.
How to play the full sound of the audio files one after another in Blackberry without stopping?
Here is my code for the application that I created with the for loop:
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)
*/
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;
开发者_StackOverflow}
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();
}
}
}
I managed to play it, but it did not play the whole sound of the audio files, it just play the first audio files and second for a few seconds, and stop playing after that. The files that played also play the sound overlap each other which should not be happening.
Please reread the BB API docs for Player
carefully:
Simple Playback
A Player can be created from one of the Manager's createPlayer methods. After the Player is created, calling start will start the playback as soon as possible. The method will return when the playback is started. The playback will continue in the background and will stop automatically when the end of media is reached.
Simple playback example illustrates this:
try {
Player p = Manager.createPlayer("http://abc.wav");
p.start();
} catch (MediaException pe) {
} catch (IOException ioe) {
}
Note the docs say The method will return when the playback is started. The playback will continue in the background ..
. This is the reason of you get "sound overlap".
To overcome this you need to attach a listener to the player Player.addPlayerListener(PlayerListener playerListener)
. The listener will be notified from the background "playback" thread when the media file has been played to the end. And this will be the right moment to start a new playback for the next media file. Please don't expect the code from me, I'm just giving you an idea.
Finally I get it, this is my code. :D
package mypackage;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
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)
*/
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 implements PlayerListener
{
/**
* Creates a new PlayMediaScreen object
*/
Player p = null;
String mp3 = "";
String test1 = "Test2seconds.mp3";
String test5 = "Test2seconds2.mp3";
//String test6 = "Test2seconds3.mp3";
String test4 = "Test2seconds4.mp3";
String test2 = "blind_willie.mp3";
String test3 = "blind_willie2.mp3";
PlayMediaScreen()
{
mp3 = test1;
play(mp3);
}
private void play(String mp3){
InputStream stream = (InputStream)this.getClass().getResourceAsStream("/" + mp3);
try {
//p = Manager.createPlayer(source);
p = Manager.createPlayer(stream,"audio/mpeg");
p.addPlayerListener(this);
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();
*/
}
public void playerUpdate(Player player, String event, Object eventData) {
// TODO Auto-generated method stub
if (event.equals(PlayerListener.END_OF_MEDIA)) {
//String mp3 = "";
if( mp3.equals(test1) ) {
mp3 = test2;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test2) ){
mp3 = test3;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test3) ) {
mp3 = test4;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test4) ) {
mp3 = test5;
//testing
System.out.println("The MP3 is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
play(mp3);
//testing
System.out.println("The MP3 FINISH PLAYING is " + mp3 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else if( mp3.equals(test5) ){
mp3 = null;
try {
player.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.deallocate();
player.close();
}
}
}
}
}
精彩评论