如何处理播放音频文件时的运行时异常?

发布于 2024-08-25 12:43:01 字数 1255 浏览 4 评论 0原文

我有一个按钮,可以在其点击侦听器上播放音频文件。如果在播放音频文件时一次又一次单击该按钮,应用程序就会崩溃。解决办法是什么?

这是一些参考代码:

 private OnClickListener btnMercyListener = new OnClickListener()
    {

        public void onClick(View v)
        {                        
           // Toast.makeText(getBaseContext(), 
             //       "Mercy audio file is being played", 
               //       Toast.LENGTH_LONG).show();

            if (status==true)
            {
                mp.stop();
                mp.release();
                status = false;

            } 
            else
            {
            mp = MediaPlayer.create(iMEvil.this,R.raw.mercy); 


          //mp.start();

            try{
                mp.start();
                status= true; 
                //mp.release();
               }catch(NullPointerException e)
               {
                   Log.v("MP error",e.toString());
               }

            }

            mp.setOnCompletionListener(new OnCompletionListener(){ 

                   // @Override 
                   public void onCompletion(MediaPlayer arg0) { 
                      mp.release();
                      status = false;

                   } 
              }

          ); 



        }
    };

I have a button that plays an audio file on its click listener. If the button is clicked again and again while the audio file is being played then the app crashes. What's the solution?

Here is some code for reference:

 private OnClickListener btnMercyListener = new OnClickListener()
    {

        public void onClick(View v)
        {                        
           // Toast.makeText(getBaseContext(), 
             //       "Mercy audio file is being played", 
               //       Toast.LENGTH_LONG).show();

            if (status==true)
            {
                mp.stop();
                mp.release();
                status = false;

            } 
            else
            {
            mp = MediaPlayer.create(iMEvil.this,R.raw.mercy); 


          //mp.start();

            try{
                mp.start();
                status= true; 
                //mp.release();
               }catch(NullPointerException e)
               {
                   Log.v("MP error",e.toString());
               }

            }

            mp.setOnCompletionListener(new OnCompletionListener(){ 

                   // @Override 
                   public void onCompletion(MediaPlayer arg0) { 
                      mp.release();
                      status = false;

                   } 
              }

          ); 



        }
    };

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

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

发布评论

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

评论(1

忘羡 2024-09-01 12:43:01

两件事:
1. 调试崩溃并查看失败的位置(哪一行)。
2. 用 try/catch 包围整个语句并简单地捕获 例外

如果您有异常或更好地了解代码失败的地方,那么向您提供有关如何修复它的建议会容易得多……事实上,您甚至可能不需要建议来修复它,您也许最终你会自己解决问题,然后你就会收获自己成功的果实。

根据评论更新:
MediaPlayer 的文档指出了可能的内容考虑到OP所看到的症状,问题是:

要停止播放,请调用stop()。如果您想稍后重播媒体,那么
在调用之前,您必须 reset()prepare() MediaPlayer 对象
再次start()。 (create() 第一次调用 prepare()。)

看起来如果播放按钮被按下太多次,那么媒体可能最终不会出现在准备状态并因此抛出一些异常。禁用播放按钮的想法是有效的,它应该可以解决这种情况。

下面是一些关于您希望程序执行的操作的说明性代码:

private OnClickListener btnMercyListener = new OnClickListener()
{
    public void onClick(View v)
    {
        if(isPressed)
        {
            return;
        }

        isPressed = true;

        // create your media player
        mp = MediaPlayer.create(iMEvil.this,R.raw.mercy); 

        // set your listener
        mp.setOnCompletionListener(mp.setOnCompletionListener(new OnCompletionListener(){ 

            // @Override 
            public void onCompletion(MediaPlayer arg0) {
                    if(!isPressed)
                    {
                        return;
                    }

                    isPressed = false;

                    // re-enable your play button
                    playButton.enable();

                    // disable the pause button
                    pauseButton.disable();

                    mp.release();
                    mp.prepare();
                } 
            }
        );

        // disable the play button
        playButton.disable();

        // enable the pause button
        pauseButton.enable();

        // start playback
        mp.start();
    }
};

当然,您应该在其中包含适当的 try/catch 语句,这样您的应用程序就不会崩溃,但是此代码应该让您大致了解要做什么。

Two things:
1. Debug the crash and see where it's failing (which line).
2. Surround the whole statement with a try/catch and simply catch an Exception.

If you have an exception or a better idea where your code is failing, then it will be much easier to give you advice on how to fix it... as a matter of fact, you might not even need advice to fix it, you might end up solving the problem by yourself and then you will reap the fruits of your own success.

Update per comments:
The documentation for MediaPlayer indicates what might be the problem given the symptoms the OP is seeing:

To stop playback, call stop(). If you wish to later replay the media, then
you must reset() and prepare() the MediaPlayer object before calling
start() again. (create() calls prepare() the first time.)

It looks like if the play button is pressed too many times, then the media may end up not being in the prepared state and thus throw some exception. The idea of disabling the play button is valid and it should take care of this situation.

Here is some illustrative code on what you want your program to do:

private OnClickListener btnMercyListener = new OnClickListener()
{
    public void onClick(View v)
    {
        if(isPressed)
        {
            return;
        }

        isPressed = true;

        // create your media player
        mp = MediaPlayer.create(iMEvil.this,R.raw.mercy); 

        // set your listener
        mp.setOnCompletionListener(mp.setOnCompletionListener(new OnCompletionListener(){ 

            // @Override 
            public void onCompletion(MediaPlayer arg0) {
                    if(!isPressed)
                    {
                        return;
                    }

                    isPressed = false;

                    // re-enable your play button
                    playButton.enable();

                    // disable the pause button
                    pauseButton.disable();

                    mp.release();
                    mp.prepare();
                } 
            }
        );

        // disable the play button
        playButton.disable();

        // enable the pause button
        pauseButton.enable();

        // start playback
        mp.start();
    }
};

Of course you should have the appropriate try/catch statements in there so your app doesn't crash, but this code should give you a general idea of what to do.

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