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

时间:2011-08-26 09:00:17

标签: blackberry audio java-me

我想创建一个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();



        }
    }

}

2 个答案:

答案 0 :(得分:1)

例如,您可以保存要在Vector中播放的文件,并创建一个队列,该队列将通过播放第一个队列开始,完成后将从第二个开始。 所以在你的矢量中你将得到test1,test2,test3,......然后你将玩test1。完成后,您将从test2开始......

答案 1 :(得分:1)

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
}