2011-08-28 127 views
0

我設法在Blackberry中創建播放音頻文件的應用程序,但它只播放一個文件。我嘗試使用for循環播放一些音頻文件。如何在黑莓中連續播放音頻文件?

我設法播放它,但它沒有播放音頻文件的全部聲音,它只播放第一個音頻文件,然後播放幾秒鐘,然後停止播放。播放的文件也播放聲音彼此重疊,這不應該發生。

如何在黑莓中一個接一個地播放音頻文件的完整聲音而不停止?

這裏是我的,我與創建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

任何人都可以幫助我!!!!我需要明天完成! – TEN

回答

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(); 
       } 

       } 
     } 
    } 
} 
1

我設法發揮它,但它沒有播放音頻文件的整個聲音,它只是玩第一個音頻文件,第二個音頻文件幾秒鐘,然後停止播放。播放的文件也播放聲音彼此重疊,這不應該發生。

請重讀BB API docsPlayer小心:

簡單回放

玩家可以從管理人的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)上附加一個監聽器。當媒體文件播放到最後時,將從背景「回放」線程通知監聽器。這將是開始下一個媒體文件的新播放的正確時刻。請不要指望我的代碼,我只是給你一個想法。

+0

嗨,你能給我看一個添加播放器監聽器的例子嗎?對不起,我是黑莓手機的新手,所以可能需要一些更具體的例子。我可以播放一個簡單的播放,但它只是我不能連續播放多個音頻文件。 :(請幫忙! – TEN

+0

對不起,我沒有一個例子,因爲我從來沒有在我的項目中使用過音頻播放。現在你被告知要做什麼以及使用什麼BB API了,我不認爲任何東西都可以保留你從成功中獲益 –

+0

哈哈,謝謝,我之前嘗試加入PlayerListener,但它給了我同樣的錯誤,我嘗試閱讀文檔,但我並不真正瞭解它如何依次播放音頻文件playerlistener。 – TEN