@roitium/expo-orpheus 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ import androidx.media3.common.C
9
9
  import androidx.media3.common.MediaItem
10
10
  import androidx.media3.common.PlaybackException
11
11
  import androidx.media3.common.Player
12
+ import androidx.media3.common.Timeline
12
13
  import androidx.media3.session.MediaController
13
14
  import androidx.media3.session.SessionCommand
14
15
  import androidx.media3.session.SessionResult
@@ -34,7 +35,7 @@ class ExpoOrpheusModule : Module() {
34
35
  // 记录上一首歌曲的 ID,用于在切歌时发送给 JS
35
36
  private var lastMediaId: String? = null
36
37
 
37
- private var currentTrackDuration: Long = 0L
38
+ private val durationCache = mutableMapOf<String, Long>()
38
39
 
39
40
  val gson = Gson()
40
41
 
@@ -346,25 +347,38 @@ class ExpoOrpheusModule : Module() {
346
347
  )
347
348
 
348
349
  lastMediaId = newId
349
- currentTrackDuration = 0L
350
350
  saveCurrentPosition()
351
351
  }
352
352
 
353
+ override fun onTimelineChanged(timeline: Timeline, reason: Int) {
354
+ val player = controller ?: return
355
+ val currentItem = player.currentMediaItem ?: return
356
+ val mediaId = currentItem.mediaId
357
+
358
+ val duration = player.duration
359
+
360
+ if (duration != C.TIME_UNSET && duration > 0) {
361
+ durationCache[mediaId] = duration
362
+ }
363
+ }
364
+
353
365
  override fun onPositionDiscontinuity(
354
366
  oldPosition: Player.PositionInfo,
355
367
  newPosition: Player.PositionInfo,
356
368
  reason: Int
357
369
  ) {
358
- Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT
359
370
  if (oldPosition.mediaItemIndex != newPosition.mediaItemIndex) {
360
371
  val lastMediaItem =
361
372
  controller?.getMediaItemAt(oldPosition.mediaItemIndex) ?: return
362
373
 
374
+ // onPositionDiscontinuity 会被连续调用两次,且两次调用参数相同,很奇怪的行为,所以采用这种方式过滤.没值就直接返回,不发事件。
375
+ val duration = durationCache.remove(lastMediaItem.mediaId) ?: return
376
+
363
377
  sendEvent(
364
378
  "onTrackFinished", mapOf(
365
379
  "trackId" to lastMediaItem.mediaId,
366
380
  "finalPosition" to oldPosition.positionMs / 1000.0,
367
- "duration" to currentTrackDuration / 1000.0,
381
+ "duration" to duration / 1000.0,
368
382
  )
369
383
  )
370
384
  }
@@ -381,11 +395,6 @@ class ExpoOrpheusModule : Module() {
381
395
  )
382
396
  )
383
397
 
384
- if (state == Player.STATE_READY) {
385
- val d = controller?.duration
386
- if (d != C.TIME_UNSET && d != null) currentTrackDuration = d
387
- }
388
-
389
398
  updateProgressRunnerState()
390
399
  }
391
400
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@roitium/expo-orpheus",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A player for bbplayer",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",