Convert buffering update to event

This commit is contained in:
ByteHamster 2021-10-24 18:28:27 +02:00
parent def0211358
commit 8a4b036148
9 changed files with 93 additions and 102 deletions

View File

@ -42,14 +42,6 @@ public class CancelablePSMPCallback implements PlaybackServiceMediaPlayer.PSMPCa
originalCallback.playbackSpeedChanged(s);
}
@Override
public void onBufferingUpdate(int percent) {
if (isCancelled) {
return;
}
originalCallback.onBufferingUpdate(percent);
}
@Override
public void onMediaChanged(boolean reloadUI) {
if (isCancelled) {

View File

@ -22,11 +22,6 @@ public class DefaultPSMPCallback implements PlaybackServiceMediaPlayer.PSMPCallb
}
@Override
public void onBufferingUpdate(int percent) {
}
@Override
public void onMediaChanged(boolean reloadUI) {

View File

@ -36,6 +36,7 @@ import androidx.core.view.WindowCompat;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
import com.bumptech.glide.Glide;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.playback.BufferUpdateEvent;
import de.danoeh.antennapod.core.event.playback.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.PlayerErrorEvent;
import de.danoeh.antennapod.core.event.playback.PlaybackServiceEvent;
@ -192,21 +193,6 @@ public class VideoplayerActivity extends CastEnabledActivity implements SeekBar.
VideoplayerActivity.this.onPositionObserverUpdate();
}
@Override
public void onBufferStart() {
viewBinding.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onBufferEnd() {
viewBinding.progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onBufferUpdate(float progress) {
viewBinding.sbPosition.setSecondaryProgress((int) (progress * viewBinding.sbPosition.getMax()));
}
@Override
public void onReloadNotification(int code) {
VideoplayerActivity.this.onReloadNotification(code);
@ -252,6 +238,18 @@ public class VideoplayerActivity extends CastEnabledActivity implements SeekBar.
};
}
@Subscribe(threadMode = ThreadMode.MAIN)
@SuppressWarnings("unused")
public void bufferUpdate(BufferUpdateEvent event) {
if (event.hasStarted()) {
viewBinding.progressBar.setVisibility(View.VISIBLE);
} else if (event.hasEnded()) {
viewBinding.progressBar.setVisibility(View.INVISIBLE);
} else {
viewBinding.sbPosition.setSecondaryProgress((int) (event.getProgress() * viewBinding.sbPosition.getMax()));
}
}
protected void loadMediaInfo() {
Log.d(TAG, "loadMediaInfo()");
if (controller == null || controller.getMedia() == null) {

View File

@ -25,6 +25,7 @@ import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.snackbar.Snackbar;
import de.danoeh.antennapod.core.event.playback.BufferUpdateEvent;
import de.danoeh.antennapod.core.event.playback.PlaybackServiceEvent;
import de.danoeh.antennapod.core.event.PlayerErrorEvent;
import org.greenrobot.eventbus.EventBus;
@ -281,25 +282,6 @@ public class AudioPlayerFragment extends Fragment implements
private PlaybackController newPlaybackController() {
return new PlaybackController(getActivity()) {
@Override
public void onBufferStart() {
progressIndicator.setVisibility(View.VISIBLE);
}
@Override
public void onBufferEnd() {
progressIndicator.setVisibility(View.GONE);
}
@Override
public void onBufferUpdate(float progress) {
if (isStreaming()) {
sbPosition.setSecondaryProgress((int) (progress * sbPosition.getMax()));
} else {
sbPosition.setSecondaryProgress(0);
}
}
@Override
public void onSleepTimerUpdate() {
AudioPlayerFragment.this.loadMediaInfo(false);
@ -367,6 +349,20 @@ public class AudioPlayerFragment extends Fragment implements
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
@SuppressWarnings("unused")
public void bufferUpdate(BufferUpdateEvent event) {
if (event.hasStarted()) {
progressIndicator.setVisibility(View.VISIBLE);
} else if (event.hasEnded()) {
progressIndicator.setVisibility(View.GONE);
} else if (controller != null && controller.isStreaming()) {
sbPosition.setSecondaryProgress((int) (event.getProgress() * sbPosition.getMax()));
} else {
sbPosition.setSecondaryProgress(0);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void updatePosition(PlaybackPositionEvent event) {
if (controller == null || txtvPosition == null || txtvLength == null || sbPosition == null) {

View File

@ -0,0 +1,35 @@
package de.danoeh.antennapod.core.event.playback;
public class BufferUpdateEvent {
private static final float PROGRESS_STARTED = -1;
private static final float PROGRESS_ENDED = -2;
final float progress;
private BufferUpdateEvent(float progress) {
this.progress = progress;
}
public static BufferUpdateEvent started() {
return new BufferUpdateEvent(PROGRESS_STARTED);
}
public static BufferUpdateEvent ended() {
return new BufferUpdateEvent(PROGRESS_ENDED);
}
public static BufferUpdateEvent progressUpdate(float progress) {
return new BufferUpdateEvent(progress);
}
public float getProgress() {
return progress;
}
public boolean hasStarted() {
return progress == PROGRESS_STARTED;
}
public boolean hasEnded() {
return progress == PROGRESS_ENDED;
}
}

View File

@ -15,6 +15,7 @@ import androidx.media.AudioAttributesCompat;
import androidx.media.AudioFocusRequestCompat;
import androidx.media.AudioManagerCompat;
import de.danoeh.antennapod.core.event.PlayerErrorEvent;
import de.danoeh.antennapod.core.event.playback.BufferUpdateEvent;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.playback.MediaPlayerError;
import org.antennapod.audio.MediaPlayer;
@ -1057,14 +1058,10 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
}
private final MediaPlayer.OnBufferingUpdateListener audioBufferingUpdateListener =
(mp, percent) -> genericOnBufferingUpdate(percent);
(mp, percent) -> EventBus.getDefault().post(BufferUpdateEvent.progressUpdate(0.01f * percent));
private final android.media.MediaPlayer.OnBufferingUpdateListener videoBufferingUpdateListener =
(mp, percent) -> genericOnBufferingUpdate(percent);
private void genericOnBufferingUpdate(int percent) {
callback.onBufferingUpdate(percent);
}
(mp, percent) -> EventBus.getDefault().post(BufferUpdateEvent.progressUpdate(0.01f * percent));
private final MediaPlayer.OnInfoListener audioInfoListener =
(mp, what, extra) -> genericInfoListener(what);
@ -1073,7 +1070,16 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
(mp, what, extra) -> genericInfoListener(what);
private boolean genericInfoListener(int what) {
return callback.onMediaPlayerInfo(what, 0);
switch (what) {
case android.media.MediaPlayer.MEDIA_INFO_BUFFERING_START:
EventBus.getDefault().post(BufferUpdateEvent.started());
return true;
case android.media.MediaPlayer.MEDIA_INFO_BUFFERING_END:
EventBus.getDefault().post(BufferUpdateEvent.ended());
return true;
default:
return callback.onMediaPlayerInfo(what, 0);
}
}
private final MediaPlayer.OnErrorListener audioErrorListener =

View File

@ -16,7 +16,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
@ -43,6 +42,7 @@ import androidx.core.app.NotificationManagerCompat;
import androidx.media.MediaBrowserServiceCompat;
import androidx.preference.PreferenceManager;
import de.danoeh.antennapod.core.event.playback.BufferUpdateEvent;
import de.danoeh.antennapod.core.event.playback.PlaybackServiceEvent;
import de.danoeh.antennapod.core.event.PlayerErrorEvent;
import org.greenrobot.eventbus.EventBus;
@ -162,8 +162,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
public static final int EXTRA_CODE_VIDEO = 2;
public static final int EXTRA_CODE_CAST = 3;
public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2;
/**
* Receivers of this intent should update their information about the curently playing media
*/
@ -172,8 +170,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
* The state of the sleeptimer changed.
*/
public static final int NOTIFICATION_TYPE_SLEEPTIMER_UPDATE = 4;
public static final int NOTIFICATION_TYPE_BUFFER_START = 5;
public static final int NOTIFICATION_TYPE_BUFFER_END = 6;
/**
* Set a max number of episodes to load for Android Auto, otherwise there could be performance issues
@ -900,11 +896,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
sendNotificationBroadcast(NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE, 0);
}
@Override
public void onBufferingUpdate(int percent) {
sendNotificationBroadcast(NOTIFICATION_TYPE_BUFFER_UPDATE, percent);
}
@Override
public void onMediaChanged(boolean reloadUI) {
Log.d(TAG, "reloadUI callback reached");
@ -916,26 +907,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
@Override
public boolean onMediaPlayerInfo(int code, @StringRes int resourceId) {
switch (code) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
sendNotificationBroadcast(NOTIFICATION_TYPE_BUFFER_START, 0);
return true;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
sendNotificationBroadcast(NOTIFICATION_TYPE_BUFFER_END, 0);
Playable playable = getPlayable();
if (getPlayable() instanceof FeedMedia
&& playable.getDuration() <= 0 && mediaPlayer.getDuration() > 0) {
// Playable is being streamed and does not have a duration specified in the feed
playable.setDuration(mediaPlayer.getDuration());
DBWriter.setFeedMedia((FeedMedia) playable);
updateNotificationAndMediaSession(playable);
}
return true;
default:
return flavorHelper.onMediaPlayerInfo(PlaybackService.this, code, resourceId);
}
return flavorHelper.onMediaPlayerInfo(PlaybackService.this, code, resourceId);
}
@Override
@ -993,6 +965,21 @@ public class PlaybackService extends MediaBrowserServiceCompat {
stateManager.stopService();
}
@Subscribe(threadMode = ThreadMode.MAIN)
@SuppressWarnings("unused")
public void bufferUpdate(BufferUpdateEvent event) {
if (event.hasEnded()) {
Playable playable = getPlayable();
if (getPlayable() instanceof FeedMedia
&& playable.getDuration() <= 0 && mediaPlayer.getDuration() > 0) {
// Playable is being streamed and does not have a duration specified in the feed
playable.setDuration(mediaPlayer.getDuration());
DBWriter.setFeedMedia((FeedMedia) playable);
updateNotificationAndMediaSession(playable);
}
}
}
private Playable getNextInQueue(final Playable currentMedia) {
if (!(currentMedia instanceof FeedMedia)) {
Log.d(TAG, "getNextInQueue(), but playable not an instance of FeedMedia, so not proceeding");

View File

@ -350,8 +350,6 @@ public abstract class PlaybackServiceMediaPlayer {
void playbackSpeedChanged(float s);
void onBufferingUpdate(int percent);
void onMediaChanged(boolean reloadUI);
boolean onMediaPlayerInfo(int code, @StringRes int resourceId);

View File

@ -206,10 +206,6 @@ public abstract class PlaybackController {
return;
}
switch (type) {
case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE:
float progress = ((float) code) / 100;
onBufferUpdate(progress);
break;
case PlaybackService.NOTIFICATION_TYPE_RELOAD:
if (playbackService == null && PlaybackService.isRunning) {
bindToService();
@ -223,12 +219,6 @@ public abstract class PlaybackController {
case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE:
onSleepTimerUpdate();
break;
case PlaybackService.NOTIFICATION_TYPE_BUFFER_START:
onBufferStart();
break;
case PlaybackService.NOTIFICATION_TYPE_BUFFER_END:
onBufferEnd();
break;
case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END:
onPlaybackEnd();
break;
@ -250,12 +240,6 @@ public abstract class PlaybackController {
*/
public void onReloadNotification(int code) {}
public void onBufferStart() {}
public void onBufferEnd() {}
public void onBufferUpdate(float progress) {}
public void onSleepTimerUpdate() {}
public void onPlaybackEnd() {}