Skip to content

Commit 99fec7f

Browse files
committed
add finish callback and loopmode option
1 parent 12dafce commit 99fec7f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

extra-sources/src/main/java/com/pedro/extrasources/Media3AudioSource.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import com.pedro.extrasources.extractor.Media3Extractor
1919
class Media3AudioSource(
2020
private val context: Context,
2121
private val path: Uri,
22-
private val speed: Float = 1f
22+
private val speed: Float = 1f,
23+
private val loopMode: Boolean = true,
24+
private val onFinish: (isLoop: Boolean) -> Unit = {}
2325
): AudioSource() {
2426

2527
private var player: ExoPlayer? = null
@@ -50,8 +52,13 @@ class Media3AudioSource(
5052
exoPlayer.setMediaItem(mediaItem)
5153
exoPlayer.playbackParameters = PlaybackParameters(speed)
5254
exoPlayer.prepare()
53-
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
55+
if (loopMode) exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
5456
}
57+
player?.addListener(object : Player.Listener {
58+
override fun onPlaybackStateChanged(state: Int) {
59+
if (state == Player.STATE_ENDED) onFinish(loopMode)
60+
}
61+
})
5562
return true
5663
}
5764

extra-sources/src/main/java/com/pedro/extrasources/Media3VideoSource.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import com.pedro.extrasources.extractor.Media3Extractor
2020
class Media3VideoSource(
2121
private val context: Context,
2222
private val path: Uri,
23-
private val speed: Float = 1f
23+
private val speed: Float = 1f,
24+
private val loopMode: Boolean = true,
25+
private val onFinish: (isLoop: Boolean) -> Unit = {}
2426
): VideoSource() {
2527

2628
private var player: ExoPlayer? = null
@@ -41,8 +43,13 @@ class Media3VideoSource(
4143
exoPlayer.setMediaItem(mediaItem)
4244
exoPlayer.playbackParameters = PlaybackParameters(speed)
4345
exoPlayer.prepare()
44-
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
46+
if (loopMode) exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
4547
}
48+
player?.addListener(object : Player.Listener {
49+
override fun onPlaybackStateChanged(state: Int) {
50+
if (state == Player.STATE_ENDED) onFinish(loopMode)
51+
}
52+
})
4653
return true
4754
}
4855

0 commit comments

Comments
 (0)