apbarratt
apbarratt

Reputation: 685

Android MediaPlayer WakeLock not Working

I've made a basic radio player, the code that makes it play is below and works perfectly except for the setWakeMode method. When I turn my phone onto standby, the audio will play well for up to 2 minutes, after which, it begins to stop and start. Any ideas?

N.B. radioPlayer is an instance of MediaPlayer.

public boolean startRadio()
{   
    try
    {
        String url = getString(R.string.radioURL); // Radio url.
        radioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        radioPlayer.setDataSource(url);
        radioPlayer.prepare(); // might take long! (for buffering, etc)

        radioPlayer.setWakeMode(this.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        radioPlayer.start();
        return true;
    }
    catch(Exception e)
    {
        showAlert(getString(R.string.error), getString(R.string.radioError));
        radioPlayer.release();
        radioPlayer = new MediaPlayer();
        return false;
    }
}

UPDATE: After reviewing another thread elsewhere, I've discovered that this problem seems to be unique to HTC phones, in fact, my Samsung Galaxy Tab survives even without the wake lock at all. Any ideas?

Upvotes: 1

Views: 5632

Answers (4)

Soufiane Elbaz
Soufiane Elbaz

Reputation: 9

First you should use the prepareAsync() instead of prepare() because the buffering might take a little while, and with this method the whole work will be done in a separated Thread ... You can also add some listeners that will surely help organizing your code and methods ;) .

Upvotes: -1

Wayne
Wayne

Reputation: 6449

Your MediaPlayer stop because Wifi go into sleep mode, so you should try using WifiLock to prevent that.

Upvotes: 6

Dheeraj Bhaskar
Dheeraj Bhaskar

Reputation: 19039

set keepScreenOn=true

The easiest thing.

*on one of the views, find the property called keepScreenOn and set it* true.

That's it. It works like a charm :)

Upvotes: -1

EGHDK
EGHDK

Reputation: 18130

Android Developers Google + page re-posted[ this

Small tip: if you want to keep the screen on while the user is in your app (for example playing a game or watching a video), the best way to do this is with one of these:

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON

http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)

http://developer.android.com/reference/android/media/MediaPlayer.html#setScreenOnWhilePlaying(boolean)

Don't use a http://developer.android.com/reference/android/os/PowerManager.WakeLock.html unless you have to, since this requires you requesting the WAKE_LOCK permission (so one more permission shown to the user leaving them less likely to install your app). Also using one of the previous APIs allows the system to manage the wake lock for you, so you can not have bugs where the user leaves your app and the screen is still being kept on.

Upvotes: 1

Related Questions