简体   繁体   中英

How to play the audio file in android using mediaplayer when screen sleeps

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.

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();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM