Skip to content

Commit c48e4f4

Browse files
committed
Add test on DefaultMediaPlayer.
1 parent e85aa50 commit c48e4f4

File tree

3 files changed

+449
-9
lines changed

3 files changed

+449
-9
lines changed

libraries/mediaplayer/impl/src/main/kotlin/io/element/android/libraries/mediaplayer/impl/DefaultMediaPlayer.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import io.element.android.libraries.di.RoomScope
1515
import io.element.android.libraries.di.SingleIn
1616
import io.element.android.libraries.mediaplayer.api.MediaPlayer
1717
import kotlinx.coroutines.CoroutineScope
18-
import kotlinx.coroutines.Dispatchers
1918
import kotlinx.coroutines.FlowPreview
2019
import kotlinx.coroutines.Job
2120
import kotlinx.coroutines.delay
@@ -36,6 +35,7 @@ import kotlin.time.Duration.Companion.seconds
3635
@SingleIn(RoomScope::class)
3736
class DefaultMediaPlayer @Inject constructor(
3837
private val player: SimplePlayer,
38+
private val coroutineScope: CoroutineScope,
3939
) : MediaPlayer {
4040
private val listener = object : SimplePlayer.Listener {
4141
override fun onIsPlayingChanged(isPlaying: Boolean) {
@@ -47,7 +47,7 @@ class DefaultMediaPlayer @Inject constructor(
4747
)
4848
}
4949
if (isPlaying) {
50-
job = scope.launch { updateCurrentPosition() }
50+
job = coroutineScope.launch { updateCurrentPosition() }
5151
} else {
5252
job?.cancel()
5353
}
@@ -79,7 +79,6 @@ class DefaultMediaPlayer @Inject constructor(
7979
player.addListener(listener)
8080
}
8181

82-
private val scope = CoroutineScope(Job() + Dispatchers.Main)
8382
private var job: Job? = null
8483

8584
private val _state = MutableStateFlow(
@@ -102,7 +101,8 @@ class DefaultMediaPlayer @Inject constructor(
102101
mimeType: String,
103102
startPositionMs: Long,
104103
): MediaPlayer.State {
105-
player.pause() // Must pause here otherwise if the player was playing it would keep on playing the new media item.
104+
// Must pause here otherwise if the player was playing it would keep on playing the new media item.
105+
player.pause()
106106
player.clearMediaItems()
107107
player.setMediaItem(
108108
MediaItem.Builder()
@@ -129,11 +129,9 @@ class DefaultMediaPlayer @Inject constructor(
129129
player.getCurrentMediaItem()?.let {
130130
player.setMediaItem(it, 0)
131131
player.prepare()
132-
player.play()
133132
}
134-
} else {
135-
player.play()
136133
}
134+
player.play()
137135
}
138136

139137
override fun pause() {

0 commit comments

Comments
 (0)