-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct the playing status logic. (#367)
- Loading branch information
Showing
8 changed files
with
334 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
media-data/src/main/java/com/google/android/horologist/media/data/MediaItemPositionMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.media.data | ||
|
||
import androidx.media3.common.C | ||
import androidx.media3.common.Player | ||
import com.google.android.horologist.media.model.MediaItemPosition | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
/** | ||
* Maps a [Media3 player][Player] position into a [MediaItemPosition]. | ||
*/ | ||
public object MediaItemPositionMapper { | ||
public fun map(player: Player?): MediaItemPosition? { | ||
return if (player == null || player.currentMediaItem == null) { | ||
null | ||
} else if (player.duration == C.TIME_UNSET) { | ||
MediaItemPosition.UnknownDuration(player.currentPosition.milliseconds) | ||
} else { | ||
MediaItemPosition.create( | ||
current = player.currentPosition.milliseconds, | ||
// Ensure progress is max 100%, even given faulty media metadata | ||
duration = (player.duration.coerceAtLeast(player.currentPosition)).milliseconds | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
media-data/src/test/java/com/google/android/horologist/media/data/FakeStatePlayer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.media.data | ||
|
||
import androidx.media3.common.AdPlaybackState | ||
import androidx.media3.common.C | ||
import androidx.media3.common.FlagSet | ||
import androidx.media3.common.MediaItem | ||
import androidx.media3.common.Player | ||
import androidx.media3.common.Player.Listener | ||
import androidx.media3.common.Timeline | ||
import androidx.media3.test.utils.FakeTimeline | ||
import androidx.media3.test.utils.StubPlayer | ||
import com.google.common.collect.ImmutableList | ||
|
||
class FakeStatePlayer( | ||
var _currentPosition: Long = 0L, | ||
var _duration: Long = C.TIME_UNSET, | ||
var _playbackState: Int = STATE_IDLE, | ||
var _playWhenReady: Boolean = false, | ||
var _currentMediaItem: MediaItem? = null | ||
) : StubPlayer() { | ||
private val listeners = mutableListOf<Listener>() | ||
|
||
override fun addListener(listener: Player.Listener) { | ||
listeners.add(listener) | ||
super.addListener(listener) | ||
} | ||
|
||
override fun getCurrentPosition(): Long = _currentPosition | ||
|
||
override fun getDuration(): Long { | ||
return _duration | ||
} | ||
|
||
override fun getPlaybackState(): Int = _playbackState | ||
|
||
override fun setPlayWhenReady(playWhenReady: Boolean) { | ||
_playWhenReady = playWhenReady | ||
} | ||
|
||
override fun getPlayWhenReady(): Boolean = _playWhenReady | ||
|
||
override fun getCurrentTimeline(): Timeline { | ||
val currentMediaItem = _currentMediaItem | ||
if (currentMediaItem == null) { | ||
return FakeTimeline() | ||
} else { | ||
return FakeTimeline( | ||
FakeTimeline.TimelineWindowDefinition( | ||
/* periodCount= */ 1, | ||
/* id= */ 1, | ||
/* isSeekable= */ true, | ||
/* isDynamic= */ false, | ||
/* isLive= */ false, | ||
/* isPlaceholder= */ false, | ||
/* durationUs= */ 1000 * C.MICROS_PER_SECOND, | ||
/* defaultPositionUs= */ 2 * C.MICROS_PER_SECOND, | ||
/* windowOffsetInFirstPeriodUs= */ 123456789, | ||
ImmutableList.of(AdPlaybackState.NONE), | ||
currentMediaItem | ||
) | ||
) | ||
} | ||
} | ||
|
||
override fun getCurrentMediaItemIndex(): Int = 0 | ||
|
||
fun overridePosition( | ||
currentPosition: Long = 0L, | ||
duration: Long = C.TIME_UNSET, | ||
currentMediaItem: MediaItem? = null | ||
) { | ||
_currentPosition = currentPosition | ||
_duration = duration | ||
_currentMediaItem = currentMediaItem | ||
|
||
listeners.forEach { | ||
it.onEvents( | ||
this, | ||
Player.Events( | ||
FlagSet.Builder().addAll( | ||
EVENT_MEDIA_ITEM_TRANSITION, | ||
EVENT_MEDIA_ITEM_TRANSITION, | ||
EVENT_MEDIA_METADATA_CHANGED | ||
).build() | ||
) | ||
) | ||
} | ||
} | ||
|
||
fun overrideState( | ||
playbackState: Int = STATE_IDLE, | ||
playWhenReady: Boolean = false, | ||
reason: @Player.PlayWhenReadyChangeReason Int = PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST | ||
) { | ||
_playbackState = playbackState | ||
_playWhenReady = playWhenReady | ||
|
||
listeners.forEach { | ||
it.onEvents( | ||
this, | ||
Player.Events( | ||
FlagSet.Builder().addAll( | ||
EVENT_PLAYBACK_STATE_CHANGED, | ||
EVENT_PLAY_WHEN_READY_CHANGED, | ||
EVENT_MEDIA_ITEM_TRANSITION | ||
).build() | ||
) | ||
) | ||
it.onPlayWhenReadyChanged(_playWhenReady, reason) | ||
it.onPlaybackStateChanged(_playbackState) | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ata/src/test/java/com/google/android/horologist/media/data/MediaItemPositionMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2022 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.media.data | ||
|
||
import androidx.media3.common.C | ||
import com.google.android.horologist.media.model.MediaItemPosition | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import kotlin.time.Duration.Companion.milliseconds | ||
|
||
class MediaItemPositionMapperTest { | ||
val fakeStatePlayer = FakeStatePlayer() | ||
|
||
@Test | ||
fun `check position calculations null`() { | ||
val position = MediaItemPositionMapper.map(null) | ||
assertThat(position).isNull() | ||
} | ||
|
||
@Test | ||
fun `check position calculations unknown duration`() { | ||
fakeStatePlayer.overridePosition( | ||
currentPosition = 10L, | ||
duration = C.TIME_UNSET | ||
) | ||
val position = | ||
MediaItemPositionMapper.map(fakeStatePlayer) as MediaItemPosition.UnknownDuration | ||
assertThat(position.current).isEqualTo(10.milliseconds) | ||
} | ||
|
||
@Test | ||
fun `check position calculations past end`() { | ||
fakeStatePlayer.overridePosition( | ||
currentPosition = 100L, | ||
duration = 99L | ||
) | ||
val position = | ||
MediaItemPositionMapper.map(fakeStatePlayer) as MediaItemPosition.KnownDuration | ||
assertThat(position.current).isEqualTo(100.milliseconds) | ||
assertThat(position.duration).isEqualTo(100.milliseconds) | ||
} | ||
|
||
@Test | ||
fun `check position calculations during`() { | ||
fakeStatePlayer.overridePosition( | ||
currentPosition = 100L, | ||
duration = 1000L | ||
) | ||
val position = | ||
MediaItemPositionMapper.map(fakeStatePlayer) as MediaItemPosition.KnownDuration | ||
assertThat(position.current).isEqualTo(100.milliseconds) | ||
assertThat(position.duration).isEqualTo(1000.milliseconds) | ||
} | ||
} |
Oops, something went wrong.