我设法在Blackberry中创建一个播放音频文件的应用程序,但它只播放一个文件。我尝试使用for循环播放一些音频文件。
我设法播放它,但它没有播放音频文件的整个声音,它只播放第一个音频文件,第二个播放几秒钟,然后停止播放。播放的文件也播放声音彼此重叠,这不应该发生。
如何在Blackberry中一个接一个地播放音频文件的完整声音而不停止?
这是我使用for循环创建的应用程序的代码:
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;
}
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();
}
}
}
答案 0 :(得分:1)
我设法播放它,但它没有播放音频文件的整个声音,它只播放第一个音频文件,第二个播放几秒钟,然后停止播放。播放的文件也播放声音彼此重叠,这不应该发生。
请仔细阅读Player
的{{3}}:
简单播放
可以从Manager的createPlayer方法之一创建播放器。创建播放器后,调用start将尽快开始播放。播放开始时,该方法将返回。播放将在后台继续,并在到达媒体结束时自动停止。
简单回放示例说明了这一点:
try {
Player p = Manager.createPlayer("http://abc.wav");
p.start();
} catch (MediaException pe) {
} catch (IOException ioe) {
}
请注意文档说The method will return when the playback is started. The playback will continue in the background ..
。这就是你得到“声音重叠”的原因。
要解决此问题,您需要将监听器附加到播放器Player.addPlayerListener(PlayerListener playerListener)
。当媒体文件播放到最后时,将从后台“播放”线程通知收听者。这将是为下一个媒体文件开始新播放的合适时机。请不要指望我的代码,我只是给你一个想法。
答案 1 :(得分:0)
最后我明白了,这是我的代码。 :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();
}
}
}
}
}