I've been using the following code to set up an `M...
# ios
y
I've been using the following code to set up an `MPNowPlayingSession`:
Copy code
val playingSession = MPNowPlayingSession(listOf(mediaPlayer))
playingSession.automaticallyPublishesNowPlayingInfo = true
However, the app crashes as soon as the player starts playing. Does anyone have any insights on why this might be happening or how I can fix it? Any suggestions or troubleshooting steps would be greatly appreciated!
h
do you have
UIBackgroundModes
in the
<http://plist.info|plist.info>
?
Copy code
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>
y
yes I do
h
then please share a stack-trace so that we can assist
y
Copy code
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'وَالْإِبْكَٰرِۖ (55) إِنَّ اَ۬لذِينَ يُجَٰدِلُونَ فِے ءَايَٰتِ: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: currentItem
Observed object: <AVPlayer: 0x600000014fd0>
Change: {
  kind = 1;
  new = "<AVPlayerItem: 0x60000002dd50, asset = <AVURLAsset: 0x6000002dc7c0, URL = file:///Users/takiacademy/Library/Developer/CoreSimulator/Devices/D238925B-A736-4ADE-9DF6-85543AF04D81/data/Containers/Data/Application/F5EF15CB-079E-4FC7-A431-E91B5539790D/Library/Application%20Support/audio/https___www.dropbox.com_scl_fi_bpmkl6er9npmrb3mt1dn2_part_001.mp3>>";
  old = "<null>";
}
Context: 0x1ec662230'
*** First throw call stack:
(
	0  CoreFoundation           0x00000001804ae138 __exceptionPreprocess + 172
	1  libobjc.A.dylib           0x0000000180087db4 objc_exception_throw + 56
	2  Foundation             0x0000000180d7cb10 -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] + 0
	3  Foundation             0x0000000180d7cf18 NSKeyValueNotifyObserver + 248
	4  Foundation             0x0000000180d7fecc NSKeyValueDidChange + 352
	5  Foundation             0x0000000180d7c4dc NSKeyValueDidChangeWithPerThreadPendingNotifications + 144
	6  AVFCore               0x00000001cf39c134 __109-[AVPlayer _runOnIvarAccessQueueOperationThatMayChangeCurrentItemWithPreflightBlock:modificationBlock:error:]_block_invoke_2 + 968
	7  AVFCore               0x00000001cf457994 -[AVSerializedMostlySynchronousReentrantBlockScheduler scheduleBlock:] + 304
	8  AVFCore               0x00000001cf39bc14 -[AVPlayer _runOnIvarAccessQueueOperationThatMayChangeCurrentItemWithPreflightBlock:modificationBlock:error:] + 304
	9  AVFCore               0x00000001cf3a081c -[AVPlayer replaceCurrentItemWithPlayerItem:] + 468
	10 Kotteb               0x000000010306df24 kfun:quran.audioplayer.IosAudioPlayer.playWithId#internal + 656
	11 Kotteb               0x0000000103066904 kfun:quran.audioplayer.IosAudioPlayer.play#internal + 444
	12 Kotteb               0x0000000103066ca8 kfun:quran.audioplayer.IosAudioPlayer#playAyah#suspend(kotlin.String;kotlin.coroutines.Continuation<kotlin.Unit>){}kotlin.Any + 572
	13 Kotteb               0x0000000103830aa4 kfun:quran.audioplayer.AudioPlayer#playAyah#suspend(kotlin.String;kotlin.coroutines.Continuation<kotlin.Unit>){}kotlin.Any-trampoline + 116
	14 Kotteb               0x0000000103486f80 kfun:quran.ui.QuranViewModel.$play$lambda$26$lambda$25COROUTINE$2.invokeSuspend#internal + 1932
	15 Kotteb               0x0000000103487758 kfun:quran.ui.QuranViewModel.play$lambda$26$lambda$25#internal + 344
	16 Kotteb               0x00000001034a1104 kfun:quran.ui.QuranViewModel.$play$lambda$26$lambda$25$FUNCTION_REFERENCE$81.invoke#internal + 140
	17 Kotteb               0x0000000103ded664 kfun:kotlin.Function2#invoke(1:0;1:1){}1:2-trampoline + 116
	18 Kotteb               0x0000000103ca0d74 kfun:kotlin.coroutines.intrinsics.object-4.invokeSuspend#internal + 740
	19 Kotteb               0x0000000103decdd8 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any?-trampoline + 72
	20 Kotteb               0x0000000103c9c260 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 648
	21 Kotteb               0x0000000103decec4 kfun:kotlin.coroutines.Continuation#resumeWith(kotlin.Result<1:0>){}-trampoline + 100
	22 Kotteb               0x0000000103f07f1c kfun:kotlinx.coroutines.internal#resumeCancellableWith__at__kotlin.coroutines.Continuation<0:0>(kotlin.Result<0:0>){0§<kotlin.Any?>} + 2260
	23 Kotteb               0x0000000103f13510 kfun:kotlinx.coroutines.intrinsics#startCoroutineCancellable__at__kotlin.coroutines.SuspendFunction1<0:0,0:1>(0:0;kotlin.coroutines.Continuation<0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>} + 432
	24 Kotteb               0x0000000103ea14e4 kfun:kotlinx.coroutines.CoroutineStart#invoke(kotlin.coroutines.SuspendFunction1<0:0,0:1>;0:0;kotlin.coroutines.Continuation<0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>} + 260
	25 Kotteb               0x0000000103e8fc3c kfun:kotlinx.coroutines.AbstractCoroutine#start(kotlinx.coroutines.CoroutineStart;0:0;kotlin.coroutines.SuspendFunction1<0:0,1:0>){0§<kotlin.Any?>} + 152
	26 Kotteb               0x0000000103e91f64 kfun:kotlinx.coroutines#launch__at__kotlinx.coroutines.CoroutineScope(kotlin.coroutines.CoroutineContext;kotlinx.coroutines.CoroutineStart;kotlin.coroutines.SuspendFunction1<<http://kotlinx.coroutines.Co|kotlinx.coroutines.Co>	27 Kotteb               0x0000000103e9215c kfun:kotlinx.coroutines#launch$default__at__kotlinx.coroutines.CoroutineScope(kotlin.coroutines.CoroutineContext?;kotlinx.coroutines.CoroutineStart?;kotlin.coroutines.SuspendFunction1<kotlinx.cor	28 Kotteb               0x00000001034878c8 kfun:quran.ui.QuranViewModel.play$lambda$26#internal + 288
	29 Kotteb               0x000000010349cebc kfun:quran.ui.QuranViewModel.$play$lambda$26$FUNCTION_REFERENCE$25.invoke#internal + 76
	30 Kotteb               0x000000010349cfb8 kfun:quran.ui.QuranViewModel.$play$lambda$26$FUNCTION_REFERENCE$25.$<bridge-DNN>invoke(){}#internal + 72
	31 Kotteb               0x0000000103de9f7c kfun:kotlin.Function0#invoke(){}1:0-trampoline + 100
	32 Kotteb               0x000000010349390c kfun:quran.ui.QuranViewModel.getAudioList$lambda$74$lambda$73#internal + 3868
	33 Kotteb               0x00000001034a1b34 kfun:quran.ui.QuranViewModel.$getAudioList$lambda$74$lambda$73$FUNCTION_REFERENCE$88.invoke#internal + 140
	34 Kotteb               0x00000001034a1d10 kfun:quran.ui.QuranViewModel.$getAudioList$lambda$74$lambda$73$FUNCTION_REFERENCE$88.$<bridge-NNNNU>invoke(kotlin.collections.List<quran.domain.quran.Audio>;kotlin.Boolean){}quran.domain.reposito	35 Kotteb               0x0000000103ded664 kfun:kotlin.Function2#invoke(1:0;1:1){}1:2-trampoline + 116
	36 Kotteb               0x00000001033f9870 kfun:quran.data.repository.QuranRepositoryImpl.$downloadPrioritizedAudioCOROUTINE$24.invokeSuspend#internal + 3488
	37 Kotteb               0x0000000103decdd8 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any?-trampoline + 72
	38 Kotteb               0x0000000103c9c260 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 648
	39 Kotteb               0x0000000103decec4 kfun:kotlin.coroutines.Continuation#resumeWith(kotlin.Result<1:0>){}-trampoline + 100
	40 Kotteb               0x0000000103f08fb4 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 1896
	41 Kotteb               0x0000000103f391a0 kfun:kotlinx.coroutines.Runnable#run(){}-trampoline + 92
	42 Kotteb               0x0000000103f345bc kfun:kotlinx.coroutines.DarwinMainDispatcher.dispatch$lambda$0#internal + 68
	43 Kotteb               0x0000000103f34844 kfun:kotlinx.coroutines.DarwinMainDispatcher.$dispatch$lambda$0$FUNCTION_REFERENCE$1.invoke#internal + 72
	44 Kotteb               0x0000000103f34914 kfun:kotlinx.coroutines.DarwinMainDispatcher.$dispatch$lambda$0$FUNCTION_REFERENCE$1.$<bridge-DNN>invoke(){}#internal + 72
	45 Kotteb               0x0000000103de9f7c kfun:kotlin.Function0#invoke(){}1:0-trampoline + 100
	46 Kotteb               0x0000000103f358bc _6f72672e6a6574627261696e732e6b6f746c696e783a6b6f746c696e782d636f726f7574696e65732d636f
libc++abi: terminating due to uncaught exception of type NSException
h
welp, you’re not handling the “keys” from the observer
Copy code
player.observe(.currentItem, options: [.old, .new]) { [weak self] player, change in
                // handle item change if needed
                self?.handleItemChange(change)
            }
Copy code
private func handleItemChange(_ change: NSKeyValueObservedChange<AVPlayerItem?>) {
        // any item change logic here
        if let newItem = change.newValue {
            // re-setup new item if needed
        }
    }
y
I haven’t added a standard observer for my player. Instead, I’m using a
PeriodicTimeObserverForInterval
. Additionally, every time I play, I call
replaceCurrentItemWithPlayerItem
, and the crash seems to originate from that line. Would it be helpful if I shared more of my code to diagnose the issue?
h
Sure thing, but i won't be able to help, hopefully someone else does as I'll be on a vacation from eod
y
Copy code
/**
 * Sets up a listener for playback completion.
 */
private fun setupPlaybackCompletionListener() {
    listener = NSNotificationCenter.defaultCenter.addObserverForName(
        name = AVPlayerItemDidPlayToEndTimeNotification,
        `object` = null,
        queue = NSOperationQueue.mainQueue,
        usingBlock = { handlePlaybackCompletion() }
    )
}

/**
 * Handles the completion of audio playback.
 */
private fun handlePlaybackCompletion() {
    isError = false
    updateNavigationState()
    scope.launch {
        handleRepeatLogic()
    }
}
turned out that it doesn't work on the emulator 😭