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

View File

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