Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

Dive into deep

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top