如何在Blackberry中连续播放音频文件?

问题描述:

我想创建一个连续播放几个音频文件的Blackberry应用程序。但我创建的应用程序可以连续打开所有音频文件,但无法播放整个音频文件。任何人都可以知道这个解决方案吗?下面是我的代码:如何在Blackberry中连续播放音频文件?

 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

任何人都可以帮助我吗?我需要明天完成申请。 – TEN

可以保存要在载体例如播放文件,并进行了队列将被打第一个开始,当它完成将从第二个开始。 所以在你的向量中,你将拥有test1,test2,test3,...你将玩test1。当这完成后,你将开始测试2 ...

+0

你在玩矢量游戏时意味着什么?对不起,我是黑莓新手。你能向我展示一个例子吗? – TEN

+0

我不是故意玩矢量。我的意思是将值存储在一个向量中,然后为(int i = 0; i

+0

如何将它存储到向量中? – TEN

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 
} 
+0

哈哈,谢谢。 :D尽管我并不需要完整的解决方案,但它帮助我找到了解决方案。 :d – TEN