从内存AVAudioPlayer preloaded声音泄漏 [英] AVAudioPlayer preloaded sound leaks from memory

查看:175
本文介绍了从内存AVAudioPlayer preloaded声音泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AVAudioPlayer实例声音加载在内存中 audioPlayer。prepareToPlay(),然后几个开始播放后它。我有一个进入的背景大约10分钟后,从内存泄漏问题,我是不是被prepared。美联储小时后,我不必 prepareToPlay()再次运行的能力。如何留在内存preloaded声音长期?我是preparing声音 applicationDidFinishLaunchingWithOptions玩方法:

I have an AVAudioPlayer instance that loads sound in memory with audioPlayer.prepareToPlay(), and then after a few start playing it. I have a problem that in approximately ten minutes after entering background, it leaks from memory and I is not being prepared. After that fed hours, I don't have an ability to run prepareToPlay() again. How to leave that preloaded sound in memory for a long term? I am preparing sound to play in applicationDidFinishLaunchingWithOptionsmethod:

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 我想在后台发挥它:<?code> self.audioPlayer .play(),它的收益真正 。也许它的工作原理,但对10后 - 等待20分钟这是行不通的。并注意 .play()现在的收益

Then in didReceiveRemoteNotification I am trying to play it in background: self.audioPlayer?.play(), it returns true. Probably it works, but after about 10 - 20 minutes it does not work. And note that .play() now returns false.


  1. 是否有禁用ARC(自动引用计数)释放和dealloc的 audioPlayer 手动?

也许我需要使用较低级别的API,如的OpenAL ?但是,由于它是在iOS的9 pcated德$ P $我不会用它,以后我会需要重写一遍,而不的OpenAL

Maybe I need to use lower level APIs such as OpenAL? However I won't use it since it is deprecated in iOS 9 and later I will need to rewrite it without OpenAL.

任何更多的想法?也许我需要使用音频单元 RemoteIO ?如果它会工作,提供一些例子吧。

Any more ideas? Maybe I need to use Audio Units with RemoteIO? If it will work, provide some examples please.

也许有第三方框架如 ObjectAL - 一个简单的实施的OpenAL 的。据我所知它是可在iOS中9。

Maybe there are third party frameworks like ObjectAL - an easy implementation of OpenAL. As I know it is available in iOS 9.

这些方法都只是我的假设。但其中最主要的问题是一样的:将这些方法从后台工作

These methods are just my assumptions. But them main question remains the same: Will those methods work from background?

推荐答案

您应该只禁用ARC。当你进入研究背景,然后如果你不使用它,它被释放。在斯威夫特创造AVAudioPlayer为未管理&LT; T&GT; 对象

You should just disable ARC. When you enter backgroud, then if you don't use it it is being released. In Swift create AVAudioPlayer as Unmanaged<T> object.

这篇关于从内存AVAudioPlayer preloaded声音泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆