Skip to content

Commit 2bc52da

Browse files
committed
[功能优化](dev_4.0.6): 调整VLC播放进度获取方式
1 parent c69153a commit 2bc52da

File tree

2 files changed

+12
-35
lines changed

2 files changed

+12
-35
lines changed

player_component/src/main/java/com/xyoye/player/controller/base/BaseVideoController.kt

+1-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ abstract class BaseVideoController(
4949
override fun run() {
5050
val position = mControlWrapper.getCurrentPosition()
5151
handleProgressChanged(mControlWrapper.getDuration(), position)
52-
if (mControlWrapper.isPlaying()) {
53-
postDelayed(this, ((1000 - position % 1000) / mControlWrapper.getSpeed()).toLong())
54-
} else {
55-
mIsStartProgress = false
56-
postDelayed(this, 1000L)
57-
}
52+
postDelayed(this, 1000L)
5853
}
5954
}
6055

player_component/src/main/java/com/xyoye/player/kernel/impl/vlc/VlcVideoPlayer.kt

+11-29
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import org.videolan.libvlc.MediaPlayer
2222
import org.videolan.libvlc.interfaces.IMedia
2323
import org.videolan.libvlc.util.VLCVideoLayout
2424
import java.io.File
25-
import kotlin.math.abs
2625

2726
/**
2827
* Created by xyoye on 2021/4/12.
@@ -42,8 +41,7 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
4241
private lateinit var mMediaPlayer: MediaPlayer
4342
private lateinit var mMedia: Media
4443

45-
private val progress = Progress()
46-
private var lastTime = 0L
44+
private var mCurrentDuration = 0L
4745
private var seekable = true
4846
private var isBufferEnd = false
4947

@@ -77,18 +75,19 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
7775
mMedia = Media(libVlc, videoUri)
7876

7977
//是否开启硬件加速
80-
if (PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_DISABLE){
78+
if (PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_DISABLE) {
8179
mMedia.setHWDecoderEnabled(false, false)
8280
} else if (PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_DECODING ||
83-
PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_FULL){
81+
PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_FULL
82+
) {
8483
mMedia.setHWDecoderEnabled(true, true)
85-
if (PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_DECODING){
84+
if (PlayerInitializer.Player.vlcHWDecode == VLCHWDecode.HW_ACCELERATION_DECODING) {
8685
mMedia.addOption(":no-mediacodec-dr")
8786
mMedia.addOption(":no-omxil-dr")
8887
}
8988
} /* else automatic: use default options */
9089

91-
progress.duration = mMedia.duration
90+
mCurrentDuration = mMedia.duration
9291
mMediaPlayer.media = mMedia
9392
mMedia.release()
9493
}
@@ -111,8 +110,6 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
111110

112111
override fun stop() {
113112
playbackState = PlaybackStateCompat.STATE_STOPPED
114-
progress.release()
115-
lastTime = 0
116113

117114
mMediaPlayer.stop()
118115
}
@@ -178,11 +175,11 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
178175
}
179176

180177
override fun getCurrentPosition(): Long {
181-
return progress.position
178+
return mMediaPlayer.time
182179
}
183180

184181
override fun getDuration(): Long {
185-
return progress.duration
182+
return mCurrentDuration
186183
}
187184

188185
override fun getSpeed(): Float {
@@ -227,7 +224,8 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
227224
}
228225
}
229226
//打开中
230-
MediaPlayer.Event.Opening -> {}
227+
MediaPlayer.Event.Opening -> {
228+
}
231229
//播放中
232230
MediaPlayer.Event.Playing -> playbackState = PlaybackStateCompat.STATE_PLAYING
233231
//已暂停
@@ -242,15 +240,7 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
242240
}
243241
//时长输出
244242
MediaPlayer.Event.LengthChanged -> {
245-
progress.duration = it.lengthChanged
246-
}
247-
//进度改变
248-
MediaPlayer.Event.TimeChanged -> {
249-
val currentTime = it.timeChanged
250-
if (abs(currentTime - lastTime) > 950L) {
251-
progress.position = currentTime
252-
lastTime = currentTime
253-
}
243+
mCurrentDuration = it.lengthChanged
254244
}
255245
//视频输出
256246
MediaPlayer.Event.Vout -> {
@@ -292,12 +282,4 @@ class VlcVideoPlayer(private val mContext: Context) : AbstractVideoPlayer() {
292282

293283
private fun isVideoPlaying() =
294284
!mMediaPlayer.isReleased && mMediaPlayer.vlcVout.areViewsAttached()
295-
296-
class Progress(var position: Long = 0L, var duration: Long = 0L) {
297-
298-
fun release() {
299-
position = 0L
300-
duration = 0L
301-
}
302-
}
303285
}

0 commit comments

Comments
 (0)