I have two audio players in my application: 1. *AV...
# ios
y
I have two audio players in my application: 1. AVQueuePlayer - used for playing sound effects. 2. AVPlayer - used for playing heavier audio content that can run in the background. Issue: The sound effect player (AVQueuePlayer) triggers the playback completion listener of the AVPlayer, which causes the
usingBlock
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:
Copy 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?
not kotlin but kotlin colored 1
f
I don’t think it’s a kotlin issue, go look at iOS slack/forum, it will be easier for you to find help.
y
do you have a link? to join the IOS Slack?