0

I am trying to play the audio files using media player. It will stop after screen lock. It is annoying can anyone suggest, how to handle this?? I want to play the music even screen goes off.

Thanks in advance.

Archana
  • 378
  • 3
  • 17

1 Answers1

1

I think you can use Media Playback

public class MyService extends Service implements MediaPlayer.OnPreparedListener {
private static final String ACTION_PLAY = "com.example.action.PLAY";
MediaPlayer mMediaPlayer = null;

public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    if (intent.getAction().equals(ACTION_PLAY)) {
        mMediaPlayer = ... // initialize it here
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
}

/** Called when MediaPlayer is ready */
public void onPrepared(MediaPlayer player) {
    player.start();
}

}

Quang Doan
  • 632
  • 4
  • 12
  • additionally(if required) you can return START_STICKY in onStartCommand. START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent. – rahul Dec 22 '15 at 07:11