androidmedia-player

Android stops stream from MediaPlayer


I am using MediaPlayer to play radio stream. The problem occurs when I background the main activity which plays the stream.

When the phone uses too much resources (for example while trying to display a list of installed applications) the stream stopps. I suspect, that Android shuts down the stream in order to save up some resources.

This is how I prepare the mediaplayer when I start:

radioPlayer=new MediaPlayer();
radioPlayer.setDataSource(streamLocation); //with a try/catch of course in the full code
radioPlayer.prepareAsync();
radioPlayer.seekTo(0);
radioPlayer.setOnInfoListener(this);
radioPlayer.setOnPreparedListener(new OnPreparedListener(){                 
public void onPrepared(MediaPlayer mp) 
        radioPlayer.start();
});

It is all running on the main thread.


Solution

  • Try to have the mediaplayer run as a service instead of within the activity. That way, when your app goes into suspend, you can have mediaplayer play in the background. When the user breaks focus away from the app (for instance, if a phone call comes in) the activity will suspend or destroy itself. However, services are designed to run in the background alongside everything that is running in the Android OS.

    Services