youssef hachicha
11/08/2024, 9:43 AMusingBlock
callback to execute unintentionally. I believe this happens because I am using NSNotificationCenter.defaultCenter
. (Note: The sound effect player does not have or require a listener.)
Here's my code:
kotlin
private val notificationCenter = NSNotificationCenter.defaultCenter
/**
* Sets up a listener for playback completion.
*/
private fun setupPlaybackCompletionListener() {
// This listener is currently listening to all AVPlayers
listener = notificationCenter.addObserverForName(
name = AVPlayerItemDidPlayToEndTimeNotification,
`object` = null,
queue = null,
usingBlock = { notification ->
if (notification.toString().contains("audio"))
handlePlaybackCompletion()
}
)
}
Attempted Solution:
If I try to replace NSNotificationCenter.defaultCenter
with a new instance (NSNotificationCenter()
), the usingBlock
is not triggered at all, which is problematic.
Current Workaround:
I'm currently inspecting the NSNotification?
provided in usingBlock
to check if the AVPlayerItem
URL contains "audio"
, which indicates it's a normal audio rather than a sound effect. However, I feel this is not an ideal solution.
Question:
Is there a better way to prevent the sound effect player from triggering the listener for the AVPlayer, while still ensuring the AVPlayer listener works as intended?François
11/08/2024, 10:50 AMyoussef hachicha
11/08/2024, 1:22 PM