0
我想播放一首歌曲。当有通知时。当应用程序处于前景模式时,我的代码工作正常。我想在应用程序处于后台模式时播放歌曲。请帮忙。在通知中播放歌曲
我想播放一首歌曲。当有通知时。当应用程序处于前景模式时,我的代码工作正常。我想在应用程序处于后台模式时播放歌曲。请帮忙。在通知中播放歌曲
添加自定义的声音到本地通知
雨燕3.0
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
var state: UIApplicationState = application.applicationState
if state == .active {
var soundID: SystemSoundID
var mainBundle: CFBundleRef = CFBundleGetMainBundle()
var ref: CFURLRef? = CFBundleCopyResourceURL(mainBundle, ("mySoundName.wav" as? CFString), nil, nil)
AudioServicesCreateSystemSoundID(ref, soundID)
AudioServicesPlaySystemSound(soundID)
}
else {
// Push Notification received in the background
}
}
推送通知 一下添加到有效载荷..
{
aps =
{
alert = "message";
sound = "pushSound.caf";//this file will have to your bundle
};
}
你需要做一些修改在plist文件中。
1.)将所需背景模式设置为App播放音频。
2)套装应用程序不能在后台运行到YES
代码
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
var state: UIApplicationState = application.applicationState
if state == .active {
print("User Info : \(userInfo.description)")
print("User Info Alert Message : \(userInfo["aps"]["alert"])")
var messageString: String? = "\((userInfo["aps"] as? String)?["alert"] as? String)"
var playSoundOnAlert: String? = "\((userInfo["aps"] as? String)?["sound"] as? String)"
var url = URL(fileURLWithPath: "\(Bundle.main.resourcePath)/\(playSoundOnAlert)")
var error: Error?
audioPlayer = try? AVAudioPlayer(contentsOf: url)
audioPlayer.numberOfLoops = 0
audioPlayer.play()
}
}
增加了3.0版本。 – Saranjith
它在我的应用程序中工作... – Saranjith