如何在黑莓手机中连续播放音频文件?

发布于 2024-12-02 00:49:03 字数 4514 浏览 0 评论 0原文

我设法在黑莓中创建一个播放音频文件的应用程序,但它只能播放一个文件。我尝试使用 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();



        }
    }
} 

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



        }
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-12-09 00:49:03

我成功播放了它,但它没有播放音频文件的整个声音,它只播放第一个音频文件和第二个音频文件几秒钟,然后停止播放。播放的文件也会播放相互重叠的声音,这是不应该发生的。

请重新阅读 BB API 文档 玩家仔细:

简单播放

可以通过 Manager 的 createPlayer 方法之一创建 Player。 Player创建后,调用start会尽快开始播放。当播放开始时该方法将返回。播放将在后台继续,并在到达媒体结尾时自动停止。

简单的播放示例说明了这一点:

try {
    Player p = Manager.createPlayer("http://abc.wav");
    p.start();
} catch (MediaException pe) {
} catch (IOException ioe) {
}

请注意文档说 该方法将在播放开始时返回。播放将在后台继续进行..。这就是你得到“声音重叠”的原因。

为了克服这个问题,您需要将一个侦听器附加到播放器Player.addPlayerListener(PlayerListener playerListener)。当媒体文件播放完时,监听器将从后台“播放”线程收到通知。这将是开始下一个媒体文件的新播放的正确时机。请不要指望我提供代码,我只是给你一个想法。

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.

一世旳自豪 2024-12-09 00:49:03

终于我明白了,这是我的代码。 :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();
                 }

              }
        }
    }
}

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

              }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文