Update playback position in queue

This commit is contained in:
ByteHamster 2019-09-25 17:07:24 +02:00
parent f12d3a8c53
commit 66cb923e9d
4 changed files with 45 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.adapter;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
@ -24,9 +25,11 @@ import android.widget.TextView;
import com.joanzapata.iconify.Iconify;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import org.apache.commons.lang3.ArrayUtils;
import java.lang.ref.WeakReference;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
@ -57,6 +60,7 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
private boolean locked;
private FeedItem selectedItem;
private ViewHolder currentlyPlayingItem = null;
private final int playingBackGroundColor;
private final int normalBackGroundColor;
@ -93,6 +97,18 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
});
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int pos, List<Object> payload) {
onBindViewHolder(holder, pos);
if (holder == currentlyPlayingItem && payload.size() == 1 && payload.get(0) instanceof PlaybackPositionEvent) {
PlaybackPositionEvent event = (PlaybackPositionEvent) payload.get(0);
holder.progressBar.setProgress((int) (100.0 * event.getPosition() / event.getDuration()));
holder.progressLeft.setText(Converter.getDurationStringLong(event.getPosition()));
holder.progressRight.setText(Converter.getDurationStringLong(event.getDuration()));
}
}
@Nullable
public FeedItem getSelectedItem() {
return selectedItem;
@ -108,6 +124,12 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
return itemAccess.getCount();
}
public void notifyCurrentlyPlayingItemChanged(PlaybackPositionEvent event) {
if (currentlyPlayingItem != null && currentlyPlayingItem.getAdapterPosition() != RecyclerView.NO_POSITION) {
notifyItemChanged(currentlyPlayingItem.getAdapterPosition(), event);
}
}
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener,
View.OnCreateContextMenuListener,
@ -287,6 +309,7 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
if(media.isCurrentlyPlaying()) {
container.setBackgroundColor(playingBackGroundColor);
currentlyPlayingItem = this;
} else {
container.setBackgroundColor(normalBackGroundColor);
}

View File

@ -26,6 +26,7 @@ import android.widget.TextView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
@ -213,6 +214,13 @@ public class QueueFragment extends Fragment {
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlaybackPositionEvent event) {
if (recyclerAdapter != null) {
recyclerAdapter.notifyCurrentlyPlayingItemChanged(event);
}
}
private void saveScrollPosition() {
int firstItem = layoutManager.findFirstVisibleItemPosition();
View firstItemView = layoutManager.findViewByPosition(firstItem);

View File

@ -1,8 +1,19 @@
package de.danoeh.antennapod.core.event;
public class PlaybackPositionEvent {
private final int position;
private final int duration;
public PlaybackPositionEvent() {
public PlaybackPositionEvent(int position, int duration) {
this.position = position;
this.duration = duration;
}
public int getPosition() {
return position;
}
public int getDuration() {
return duration;
}
}

View File

@ -12,7 +12,6 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
@ -70,7 +69,6 @@ import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.FeedSearcher;
import de.danoeh.antennapod.core.util.IntList;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.core.util.QueueAccess;
@ -1668,7 +1666,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
positionEventDistributorFuture.isDone()) {
Log.d(TAG, "Setting up position observer");
positionEventDistributorFuture = positionEventDistributorExecutor.scheduleWithFixedDelay(
() -> EventBus.getDefault().post(new PlaybackPositionEvent()), 1000, 1000, TimeUnit.MILLISECONDS);
() -> EventBus.getDefault().post(new PlaybackPositionEvent(getCurrentPosition(), getDuration())),
1000, 1000, TimeUnit.MILLISECONDS);
}
}