Skip to content Skip to sidebar Skip to footer

Android Mediaplayer Mediacontroller Timeout

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

Solution 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:

publicclassConnectivityCheckingReceiverextendsWakefulBroadcastReceiver
{
  private AlarmManager alarmManager;
  private PendingIntent pendingIntent;

  @Override
  publicvoidonReceive(Context context, Intent intent)
  {
    if (MusicService.mediaPlayer != null)
    {
        if (!MusicService.mediaPlayer.isPlaying())
            Log.v("Music", "Music is NOT playing"); 
            //stop service and notify userelse
            Log.v("Music", "Music is playing");
    }
    else
    {
        Log.v("Music", "User stopped player");
    }
  }
  publicvoidsetAlarm (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-permissionandroid:name="android.permission.WAKE_LOCK" /><receiverandroid: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.

Solution 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.

Solution 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.

Post a Comment for "Android Mediaplayer Mediacontroller Timeout"