android mediaplayer mediacontroller timeout

Deadly 提交于 2020-01-04 02:47:05

问题


Ive implemented a mediaplayer and mediacontroller that streams a mp3 url. However my device on the TMobile network doesnt get a great 3G signal so it operates on EDGE. Im assuming that the mediaplayer is crashing because the stream is too slow or incomplete, is there a timeout I can set?


回答1:


There is no timeout method in MediaPlayer, but you can implement it yourself - there are variety of ways you can do that. I suggest one of them, that I used myself and it worked for me - BroadcastReceiver Code would look like that:

public class ConnectivityCheckingReceiver extends WakefulBroadcastReceiver
{
  private AlarmManager alarmManager;
  private PendingIntent pendingIntent;

  @Override
  public void onReceive(Context context, Intent intent)
  {
    if (MusicService.mediaPlayer != null)
    {
        if (!MusicService.mediaPlayer.isPlaying())
            Log.v("Music", "Music is NOT playing"); 
            //stop service and notify user
        else
            Log.v("Music", "Music is playing");
    }
    else
    {
        Log.v("Music", "User stopped player");
    }
  }
  public void setAlarm (Context context, int hour, int minute, int second)
  {
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, ConnectivityCheckingReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);      
  }
}

In your activity/service/fragment you add this line:

ConnectivityCheckingReceiver conCheck = new ConnectivityCheckingReceiver();
conCheck.setAlarm(context, hour, min, second);

You will need to implement hour/min/second checking logic yourself, but it's easily done with libraries like Joda Time. And don't forget to add to your manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".receivers.ConnectivityCheckingReceiver" />

Ps, my solution is not perfect, but I have not seen any good answers for this question, so if find one, please share it.




回答2:


You can also use prepare() instead of prepareAsync() on MediaPlayer and put it inside asyncTask or worker thread; then you can inmplement timeout functionality by yourself. I will add a code sample later, but the idea is clear I think.




回答3:


Indeed Mediaplayer takes some time to buffer and on slow networks that can lead to problems.

You cannot control this process, there is no user set timeout, but you can make sure it doesn't crash your app by catching all exception that methods such as setDataSource(), prepare(), prepareAsync(), and start() can throw.



来源:https://stackoverflow.com/questions/8542607/android-mediaplayer-mediacontroller-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!