@@ -15,7 +15,6 @@ import io.element.android.libraries.di.RoomScope
1515import io.element.android.libraries.di.SingleIn
1616import io.element.android.libraries.mediaplayer.api.MediaPlayer
1717import kotlinx.coroutines.CoroutineScope
18- import kotlinx.coroutines.Dispatchers
1918import kotlinx.coroutines.FlowPreview
2019import kotlinx.coroutines.Job
2120import kotlinx.coroutines.delay
@@ -36,6 +35,7 @@ import kotlin.time.Duration.Companion.seconds
3635@SingleIn(RoomScope ::class )
3736class 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