@javascriptcommon/react-native-track-player 1.2.23 → 1.2.24

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.
@@ -111,7 +111,7 @@ public abstract class HeadlessJsMediaService extends MediaBrowserServiceCompat i
111
111
  */
112
112
  protected void startTask(final HeadlessJsTaskConfig taskConfig) {
113
113
  UiThreadUtil.assertOnUiThread();
114
- // acquireWakeLockNow(this);
114
+ acquireWakeLockNow(this);
115
115
  ReactContext reactContext = getReactContext();
116
116
  if (reactContext == null) {
117
117
  createReactContextAndScheduleTask(taskConfig);
@@ -611,13 +611,28 @@ class MusicModule(reactContext: ReactApplicationContext?) :
611
611
  IntentFilter(EVENT_INTENT)
612
612
  )
613
613
 
614
- Intent(reactApplicationContext, MusicService::class.java).also { intent ->
615
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
616
- reactApplicationContext.startForegroundService(intent)
617
- } else {
618
- reactApplicationContext.startService(intent)
614
+ val musicModule = this
615
+ scope.launch {
616
+ var retries = 0
617
+ while (true) {
618
+ try {
619
+ Intent(reactApplicationContext, MusicService::class.java).also { intent ->
620
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
621
+ reactApplicationContext.startForegroundService(intent)
622
+ } else {
623
+ reactApplicationContext.startService(intent)
624
+ }
625
+ reactApplicationContext.bindService(intent, musicModule, Context.BIND_AUTO_CREATE)
626
+ }
627
+ break
628
+ } catch (exception: Exception) {
629
+ retries += 1
630
+ if (retries > 10) {
631
+ throw exception
632
+ }
633
+ delay(500)
634
+ }
619
635
  }
620
- reactApplicationContext.bindService(intent, this, Context.BIND_AUTO_CREATE)
621
636
  }
622
637
  } catch (_: Exception) {}
623
638
  }
@@ -173,12 +173,12 @@ class MusicService : HeadlessJsMediaService() {
173
173
  // Sets the service to foreground with an empty notification
174
174
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
175
175
  startForeground(
176
- 1,
176
+ EMPTY_NOTIFICATION_ID,
177
177
  NotificationCompat.Builder(this, channel).build(),
178
178
  ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
179
179
  )
180
180
  } else {
181
- startForeground(1, NotificationCompat.Builder(this, channel).build())
181
+ startForeground(EMPTY_NOTIFICATION_ID, NotificationCompat.Builder(this, channel).build())
182
182
  }
183
183
  // Stops the service right after
184
184
  stopSelf()
@@ -646,12 +646,12 @@ class MusicService : HeadlessJsMediaService() {
646
646
  try {
647
647
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
648
648
  startForeground(
649
- 1,
649
+ EMPTY_NOTIFICATION_ID,
650
650
  NotificationCompat.Builder(this, channel).build(),
651
651
  ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
652
652
  )
653
653
  } else {
654
- startForeground(1, NotificationCompat.Builder(this, channel).build())
654
+ startForeground(EMPTY_NOTIFICATION_ID, NotificationCompat.Builder(this, channel).build())
655
655
  }
656
656
  } catch (_: Exception) { }
657
657
  }
@@ -683,9 +683,17 @@ class MusicService : HeadlessJsMediaService() {
683
683
  notificationBuilder.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
684
684
  }
685
685
  val notification = notificationBuilder.build()
686
- startForeground(1, notification)
687
- @Suppress("DEPRECATION")
688
- stopForeground(true)
686
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
687
+ startForeground(EMPTY_NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
688
+ } else {
689
+ startForeground(EMPTY_NOTIFICATION_ID, notification)
690
+ }
691
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
692
+ stopForeground(STOP_FOREGROUND_REMOVE)
693
+ } else {
694
+ @Suppress("DEPRECATION")
695
+ stopForeground(true)
696
+ }
689
697
  }
690
698
 
691
699
  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -750,9 +758,9 @@ class MusicService : HeadlessJsMediaService() {
750
758
  NotificationCompat.PRIORITY_MIN
751
759
  ).build()
752
760
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
753
- startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
761
+ startForeground(EMPTY_NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
754
762
  } else {
755
- startForeground(1, notification)
763
+ startForeground(EMPTY_NOTIFICATION_ID, notification)
756
764
  }
757
765
  }
758
766
  }
@@ -1185,6 +1193,9 @@ class MusicService : HeadlessJsMediaService() {
1185
1193
  notification = it.notification;
1186
1194
  if (it.ongoing) {
1187
1195
  if (player?.playWhenReady == true) {
1196
+ if (sWakeLock?.isHeld != true) {
1197
+ sWakeLock?.acquire()
1198
+ }
1188
1199
  startForegroundIfNecessary()
1189
1200
  }
1190
1201
  } else if (shouldStopForeground()) {
@@ -1194,7 +1205,10 @@ class MusicService : HeadlessJsMediaService() {
1194
1205
  // user's queue is complete. This prevents the service from potentially
1195
1206
  // being immediately destroyed once the player finishes playing media.
1196
1207
  scope.launch {
1197
- delay(stopForegroundGracePeriod.toLong() * 1000)
1208
+ if (sWakeLock?.isHeld == true) {
1209
+ sWakeLock?.release()
1210
+ }
1211
+ // delay(stopForegroundGracePeriod.toLong() * 1000)
1198
1212
  // if (shouldStopForeground()) {
1199
1213
  // @Suppress("DEPRECATION")
1200
1214
  // stopForeground(removeNotificationWhenNotOngoing)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@javascriptcommon/react-native-track-player",
3
- "version": "1.2.23",
3
+ "version": "1.2.24",
4
4
  "description": "A fully fledged audio module created for music apps",
5
5
  "contributors": [
6
6
  {