Fix Sonic playback

This commit is contained in:
ByteHamster 2019-04-07 12:54:12 +02:00
parent 2c9cb25eda
commit 156a20734a
2 changed files with 22 additions and 8 deletions

View File

@ -132,7 +132,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
@Override
public void playMediaObject(@NonNull final Playable playable, final boolean stream, final boolean startWhenPrepared, final boolean prepareImmediately) {
Log.d(TAG, "playMediaObject(...)");
executor.useMainThread = true; // ExoPlayer needs to be initialized in main thread
executor.useMainThread = UserPreferences.useExoplayer();
executor.submit(() -> {
playerLock.lock();
try {
@ -400,6 +400,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
*/
@Override
public void reinit() {
executor.useMainThread = UserPreferences.useExoplayer();
executor.submit(() -> {
playerLock.lock();
Log.d(TAG, "reinit()");
@ -783,13 +784,10 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
if (UserPreferences.useExoplayer()) {
mediaPlayer = new ExoPlayerWrapper(context);
executor.useMainThread = true;
} else if (media.getMediaType() == MediaType.VIDEO) {
mediaPlayer = new VideoPlayer();
executor.useMainThread = false;
} else {
mediaPlayer = new AudioPlayer(context);
executor.useMainThread = false;
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
@ -852,6 +850,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
@Override
protected Future<?> endPlayback(final boolean hasEnded, final boolean wasSkipped,
final boolean shouldContinue, final boolean toStoppedState) {
executor.useMainThread = UserPreferences.useExoplayer();
return executor.submit(() -> {
playerLock.lock();
releaseWifiLockIfNecessary();
@ -1058,7 +1057,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
playerLock.unlock();
};
if (mediaPlayer instanceof ExoPlayerWrapper) {
if (executor.useMainThread) {
r.run();
} else {
new Thread(r).start();

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.core.service.playback;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Vibrator;
import android.support.annotation.NonNull;
import android.util.Log;
@ -126,8 +127,8 @@ public class PlaybackServiceTaskManager {
*/
public synchronized void startPositionSaver() {
if (!isPositionSaverActive()) {
Handler handler = new Handler(); // Execute on main thread
Runnable positionSaver = () -> handler.post(callback::positionSaverTick);
Runnable positionSaver = callback::positionSaverTick;
positionSaver = useMainThreadIfNecessary(positionSaver);
positionSaverFuture = schedExecutor.scheduleWithFixedDelay(positionSaver, POSITION_SAVER_WAITING_INTERVAL,
POSITION_SAVER_WAITING_INTERVAL, TimeUnit.MILLISECONDS);
@ -160,6 +161,7 @@ public class PlaybackServiceTaskManager {
public synchronized void startWidgetUpdater() {
if (!isWidgetUpdaterActive()) {
Runnable widgetUpdater = callback::onWidgetUpdaterTick;
widgetUpdater = useMainThreadIfNecessary(widgetUpdater);
widgetUpdaterFuture = schedExecutor.scheduleWithFixedDelay(widgetUpdater, WIDGET_UPDATER_NOTIFICATION_INTERVAL,
WIDGET_UPDATER_NOTIFICATION_INTERVAL, TimeUnit.MILLISECONDS);
@ -186,7 +188,8 @@ public class PlaybackServiceTaskManager {
sleepTimerFuture.cancel(true);
}
sleepTimer = new SleepTimer(waitingTime, shakeToReset, vibrate);
sleepTimerFuture = schedExecutor.schedule(sleepTimer, 0, TimeUnit.MILLISECONDS);
Runnable runnable = useMainThreadIfNecessary(sleepTimer);
sleepTimerFuture = schedExecutor.schedule(runnable, 0, TimeUnit.MILLISECONDS);
}
/**
@ -269,6 +272,7 @@ public class PlaybackServiceTaskManager {
}
Log.d(TAG, "Chapter loader stopped");
};
chapterLoader = useMainThreadIfNecessary(chapterLoader);
chapterLoaderFuture = schedExecutor.submit(chapterLoader);
}
@ -294,6 +298,17 @@ public class PlaybackServiceTaskManager {
schedExecutor.shutdown();
}
private Runnable useMainThreadIfNecessary(Runnable runnable) {
if (Looper.myLooper() == Looper.getMainLooper()) {
// Called in main thread => ExoPlayer is used
// Run on ui thread even if called from schedExecutor
Handler handler = new Handler();
return () -> handler.post(runnable);
} else {
return runnable;
}
}
/**
* Sleeps for a given time and then pauses playback.
*/