androidsleepwakeup

Radio Stream stops when device goes to sleep mode


I have simple app with background service that play radio stream with media player but the media player stops 2-3 min after i lock my phone...

Code for my background service:

public class ServiceMusic extends Service {
MediaPlayer mediaPlayer;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
    if (mediaPlayer != null) {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(this, Uri.parse("http://188.40.62.20:8014"));
        mediaPlayer.prepare();
    }catch (IOException e){
        Log.e("---------------","---------------------: " + e.getMessage());
    }
}
@Override
public void onStart(Intent intent, int startid) {
    mediaPlayer.start();
}
@Override
public void onDestroy() {
    mediaPlayer.stop();
}

i have writen in onCreate but it still stops...

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyWakelockTag");
wakeLock.acquire();

then i added to mediaPlayer awake mode but this doesent help too

mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

and to i added permission WAKE_LOCK to android manifest file...

i hope someone can help me.


Solution

  • If you are not using foreground service to run background task, then it is possible that service might get destroyed with activity, which also happens when device get lock, have a look on this answer it might help in your case:

    Cannot keep android service alive after app is closed

    Cheers!!