Ajvar1
Ajvar1

Reputation: 147

Is it possible to call AudioServicesDisposeSystemSoundID(_ inSystemSoundID: SystemSoundID) in different target?

I am creating a notification service extension and I have a special use case so I have to play sound using AudioToolbox instead of regular notification sound.

It works pretty well, but I don't know how to stop it.

My desired behavior is that when remote push notification is received, it starts playing (that works) and when the app is launched or brought to the front it is stopped (don't know how to do it).

I play the sound like this:

func playSound() {
    guard let soundURL = Bundle.main.url(forResource: "mySound", withExtension: "aiff") else {
        return
    }
    
    var soundID: SystemSoundID = 0
    AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
            
    // Play the sound
    AudioServicesPlaySystemSound(soundID)
}

And I want to stop it in AppDelegate like this:

public func applicationWillEnterForeground(_ application: UIApplication) {
    AudioServicesDisposeSystemSoundID(0)
}

Is it caused because notification service extension is different target than app?

I also tried to call AudioServicesDisposeSystemSoundID(0) in serviceExtensionTimeWillExpire() but this method is not being called.

Upvotes: 0

Views: 156

Answers (0)

Related Questions