AVAudioPlayer预装了内存中的声音泄漏
我有一个AVAudioPlayer实例,它使用audioPlayer.prepareToPlay()
在内存中加载声音,然后在几次开始播放之后。 我有一个问题,在进入背景后大约十分钟内,它从内存泄漏,我没有准备好。 在那个美联储时间之后,我没有能力再次运行prepareToPlay()
。 如何长时间将预先加载的声音留在内存中? 我准备在applicationDidFinishLaunchingWithOptions
方法中播放声音:
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
情况下重写它。 -
还有什么想法吗? 也许我需要将
Audio Units
与RemoteIO
配合RemoteIO
? 如果它可以工作,请提供一些例子。 -
也许有像
ObjectAL
这样的第三方框架 – 一个简单的OpenAL
实现。 据我所知,它可以在iOS 9中使用。
这些方法只是我的假设。 但他们的主要问题仍然是相同的: 这些方法是否可以从后台运作?
你应该只是禁用ARC。 当您输入背景时,如果您不使用它,它将被释放。 在Swift中创建AVAudioPlayer作为Unmanaged
对象。