mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-31 15:19:34 +02:00
Fix:ANR with starting foreground service before starting session
This commit is contained in:
parent
d9cdc16ff9
commit
25ec5b3dcb
3 changed files with 10 additions and 13 deletions
|
@ -92,9 +92,7 @@ class MainActivity : BridgeActivity() {
|
||||||
foregroundService = mLocalBinder.getService()
|
foregroundService = mLocalBinder.getService()
|
||||||
|
|
||||||
// Let NativeAudio know foreground service is ready and setup event listener
|
// Let NativeAudio know foreground service is ready and setup event listener
|
||||||
if (pluginCallback != null) {
|
pluginCallback()
|
||||||
pluginCallback()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import android.support.v4.media.session.PlaybackStateCompat
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.media.MediaBrowserServiceCompat
|
import androidx.media.MediaBrowserServiceCompat
|
||||||
import androidx.media.utils.MediaConstants
|
import androidx.media.utils.MediaConstants
|
||||||
import com.audiobookshelf.app.BuildConfig
|
import com.audiobookshelf.app.BuildConfig
|
||||||
|
@ -300,6 +301,13 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||||
User callable methods
|
User callable methods
|
||||||
*/
|
*/
|
||||||
fun preparePlayer(playbackSession: PlaybackSession, playWhenReady:Boolean, playbackRate:Float?) {
|
fun preparePlayer(playbackSession: PlaybackSession, playWhenReady:Boolean, playbackRate:Float?) {
|
||||||
|
if (!isStarted) {
|
||||||
|
Log.i(tag, "preparePlayer: foreground service not started - Starting service --")
|
||||||
|
Intent(ctx, PlayerNotificationService::class.java).also { intent ->
|
||||||
|
ContextCompat.startForegroundService(ctx, intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
isClosed = false
|
isClosed = false
|
||||||
val playbackRateToUse = playbackRate ?: initialPlaybackRate ?: 1f
|
val playbackRateToUse = playbackRate ?: initialPlaybackRate ?: 1f
|
||||||
initialPlaybackRate = playbackRate
|
initialPlaybackRate = playbackRate
|
||||||
|
@ -660,6 +668,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||||
PlayerListener.lastPauseTime = 0
|
PlayerListener.lastPauseTime = 0
|
||||||
isClosed = true
|
isClosed = true
|
||||||
stopForeground(true)
|
stopForeground(true)
|
||||||
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendClientMetadata(playerState: PlayerState) {
|
fun sendClientMetadata(playerState: PlayerState) {
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package com.audiobookshelf.app.plugins
|
package com.audiobookshelf.app.plugins
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.content.ContextCompat
|
|
||||||
import com.audiobookshelf.app.MainActivity
|
import com.audiobookshelf.app.MainActivity
|
||||||
import com.audiobookshelf.app.data.*
|
import com.audiobookshelf.app.data.*
|
||||||
import com.audiobookshelf.app.device.DeviceManager
|
import com.audiobookshelf.app.device.DeviceManager
|
||||||
|
@ -150,14 +148,6 @@ class AbsAudioPlayer : Plugin() {
|
||||||
|
|
||||||
@PluginMethod
|
@PluginMethod
|
||||||
fun prepareLibraryItem(call: PluginCall) {
|
fun prepareLibraryItem(call: PluginCall) {
|
||||||
// Need to make sure the player service has been started
|
|
||||||
if (!PlayerNotificationService.isStarted) {
|
|
||||||
Log.w(tag, "prepareLibraryItem: PlayerService not started - Starting foreground service --")
|
|
||||||
Intent(mainActivity, PlayerNotificationService::class.java).also { intent ->
|
|
||||||
ContextCompat.startForegroundService(mainActivity, intent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val libraryItemId = call.getString("libraryItemId", "").toString()
|
val libraryItemId = call.getString("libraryItemId", "").toString()
|
||||||
val episodeId = call.getString("episodeId", "").toString()
|
val episodeId = call.getString("episodeId", "").toString()
|
||||||
val playWhenReady = call.getBoolean("playWhenReady") == true
|
val playWhenReady = call.getBoolean("playWhenReady") == true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue