Skip to content

Commit 15e6bf9

Browse files
authored
Add a proper position to the AudioItemTransition event (doublesymmetry#1643)
* Use proper old position in event # Conflicts: # android/build.gradle # android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt * Finish * Fix compile * Fix removed case * Refactor
1 parent 67e929c commit 15e6bf9

File tree

7 files changed

+51
-45
lines changed

7 files changed

+51
-45
lines changed

android/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ android {
2626
minSdkVersion getExtOrIntegerDefault('minSdkVersion') // RN's minimum version
2727
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
2828

29-
versionCode 1
30-
versionName '1.0'
29+
versionCode 300
30+
versionName '3.0'
3131

3232
consumerProguardFiles 'proguard-rules.txt'
3333
}
@@ -51,8 +51,8 @@ repositories {
5151
}
5252

5353
dependencies {
54-
implementation "com.github.DoubleSymmetry:KotlinAudio:v0.1.35"
55-
// implementation "com.github.doublesymmetry:kotlin-audio:0.1.36"
54+
implementation 'com.github.DoubleSymmetry:KotlinAudio:v1.0'
55+
// implementation "com.github.doublesymmetry:kotlin-audio:1.0"
5656

5757
//noinspection GradleDynamicVersion
5858
implementation "com.facebook.react:react-native:+"

android/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RNTP_kotlinVersion=1.5.31
1+
RNTP_kotlinVersion=1.7.10
22
RNTP_minSdkVersion=21
33
RNTP_targetSdkVersion=31
44
RNTP_compileSdkVersion=31
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.doublesymmetry.trackplayer.extensions
2+
3+
class NumberExt {
4+
companion object {
5+
fun Number.toSeconds(): Double {
6+
return this.toDouble() / 1000
7+
}
8+
9+
fun Number.toMilliseconds(): Long {
10+
return (this.toDouble() * 1000).toLong()
11+
}
12+
}
13+
}

android/src/main/java/com/doublesymmetry/trackplayer/model/TrackMetadata.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.net.Uri
55
import android.os.Bundle
66
import android.support.v4.media.MediaMetadataCompat
77
import android.support.v4.media.RatingCompat
8+
import com.doublesymmetry.trackplayer.extensions.NumberExt.Companion.toMilliseconds
89
import com.doublesymmetry.trackplayer.utils.Utils
910

1011
abstract class TrackMetadata {
@@ -23,7 +24,7 @@ abstract class TrackMetadata {
2324
album = bundle.getString("album")
2425
date = bundle.getString("date")
2526
genre = bundle.getString("genre")
26-
duration = Utils.toMillis(bundle.getDouble("duration", 0.0))
27+
duration = (bundle.getDouble("duration", 0.0)).toMilliseconds()
2728
rating = Utils.getRating(bundle, "rating", ratingType)
2829
}
2930

android/src/main/java/com/doublesymmetry/trackplayer/module/MusicModule.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.core.content.ContextCompat
88
import androidx.localbroadcastmanager.content.LocalBroadcastManager
99
import com.doublesymmetry.kotlinaudio.models.Capability
1010
import com.doublesymmetry.kotlinaudio.models.RepeatMode
11+
import com.doublesymmetry.trackplayer.extensions.NumberExt.Companion.toMilliseconds
1112
import com.doublesymmetry.trackplayer.extensions.asLibState
1213
import com.doublesymmetry.trackplayer.model.State
1314
import com.doublesymmetry.trackplayer.model.Track
@@ -139,14 +140,10 @@ class MusicModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaM
139140

140141
// Validate buffer keys.
141142
val bundledData = Arguments.toBundle(data)
142-
val minBuffer = bundledData?.getDouble(MusicService.MIN_BUFFER_KEY)
143-
?.let { Utils.toMillis(it).toInt() } ?: DEFAULT_MIN_BUFFER_MS
144-
val maxBuffer = bundledData?.getDouble(MusicService.MAX_BUFFER_KEY)
145-
?.let { Utils.toMillis(it).toInt() } ?: DEFAULT_MAX_BUFFER_MS
146-
val playBuffer = bundledData?.getDouble(MusicService.PLAY_BUFFER_KEY)
147-
?.let { Utils.toMillis(it).toInt() } ?: DEFAULT_BUFFER_FOR_PLAYBACK_MS
148-
val backBuffer = bundledData?.getDouble(MusicService.BACK_BUFFER_KEY)
149-
?.let { Utils.toMillis(it).toInt() } ?: DEFAULT_BACK_BUFFER_DURATION_MS
143+
val minBuffer = bundledData?.getDouble(MusicService.MIN_BUFFER_KEY)?.toMilliseconds()?.toInt() ?: DEFAULT_MIN_BUFFER_MS
144+
val maxBuffer = bundledData?.getDouble(MusicService.MAX_BUFFER_KEY)?.toMilliseconds()?.toInt() ?: DEFAULT_MAX_BUFFER_MS
145+
val playBuffer = bundledData?.getDouble(MusicService.PLAY_BUFFER_KEY)?.toMilliseconds()?.toInt() ?: DEFAULT_BUFFER_FOR_PLAYBACK_MS
146+
val backBuffer = bundledData?.getDouble(MusicService.BACK_BUFFER_KEY)?.toMilliseconds()?.toInt() ?: DEFAULT_BACK_BUFFER_DURATION_MS
150147

151148
if (playBuffer < 0) {
152149
promise.reject(

android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import com.doublesymmetry.kotlinaudio.models.*
1313
import com.doublesymmetry.kotlinaudio.models.NotificationButton.*
1414
import com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer
1515
import com.doublesymmetry.trackplayer.R
16+
import com.doublesymmetry.trackplayer.extensions.NumberExt.Companion.toMilliseconds
17+
import com.doublesymmetry.trackplayer.extensions.NumberExt.Companion.toSeconds
1618
import com.doublesymmetry.trackplayer.extensions.asLibState
1719
import com.doublesymmetry.trackplayer.model.Track
1820
import com.doublesymmetry.trackplayer.model.TrackAudioItem
@@ -65,10 +67,10 @@ class MusicService : HeadlessJsTaskService() {
6567
fun setupPlayer(playerOptions: Bundle?) {
6668
// fun setupPlayer(playerOptions: Bundle?) {
6769
val bufferOptions = BufferConfig(
68-
playerOptions?.getDouble(MIN_BUFFER_KEY)?.let { Utils.toMillis(it).toInt() },
69-
playerOptions?.getDouble(MAX_BUFFER_KEY)?.let { Utils.toMillis(it).toInt() },
70-
playerOptions?.getDouble(PLAY_BUFFER_KEY)?.let { Utils.toMillis(it).toInt() },
71-
playerOptions?.getDouble(BACK_BUFFER_KEY)?.let { Utils.toMillis(it).toInt() },
70+
playerOptions?.getDouble(MIN_BUFFER_KEY)?.toMilliseconds()?.toInt(),
71+
playerOptions?.getDouble(MAX_BUFFER_KEY)?.toMilliseconds()?.toInt(),
72+
playerOptions?.getDouble(PLAY_BUFFER_KEY)?.toMilliseconds()?.toInt(),
73+
playerOptions?.getDouble(BACK_BUFFER_KEY)?.toMilliseconds()?.toInt(),
7274
)
7375

7476
val cacheOptions = CacheConfig(
@@ -170,9 +172,9 @@ class MusicService : HeadlessJsTaskService() {
170172
private suspend fun progressUpdateEvent(): Bundle {
171173
return withContext(Dispatchers.Main) {
172174
Bundle().apply {
173-
putDouble(POSITION_KEY, player.position.toDouble() / 1000)
174-
putDouble(DURATION_KEY, player.duration.toDouble() / 1000)
175-
putDouble(BUFFERED_POSITION_KEY, player.bufferedPosition.toDouble() / 1000)
175+
putDouble(POSITION_KEY, player.position.toSeconds())
176+
putDouble(DURATION_KEY, player.duration.toSeconds())
177+
putDouble(BUFFERED_POSITION_KEY, player.bufferedPosition.toSeconds())
176178
putInt(TRACK_KEY, player.currentIndex)
177179
}
178180
}
@@ -260,7 +262,7 @@ class MusicService : HeadlessJsTaskService() {
260262

261263
@MainThread
262264
fun seekTo(seconds: Float) {
263-
player.seek((seconds * 1000).toLong(), TimeUnit.MILLISECONDS)
265+
player.seek((seconds.toMilliseconds()), TimeUnit.MILLISECONDS)
264266
}
265267

266268
@MainThread
@@ -291,13 +293,13 @@ class MusicService : HeadlessJsTaskService() {
291293
}
292294

293295
@MainThread
294-
fun getDurationInSeconds(): Double = player.duration.toDouble() / 1000
296+
fun getDurationInSeconds(): Double = player.duration.toSeconds()
295297

296298
@MainThread
297-
fun getPositionInSeconds(): Double = player.position.toDouble() / 1000
299+
fun getPositionInSeconds(): Double = player.position.toSeconds()
298300

299301
@MainThread
300-
fun getBufferedPositionInSeconds(): Double = player.bufferedPosition.toDouble() / 1000
302+
fun getBufferedPositionInSeconds(): Double = player.bufferedPosition.toSeconds()
301303

302304
@MainThread
303305
fun updateMetadataForTrack(index: Int, track: Track) {
@@ -323,28 +325,29 @@ class MusicService : HeadlessJsTaskService() {
323325
emit(MusicEvents.PLAYBACK_STATE, bundle)
324326

325327
if (it == AudioPlayerState.ENDED && player.nextItem == null) {
326-
val endBundle = Bundle()
327-
endBundle.putInt(TRACK_KEY, player.currentIndex)
328-
endBundle.putDouble(POSITION_KEY, Utils.toSeconds(player.position))
328+
Bundle().apply {
329+
putInt(TRACK_KEY, player.currentIndex)
330+
putDouble(POSITION_KEY, player.position.toSeconds())
329331

330-
emit(MusicEvents.PLAYBACK_QUEUE_ENDED, endBundle)
331-
emit(MusicEvents.PLAYBACK_TRACK_CHANGED, endBundle)
332+
emit(MusicEvents.PLAYBACK_QUEUE_ENDED, this)
333+
emit(MusicEvents.PLAYBACK_TRACK_CHANGED, this)
334+
}
332335
}
333336
}
334337
}
335338

336339
scope.launch {
337340
event.audioItemTransition.collect {
338341
Bundle().apply {
339-
putDouble(POSITION_KEY, 0.0)
342+
putDouble(POSITION_KEY, (it?.oldPosition ?: 0).toSeconds())
340343
putInt(NEXT_TRACK_KEY, player.currentIndex)
341344

342345
// correctly set the previous index on the event payload
343346
var previousIndex: Int? = null
344-
if (it == AudioItemTransitionReason.REPEAT) {
347+
if (it is AudioItemTransitionReason.REPEAT) {
345348
previousIndex = player.currentIndex
346349
} else if (player.previousItem != null) {
347-
previousIndex = player?.previousIndex
350+
previousIndex = player.previousIndex
348351
}
349352

350353
if (previousIndex != null) {
@@ -377,8 +380,8 @@ class MusicService : HeadlessJsTaskService() {
377380
is FORWARD -> {
378381
Bundle().apply {
379382
val interval = latestOptions?.getDouble(
380-
FORWARD_JUMP_INTERVAL_KEY,
381-
DEFAULT_JUMP_INTERVAL,
383+
FORWARD_JUMP_INTERVAL_KEY,
384+
DEFAULT_JUMP_INTERVAL,
382385
) ?: DEFAULT_JUMP_INTERVAL
383386
putInt("interval", interval.toInt())
384387
emit(MusicEvents.BUTTON_JUMP_FORWARD, this)
@@ -387,8 +390,8 @@ class MusicService : HeadlessJsTaskService() {
387390
is BACKWARD -> {
388391
Bundle().apply {
389392
val interval = latestOptions?.getDouble(
390-
BACKWARD_JUMP_INTERVAL_KEY,
391-
DEFAULT_JUMP_INTERVAL,
393+
BACKWARD_JUMP_INTERVAL_KEY,
394+
DEFAULT_JUMP_INTERVAL,
392395
) ?: DEFAULT_JUMP_INTERVAL
393396
putInt("interval", interval.toInt())
394397
emit(MusicEvents.BUTTON_JUMP_BACKWARD, this)

android/src/main/java/com/doublesymmetry/trackplayer/utils/Utils.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper
1111
* @author Milen Pivchev @mpivchev
1212
*/
1313
object Utils {
14-
fun toMillis(seconds: Double): Long {
15-
return (seconds * 1000).toLong()
16-
}
17-
18-
fun toSeconds(millis: Long): Double {
19-
return millis / 1000.0
20-
}
21-
2214
fun getUri(context: Context, data: Bundle?, key: String?): Uri? {
2315
if (!data!!.containsKey(key)) return null
2416
val obj = data[key]

0 commit comments

Comments
 (0)