MediaPlayer, Doze Mode, Wake Lock & Foreground Service

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

I have read several posts regarding the new 'Doze' Mode in Android M, also the article on Android developers website : https://developer.android.com/training/monitoring-device-state/doze-standby.html yet I still have an issue with a Foreground Service correctly running on a phone (Android 6.0.1).

Issue:

I have a Service which handles playing music using a MediaPlayer object, the Service Implements onCompletion and onPrepared MediaPlayer interfaces.

Pseudo code:

    public class MusicService extends Service implements MediaPlayer.OnPreparedListener,             MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {              // Class Variables/Methods etc...              @Override             public void onPrepared(MediaPlayer mp) {                     mp.start();                     // update notification using startForeground()             }               @Override                 public void onCompletion(MediaPlayer mp) {                     // Go and get another music track in separate thread                    // (Have used AsyncTask and RX Java/RX Android to test if problem is here - works fine)             }              @Override                 public boolean onError(MediaPlayer mp, int what, int extra) {                      // handleError                      return false;             }      } 

The app works well, when the phone is plugged into a power source, or when the screen is on, or app is open and screen is on. However when the phone has been sitting idle and playing music (between 5 - 40mins) the Service fails to retrieve the next track once onCompletion() is called.

The Service is setup and meets the following conditions:

  • The Service Notification is started with startForeground
  • The MediaPlayer Object has a partial wakelock PARTIAL_WAKE_LOCK
  • The Service runs in a separate process to the main app/UI
  • All Media uses prepareAsync() (logic to get the next track uri is performed in background thread, prepareAsync() is called only in the Main thread)

Things I have tried:

  • Retrieving the next song (after onCompletion is called) in the Main thread instead of a background thread
  • Acquiring a separate Partial WakeLock in onCompletion(), and holding and then releasing it when onPrepared() is called.
  • Few other trivial things that didn't make a difference...

I have a logcat output, from when the phone is left, untouched for a period of time with the screen off, and UNPLUGGED (logcat was obtained once I plugged it back in and it updated the log) which demonstrates the problem.

06-15 02:31:39.346 musicplayer:music_service E/MusicService: onPrepared : Tom Petty GH - 11 - You Got Lucky 06-15 02:31:39.548 musicplayer:music_service D/MusicService: Releasing wakelock 06-15 02:35:15.467 musicplayer:music_service E/MusicService: onCompletion called 06-15 02:35:15.467 musicplayer:music_service D/MusicService: Acquiring wakelock 06-15 02:35:15.514 musicplayer:music_service D/NextActionStrategy: Retrieving song - rx java - Main Ui Thread =false 06-15 02:35:15.870 musicplayer:music_service D/NextActionStrategy: Song Retrieved : Ten Years- Main Ui Thread = true 06-15 02:35:15.950 musicplayer:music_service E/MusicService: onPrepared : Ten Years 06-15 02:35:16.149 musicplayer:music_service D/MusicService: Releasing wakelock 06-15 02:38:59.002 musicplayer:music_service E/MusicService: onCompletion called 06-15 02:38:59.002 musicplayer:music_service D/MusicService: Acquiring wakelock 06-15 02:38:59.049 musicplayer:music_service D/NextActionStrategy: Retrieving song - rx java - Main Ui Thread =false 06-15 02:38:59.211 musicplayer:music_service D/NextActionStrategy: Song Retrieved : What Am I- Main Ui Thread = true 06-15 02:50:07.642 musicplayer:music_service E/MusicService: onPrepared : What Am I 06-15 02:50:07.976 musicplayer:music_service D/MusicService: Releasing wakelock 06-15 02:50:22.097 musicplayer:music_service D/MusicService: onStartCommand called : action.PAUSE 

The routine would be

  • play a track
  • onCompletion() called
  • aquire wakelock get the next track
  • PrepareAsync()
  • onPrepared called - start playing and update notification using startForeground

From the logs you can see at 02:38:59.xxx onCompletion is called, a track is found, and returns to the Main thread where prepareAsync() is called, but then nothing happens for 12mins until I manually wake the phone up, and at which point just code continues where is stopped and the track starts playing.

I don't believe its the device as other music apps I use work fine in this situation, however I am limited to testing on one real device only :(

If you've got this far thanks for bearing with me! Any suggestions would be appreciated (hopefully regarding a solution and not my music tastes!).

回答1:

I just wanted you to see how I am implementing my startForeground() method. Maybe this can help:

 void setUpAsForeground(String text) {     mNotificationManager = (NotificationManager)      this.getSystemService(Context.NOTIFICATION_SERVICE);     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);      NotificationCompat.Builder noti = new NotificationCompat.Builder(this)             .setContentTitle( "My App Title")             .setContentText(text)             .setSmallIcon(R.drawable.ic_stat_recording)             .setStyle(new NotificationCompat.BigTextStyle()                     .bigText(text))              .setPriority(Notification.PRIORITY_MAX)             .setContentIntent(contentIntent)             .setOngoing(false);     final Notification notification = noti.build();     //mNotificationManager.notify(NOTIFICATION_ID, noti.build());     startForeground(NOTIFICATION_ID, notification);  } 

And in onCreate():

 @Override  public void onCreate() {      PowerManager powerManager = (PowerManager)      getSystemService(POWER_SERVICE);     PowerManager.WakeLock wakeLock =      powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,             "MyWakelockTag");     wakeLock.acquire();      *********** } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!