Fixed foreground service stop and start

Fixed play on restore
This commit is contained in:
Nite 2020-06-29 14:44:19 +02:00
parent bd77c2a851
commit fdc1748f8a
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
3 changed files with 27 additions and 28 deletions

View File

@ -119,21 +119,20 @@ public class LocalMediaPlayer
public void onCreate() public void onCreate()
{ {
new Thread(new Runnable()
{
@Override
public void run()
{
Thread.currentThread().setName("MediaPlayerThread");
Looper.prepare();
if (mediaPlayer != null) if (mediaPlayer != null)
{ {
mediaPlayer.release(); mediaPlayer.release();
} }
mediaPlayer = new MediaPlayer(); mediaPlayer = new MediaPlayer();
new Thread(new Runnable()
{
@Override
public void run()
{
Thread.currentThread().setName("MediaPlayerThread");
Looper.prepare();
mediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK); mediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener()

View File

@ -96,6 +96,9 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
{ {
externalStorageMonitor.onDestroy(); externalStorageMonitor.onDestroy();
context.stopService(new Intent(context, MediaPlayerService.class)); context.stopService(new Intent(context, MediaPlayerService.class));
shufflePlayBuffer.onDestroy();
downloader.onDestroy();
Log.i(TAG, "MediaPlayerControllerImpl destroyed"); Log.i(TAG, "MediaPlayerControllerImpl destroyed");
} }
@ -110,8 +113,6 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
@Override @Override
public void accept(MediaPlayerService mediaPlayerService) { public void accept(MediaPlayerService mediaPlayerService) {
mediaPlayerService.play(currentPlayingIndex, autoPlayStart); mediaPlayerService.play(currentPlayingIndex, autoPlayStart);
}
});
if (localMediaPlayer.currentPlaying != null) if (localMediaPlayer.currentPlaying != null)
{ {
@ -123,17 +124,14 @@ public class MediaPlayerControllerImpl implements MediaPlayerController
{ {
if (localMediaPlayer.currentPlaying.isCompleteFileAvailable()) if (localMediaPlayer.currentPlaying.isCompleteFileAvailable())
{ {
MediaPlayerService.executeOnStartedMediaPlayerService(context, new Consumer<MediaPlayerService>() {
@Override
public void accept(MediaPlayerService mediaPlayerService) {
localMediaPlayer.doPlay(localMediaPlayer.currentPlaying, currentPlayingPosition, autoPlay); localMediaPlayer.doPlay(localMediaPlayer.currentPlaying, currentPlayingPosition, autoPlay);
} }
});
}
} }
} }
autoPlayStart = false; autoPlayStart = false;
} }
});
}
} }
@Override @Override

View File

@ -191,9 +191,9 @@ public class MediaPlayerService extends Service
instance = null; instance = null;
try { try {
downloadQueueSerializer.serializeDownloadQueueNow(downloader.downloadList,
downloader.getCurrentPlayingIndex(), getPlayerPosition());
localMediaPlayer.onDestroy(); localMediaPlayer.onDestroy();
shufflePlayBuffer.onDestroy();
downloader.onDestroy();
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
@ -205,7 +205,7 @@ public class MediaPlayerService extends Service
synchronized (instanceLock) synchronized (instanceLock)
{ {
// currentPlaying could be changed from another thread in the meantime, so check again before stopping for good // currentPlaying could be changed from another thread in the meantime, so check again before stopping for good
if (localMediaPlayer.currentPlaying == null) stopSelf(); if (localMediaPlayer.currentPlaying == null || localMediaPlayer.playerState == STOPPED) stopSelf();
} }
} }
@ -385,6 +385,8 @@ public class MediaPlayerService extends Service
} }
else else
{ {
setCurrentPlaying(index);
if (start) if (start)
{ {
if (jukeboxMediaPlayer.getValue().isEnabled()) if (jukeboxMediaPlayer.getValue().isEnabled())