Commit 3d776270 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Set playback progress a LiveData

parent e947b9b8
......@@ -6,10 +6,7 @@
<import type="org.videolan.medialibrary.Tools" />
<variable
name="progress"
type="android.databinding.ObservableInt" />
<variable
name="length"
type="android.databinding.ObservableLong" />
type="android.arch.lifecycle.LiveData&lt;org.videolan.vlc.media.Progress>" />
<variable
name="player"
type="org.videolan.vlc.gui.video.VideoPlayerActivity" />
......@@ -39,8 +36,8 @@
android:thumb="@drawable/seekbar_thumb"
android:splitTrack="false"
android:focusable="true"
android:progress="@{progress}"
vlc:mediamax="@{length}"/>
android:progress="@{(int)progress.time}"
vlc:mediamax="@{(int)progress.length}"/>
<TextView
android:id="@+id/player_overlay_time"
......@@ -56,7 +53,7 @@
android:gravity="left|start"
android:textAppearance="@style/TextAppearance.AppCompat.SearchResult.Title"
android:textColor="@color/orange500"
android:text="@{Tools.millisToString(progress)}"
android:text="@{Tools.millisToString(progress.time)}"
android:textSize="16sp"
android:onClick="@{(v) -> player.toggleTimeDisplay()}" />
......@@ -69,9 +66,8 @@
android:layout_alignRight="@+id/player_overlay_seekbar"
android:layout_marginRight="@dimen/time_margin_sides"
android:gravity="right|end"
android:text="--:--"
vlc:time="@{progress}"
vlc:length="@{length}"
vlc:time="@{progress.time}"
vlc:length="@{progress.length}"
android:textAppearance="@style/TextAppearance.AppCompat.SearchResult.Title"
android:textSize="16sp"
android:layout_alignParentEnd="true"
......
......@@ -326,7 +326,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
val length: Long
@MainThread
get() = playlistManager.player.length
get() = playlistManager.player.getLength()
val lastStats: Media.Stats?
get() = playlistManager.player.previousMediaStats
......@@ -742,6 +742,7 @@ class PlaybackService : MediaBrowserServiceCompat() {
val media = playlistManager.getCurrentMedia() ?: return
if (!this::mediaSession.isInitialized) initMediaSession()
val ctx = this
val length = length
val bob = withContext(CommonPool) {
val title = media.nowPlaying ?: media.title
val coverOnLockscreen = settings.getBoolean("lockscreen_cover", true)
......
......@@ -38,8 +38,6 @@ import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableInt;
import android.databinding.ObservableLong;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.media.AudioManager;
......@@ -489,16 +487,10 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
initUI();
setPlaybackParameters();
mForcedTime = mLastTime = -1;
updateTimeValues();
enableSubs();
}
}
private void updateTimeValues() {
mProgress.set((int) getTime());
mMediaLength.set(mService.getLength());
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onPause() {
......@@ -1522,12 +1514,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
case MediaPlayer.Event.EncounteredError:
encounteredError();
break;
case MediaPlayer.Event.TimeChanged:
mProgress.set((int) event.getTimeChanged());
break;
case MediaPlayer.Event.LengthChanged:
mMediaLength.set(event.getLengthChanged());
break;
case MediaPlayer.Event.Vout:
updateNavStatus();
if (mMenuIdx == -1)
......@@ -1645,7 +1631,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
updateOverlayPausePlay();
updateNavStatus();
final MediaWrapper mw = mService.getCurrentMediaWrapper();
mMediaLength.set(mService.getLength());
if (!mw.hasFlag(MediaWrapper.MEDIA_PAUSED))
mHandler.sendEmptyMessageDelayed(FADE_OUT, OVERLAY_TIMEOUT);
else {
......@@ -2377,7 +2362,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
mForcedTime = position;
mLastTime = mService.getTime();
mService.seek(position, length);
mProgress.set((int) position);
}
void seekDelta(int delta) {
......@@ -2500,8 +2484,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
}
private PlayerHudBinding mHudBinding;
private ObservableInt mProgress = new ObservableInt(0);
private ObservableLong mMediaLength = new ObservableLong(0L);
private boolean mSeekButtons, mHasPlaylist;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void initOverlay() {
......@@ -2511,9 +2493,8 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
vsc.inflate();
mHudBinding = DataBindingUtil.bind(findViewById(R.id.progress_overlay));
mHudBinding.setPlayer(this);
updateTimeValues();
mHudBinding.setProgress(mProgress);
mHudBinding.setLength(mMediaLength);
mHudBinding.setProgress(mService.getPlaylistManager().getPlayer().getProgress());
mHudBinding.setLifecycleOwner(this);
final RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)mHudBinding.progressOverlay.getLayoutParams();
if (AndroidDevices.isPhone || !AndroidDevices.hasNavBar)
......@@ -3246,7 +3227,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
};
@BindingAdapter({"length", "time"})
public static void setPlaybackTime(TextView view, long length, int time) {
public static void setPlaybackTime(TextView view, long length, long time) {
view.setText(sDisplayRemainingTime && length > 0
? "-" + '\u00A0' + Tools.millisToString(length - time)
: Tools.millisToString(length));
......
......@@ -17,6 +17,7 @@ import org.videolan.vlc.VLCApplication
import org.videolan.vlc.gui.preferences.PreferencesActivity
import org.videolan.vlc.util.VLCInstance
import org.videolan.vlc.util.VLCOptions
import kotlin.math.abs
@Suppress("EXPERIMENTAL_FEATURE_WARNING")
class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
......@@ -24,7 +25,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
// private val exceptionHandler by lazy(LazyThreadSafetyMode.NONE) { CoroutineExceptionHandler { _, _ -> onPlayerError() } }
private val playerContext by lazy(LazyThreadSafetyMode.NONE) { newSingleThreadContext("vlc-player") }
private val settings by lazy(LazyThreadSafetyMode.NONE) { VLCApplication.getSettings() }
val currentTime by lazy(LazyThreadSafetyMode.NONE) { MutableLiveData<Long>() }
val progress by lazy(LazyThreadSafetyMode.NONE) { MutableLiveData<Progress>().apply { value = Progress() } }
private var mediaplayer = newMediaPlayer()
var switchToVideo = false
......@@ -36,8 +37,6 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
private set
@Volatile var hasRenderer = false
private set
@Volatile var length = 0L
private set
fun getVout(): IVLCVout? = mediaplayer.vlcVout
......@@ -72,9 +71,8 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
mediaplayerEventListener = listener
seekable = true
pausable = true
currentTime.value = 0L
lastTime = 0L
length = media.duration
updateProgress(0L, media.duration)
mediaplayer.setEventListener(null)
mediaplayer.media = media.apply { if (hasRenderer) parse() }
mediaplayer.setEventListener(this@PlayerController)
......@@ -92,7 +90,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
release(mp)
}
fun seek(position: Long, length: Double = this.length.toDouble()) {
fun seek(position: Long, length: Double = getLength().toDouble()) {
if (length > 0.0) setPosition((position / length).toFloat())
else setTime(position)
}
......@@ -213,7 +211,9 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
switchToVideo = false
}
fun getCurrentTime() = currentTime.value ?: 0L
fun getCurrentTime() = progress.value?.time ?: 0L
fun getLength() = progress.value?.length ?: 0L
fun setRate(rate: Float, save: Boolean) {
mediaplayer.rate = rate
......@@ -284,11 +284,11 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
MediaPlayer.Event.EncounteredError -> setPlaybackStopped()
MediaPlayer.Event.PausableChanged -> pausable = event.pausable
MediaPlayer.Event.SeekableChanged -> seekable = event.seekable
MediaPlayer.Event.LengthChanged -> length = event.lengthChanged
MediaPlayer.Event.LengthChanged -> updateProgress(newLength = event.lengthChanged)
MediaPlayer.Event.TimeChanged -> {
val time = event.timeChanged
if (time - lastTime > 950L) {
currentTime.value = time
if (abs(time - lastTime) > 950L) {
updateProgress(newTime = time)
lastTime = time
}
}
......@@ -297,15 +297,18 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
}
}
private fun updateProgress(newTime: Long = progress.value?.time ?: 0L, newLength: Long = progress.value?.length ?: 0L) {
progress.value = progress.value?.apply { time = newTime; length = newLength }
}
override fun onEvent(event: MediaPlayer.Event?) {
if (event != null) eventActor.offer(event)
}
private fun setPlaybackStopped() {
playbackState = PlaybackStateCompat.STATE_STOPPED
currentTime.value = 0L
updateProgress(0L, 0L)
lastTime = 0L
length = 0L
}
// private fun onPlayerError() {
......@@ -316,6 +319,8 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
// }
}
class Progress(var time: Long = 0L, var length: Long = 0L)
internal interface MediaPLayerEventListener {
suspend fun onEvent(event: MediaPlayer.Event)
}
\ No newline at end of file
......@@ -390,7 +390,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
if (media.type == MediaWrapper.TYPE_VIDEO || canSwitchToVideo || media.isPodcast) {
//Save progress
val time = player.getCurrentTime()
val length = player.length
val length = player.getLength()
var progress = time / length.toFloat()
if (progress > 0.95f || length - time < 10000) {
//increase seen counter if more than 95% of the media have been seen
......
......@@ -20,8 +20,8 @@ class PlaylistModel(private val service: PlaybackService) : ViewModel(), Playbac
val dataset = LiveDataset<MediaWrapper>()
val progress by lazy(LazyThreadSafetyMode.NONE) {
MediatorLiveData<PlaybackProgress>().apply {
addSource(service.playlistManager.player.currentTime, {
value = PlaybackProgress(it ?: 0L, service.length)
addSource(service.playlistManager.player.progress, {
value = PlaybackProgress(it?.time ?: 0L, it?.length ?: 0L)
})
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment