Using PlaybackService to distribute position events instead of PlaybackController
This commit is contained in:
parent
97c2b47a4a
commit
72fc4f5eb0
@ -0,0 +1,8 @@
|
||||
package de.danoeh.antennapod.core.event;
|
||||
|
||||
public class PlaybackPositionEvent {
|
||||
|
||||
public PlaybackPositionEvent() {
|
||||
|
||||
}
|
||||
}
|
@ -45,10 +45,14 @@ import com.bumptech.glide.request.target.Target;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import de.danoeh.antennapod.core.ClientConfig;
|
||||
import de.danoeh.antennapod.core.R;
|
||||
import de.danoeh.antennapod.core.event.MessageEvent;
|
||||
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
|
||||
import de.danoeh.antennapod.core.event.ServiceEvent;
|
||||
import de.danoeh.antennapod.core.feed.Chapter;
|
||||
import de.danoeh.antennapod.core.feed.Feed;
|
||||
@ -213,6 +217,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
||||
private PlaybackServiceFlavorHelper flavorHelper;
|
||||
private PlaybackServiceStateManager stateManager;
|
||||
|
||||
private final ScheduledThreadPoolExecutor positionEventDistributorExecutor = new ScheduledThreadPoolExecutor(1);
|
||||
private ScheduledFuture<?> positionEventDistributorFuture;
|
||||
|
||||
/**
|
||||
* Used for Lollipop notifications, Android Wear, and Android Auto.
|
||||
*/
|
||||
@ -734,6 +741,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
||||
// remove notification on pause
|
||||
stateManager.stopForeground(true);
|
||||
}
|
||||
cancelPositionObserver();
|
||||
writePlayerStatusPlaybackPreferences();
|
||||
break;
|
||||
|
||||
@ -745,6 +753,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
||||
case PLAYING:
|
||||
writePlayerStatusPlaybackPreferences();
|
||||
setupNotification(newInfo);
|
||||
setupPositionUpdater();
|
||||
stateManager.validStartCommandWasReceived();
|
||||
// set sleep timer if auto-enabled
|
||||
if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING &&
|
||||
@ -1653,6 +1662,23 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|
||||
return mediaPlayer.getVideoSize();
|
||||
}
|
||||
|
||||
private void setupPositionUpdater() {
|
||||
if (positionEventDistributorFuture == null ||
|
||||
positionEventDistributorFuture.isCancelled() ||
|
||||
positionEventDistributorFuture.isDone()) {
|
||||
Log.d(TAG, "Setting up position observer");
|
||||
positionEventDistributorFuture = positionEventDistributorExecutor.scheduleWithFixedDelay(
|
||||
() -> EventBus.getDefault().post(new PlaybackPositionEvent()), 1000, 1000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
private void cancelPositionObserver() {
|
||||
if (positionEventDistributorFuture != null) {
|
||||
boolean result = positionEventDistributorFuture.cancel(true);
|
||||
Log.d(TAG, "PositionObserver cancelled. Result: " + result);
|
||||
}
|
||||
}
|
||||
|
||||
private final MediaSessionCompat.Callback sessionCallback = new MediaSessionCompat.Callback() {
|
||||
|
||||
private static final String TAG = "MediaSessionCompat";
|
||||
|
@ -21,11 +21,10 @@ import android.widget.ImageButton;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import de.danoeh.antennapod.core.R;
|
||||
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
|
||||
import de.danoeh.antennapod.core.event.ServiceEvent;
|
||||
import de.danoeh.antennapod.core.feed.Chapter;
|
||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||
@ -69,9 +68,6 @@ public class PlaybackController {
|
||||
private final ScheduledThreadPoolExecutor schedExecutor;
|
||||
private static final int SCHED_EX_POOLSIZE = 1;
|
||||
|
||||
private MediaPositionObserver positionObserver;
|
||||
private ScheduledFuture<?> positionObserverFuture;
|
||||
|
||||
private boolean mediaInfoLoaded = false;
|
||||
private boolean released = false;
|
||||
private boolean initialized = false;
|
||||
@ -177,7 +173,6 @@ public class PlaybackController {
|
||||
} catch (IllegalArgumentException e) {
|
||||
// ignore
|
||||
}
|
||||
cancelPositionObserver();
|
||||
schedExecutor.shutdownNow();
|
||||
media = null;
|
||||
released = true;
|
||||
@ -254,29 +249,6 @@ public class PlaybackController {
|
||||
.getIntent());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void setupPositionObserver() {
|
||||
if (positionObserverFuture == null ||
|
||||
positionObserverFuture.isCancelled() ||
|
||||
positionObserverFuture.isDone()) {
|
||||
|
||||
Log.d(TAG, "Setting up position observer");
|
||||
positionObserver = new MediaPositionObserver();
|
||||
positionObserverFuture = schedExecutor.scheduleWithFixedDelay(
|
||||
positionObserver, MediaPositionObserver.WAITING_INTERVALL,
|
||||
MediaPositionObserver.WAITING_INTERVALL,
|
||||
TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
private void cancelPositionObserver() {
|
||||
if (positionObserverFuture != null) {
|
||||
boolean result = positionObserverFuture.cancel(true);
|
||||
Log.d(TAG, "PositionObserver cancelled. Result: " + result);
|
||||
}
|
||||
}
|
||||
|
||||
private final ServiceConnection mConnection = new ServiceConnection() {
|
||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||
if(service instanceof PlaybackService.LocalBinder) {
|
||||
@ -337,7 +309,6 @@ public class PlaybackController {
|
||||
onBufferUpdate(progress);
|
||||
break;
|
||||
case PlaybackService.NOTIFICATION_TYPE_RELOAD:
|
||||
cancelPositionObserver();
|
||||
mediaInfoLoaded = false;
|
||||
queryService();
|
||||
onReloadNotification(intent.getIntExtra(
|
||||
@ -447,7 +418,6 @@ public class PlaybackController {
|
||||
case PAUSED:
|
||||
clearStatusMsg();
|
||||
checkMediaInfoLoaded();
|
||||
cancelPositionObserver();
|
||||
onPositionObserverUpdate();
|
||||
updatePlayButtonAppearance(playResource, playText);
|
||||
if (!PlaybackService.isCasting() &&
|
||||
@ -463,7 +433,6 @@ public class PlaybackController {
|
||||
onAwaitingVideoSurface();
|
||||
setScreenOn(true);
|
||||
}
|
||||
setupPositionObserver();
|
||||
updatePlayButtonAppearance(pauseResource, pauseText);
|
||||
break;
|
||||
case PREPARING:
|
||||
@ -581,7 +550,6 @@ public class PlaybackController {
|
||||
*/
|
||||
public void onSeekBarStartTrackingTouch(SeekBar seekBar) {
|
||||
// interrupt position Observer, restart later
|
||||
cancelPositionObserver();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -590,7 +558,6 @@ public class PlaybackController {
|
||||
public void onSeekBarStopTrackingTouch(SeekBar seekBar, float prog) {
|
||||
if (playbackService != null && media != null) {
|
||||
playbackService.seekTo((int) (prog * media.getDuration()));
|
||||
setupPositionObserver();
|
||||
}
|
||||
}
|
||||
|
||||
@ -836,19 +803,4 @@ public class PlaybackController {
|
||||
}
|
||||
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes the current position of the media file that is playing.
|
||||
*/
|
||||
public class MediaPositionObserver implements Runnable {
|
||||
|
||||
static final int WAITING_INTERVALL = 1000;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (playbackService != null && playbackService.getStatus() == PlayerStatus.PLAYING) {
|
||||
activity.runOnUiThread(PlaybackController.this::onPositionObserverUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user