如何在Android中发生错误时重新启动MediaPlayer? [英] How to implement a MediaPlayer restart on errors in Android?

查看:122
本文介绍了如何在Android中发生错误时重新启动MediaPlayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当出现错误(与服务器的连接丢失,网络无法访问等)时,我正在尝试在Android中实现MediaPlayer的重启. 我已经看到了许多代码示例,但都有些不标准.我认为必须有一种标准的重启方法,对应于developer.android.com,但是从这里还不清楚如何设置侦听器,以便在出现此类错误时重启播放器.

I'm trying to implement the restart of MediaPlayer in Android, when errors happen (connection with server lost, network is unreachable and other). I've seen many code examples, but all are somewhat non-standard. I think there must be the standard way to restart corresponding to the developer.android.com, but it's not clear from here, how to set the listener which would restart player on such errors.

这是我的代码的一部分:

Here are the parts of my code:

public class PlayerService extends Service implements OnErrorListener {
....
////////////////////

this.mplayer = MediaPlayer.create(c, Uri.parse(url));
mplayer.setOnErrorListener(onErrorListener);
////////////////////

MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener()   
    {  
         @Override  
         public boolean onError(MediaPlayer mp, int what, int extra)   
         {  
              Log.e(getPackageName(), String.format("Error(%s%s)", what, extra));
              playlist="ERROR";

              restart();
              return true;  
         }  
    }; 

@Override
    public boolean onError(MediaPlayer player, int what, int extra) {
        restart();
        return true;
    };

public void restart()
    {
        try
        {
        playlist="RELOADING";
        for (int u=1; u<=5; u++)
        {
        Thread.sleep(5000);
        mplayer.stop();
        mplayer.release();
        mplayer=null;
    playSong(getApplicationContext(),currenturl);
        };

        }
        catch (Exception e)
        {
        playlist="RELOADING ERROR";

        }



    }
//////////////
....

}

我可以正确设置监听器吗?我不确定在哪里放置onError函数,所以我有两个.当我通过将手机设置为飞行模式来模拟错误时,侦听器将触发"RELOADING"和"RELOADING ERROR"标题.但是在网络打开后,播放器不会重启.没有声音.

Am I setting the listener right? I'm not sure where to put onError function so I have 2 of them. When I emulate the error by setting the phone to the flight mode, the listener fires "RELOADING" and "RELOADING ERROR" title. But after the network is on, no restart of the player happens. There is no sound.

这是怎么了?播放器无法重新启动.

What's wrong here? The player cannot restart.

请帮助使代码可行.也可以是连接跳过和IO异常.

Please help to make the code workable. Also can be connection skips and IO Exception.

推荐答案

概述

我遇到了类似的问题,根据文档,它表明您需要做的就是重置媒体播放器:

Overview

I ran into a similar issue and based on the documentation it indicates that all you need to do is reset your media player:

为了重用处于Error状态的MediaPlayer对象并从错误中恢复,可以调用reset()将对象恢复为Idle状态.

In order to reuse a MediaPlayer object that is in the Error state and recover from the error, reset() can be called to restore the object to its Idle state.

您当前正在做的是停止并释放(mplayer.stop()mplayer.release())处于错误状态的媒体播放器.这应该导致引发类似IllegalStateException的事件.如果没有引发错误,您仍将尝试在空对象中开始播放歌曲.您应该使用mplayer.reset()函数,而不是调用stop和release,然后将变量设置为null.

What you are currently doing is stopping and releasing (mplayer.stop() and mplayer.release()) a media player that is in the Error state. This should be causing something like an IllegalStateException to be raised. If it's not throwing an error you would still be trying to start a song in a null object. Instead of calling stop and release then setting the variable to null you should be using the mplayer.reset() function.

另一种选择是启动一个新的媒体播放器,但文档详细介绍了新实例化的MediaPlayer对象与调用了reset()的对象之间的细微差别.

Another option would be to initiate a new media player but the documentation details the subtle difference between a newly instantiated MediaPlayer object and one that has had reset() called on it.


基于此信息,类似以下内容的内容应该可以解决您的问题:

Based on this information something like the following should fix your issue:

 public boolean onError(MediaPlayer mp, int what, int extra)   
 {  
      Log.e(getPackageName(), String.format("Error(%s%s)", what, extra));
      playlist="ERROR";

      if(what == MediaPlayer.MEDIA_ERROR_SERVER_DIED)
          mp.reset();

      else if(what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
          mp.reset();
      // Deal with any other errors you need to. 

      // I'm under the assumption you set the path to the song
      // and handle onPrepare, start(), etc with this function
      playSong(getApplicationContext(),currenturl);
      mplayer.setOnErrorListener(this);
      mplayer.setOnCompletionListener(this);
      mplayer.setOnPreparedListener(this);

      return true;  
 }  

有关可能的列表,请参见媒体播放器常量文档错误.

See media player constant documentation for a list of potential errors.


关于设置错误侦听器,这是我过去的实现方式:

As for setting the error listener, here is how I've implemented it in the past:

public class MediaPlayerActivity extends Activity implements OnCompletionListener,
    OnPreparedListener, AnimationListener, OnErrorListener{

    private MediaPlayer mediaPlayer;

    @Override
    public boolean onError(final MediaPlayer arg0, final int arg1, final int arg2) {
        // Error handling logic here
        return true;
    }

    protected void onResume(){
        super.onResume();
        // do some onResume logic
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnPreparedListener(this);
        // finish on resume and start up media player
    }
}

然后我处理由onResume()启动的另一个函数中的媒体播放器的加载.

I then handle loading up the media player in another function initiated by onResume().

这篇关于如何在Android中发生错误时重新启动MediaPlayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆