
let dispatchQueue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0) dispatch_async(dispatchQueue,{[weak self] in var audioSessionError: NSError? let audioSession = AVAudioSession.sharedInstance() NSNotificationCenter.defaultCenter().addobserver(self!,selector: "handleInterruption:",name: AVAudioSessionInterruptionNotification,object: nil) audioSession.setActive(true,error: nil) if (audioSession.setcategory(AVAudioSessioncategoryPlayback,error: &audioSessionError)) { println("Successfully set the audio session") } else { println("Could not set the audio session") } let filePath = NSBundle.mainBundle().pathForResource("sound",ofType:"mp3") let fileData = NSData(contentsOffile: filePath!,options: .DataReadingMappedIfSafe,error: nil) var error:NSError? self!.audioPlayer = AVAudioPlayer(data: fileData,error: &error) self!.audioPlayer?.numberOfLoops = -1 self!.audioPlayer?.delegate = self if (self?.audioPlayer?.preparetoPlay() != false) { println("Successfully prepared for playing") } else { println("Failed to prepare for playing") } }) 然后在dIDReceiveRemoteNotification我试图在后台播放:self.audioPlayer?.play(),它返回true.可能是有效的,但在大约10 – 20分钟之后,它不起作用.并注意到.play()现在返回false.
>有意义的是禁用ARC(自动引用计数)手动释放和释放audioPlayer?
>也许我需要使用较低级别的API,如OpenAL?然而,我不会使用它,因为它在iOS 9和更高版本中被弃用,我将需要重写它没有OpenAL.
>还有什么想法?也许我需要使用AudioIO AudioIO?如果可以,请提供一些例子.
>也许有像ObjectAL这样的第三方框架 – 一个简单的OpenAL实现.据我所知,它在iOS 9中可用.
这些方法只是我的假设.但他们的主要问题仍然是一样的:这些方法是否可以从后台工作?
解决方法 你应该禁用ARC.当您输入背景音乐时,如果您不使用它,它将被释放.在Swift中,将AVAudioPlayer创建为非托管< T>目的. 总结以上是内存溢出为你收集整理的ios – AVAudioPlayer从内存中预先声音泄漏全部内容,希望文章能够帮你解决ios – AVAudioPlayer从内存中预先声音泄漏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)