Rename onCreate/onDestroy functions; create a new MediaPlayer instance on release()

This commit is contained in:
tzugen 2021-03-30 14:38:59 +02:00
parent 974afda777
commit 05067aaf3c
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
2 changed files with 7 additions and 4 deletions

View File

@ -146,7 +146,7 @@ public class MediaPlayerService extends Service
downloader.onCreate();
shufflePlayBuffer.onCreate();
localMediaPlayer.onCreate();
localMediaPlayer.init();
setupOnCurrentPlayingChangedHandler();
setupOnPlayerStateChangedHandler();
setupOnSongCompletedHandler();
@ -198,7 +198,7 @@ public class MediaPlayerService extends Service
instance = null;
try {
localMediaPlayer.onDestroy();
localMediaPlayer.release();
downloader.stop();
shufflePlayBuffer.onDestroy();
} catch (Throwable ignored) {

View File

@ -85,7 +85,7 @@ class LocalMediaPlayer(
private val pm = context.getSystemService(POWER_SERVICE) as PowerManager
private val wakeLock: WakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, this.javaClass.name)
fun onCreate() {
fun init() {
Thread {
Thread.currentThread().name = "MediaPlayerThread"
Looper.prepare()
@ -126,7 +126,7 @@ class LocalMediaPlayer(
Timber.i("LocalMediaPlayer created")
}
fun onDestroy() {
fun release() {
reset()
try {
val i = Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION)
@ -136,6 +136,9 @@ class LocalMediaPlayer(
EqualizerController.release()
VisualizerController.release()
mediaPlayer.release()
mediaPlayer = MediaPlayer()
if (nextMediaPlayer != null) {
nextMediaPlayer!!.release()
}