AVAudioPlayer 阻塞和 iOS 5 问题
Posted
技术标签:
【中文标题】AVAudioPlayer 阻塞和 iOS 5 问题【英文标题】:AVAudioPlayer blocking and iOS 5 issues 【发布时间】:2012-01-05 22:35:24 【问题描述】:我目前正在从远程服务器下载一个 .mp3 文件,一旦文件加载完毕,我会将其存储在 Caches 目录中。然后我创建一个 AVAudioPlayer 实例并将数据加载到其中:
- (void)playAudio:(NSString *)path
NSLog(@"start playing audio at path %@", path);
NSError *error = nil;
NSData *audioData = [NSData dataWithContentsOfFile:path];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
if (error == nil)
[_audioPlayer play];
在此文件开始播放期间,还会出现一些动画。如果我允许 AVAudioPlayer 开始播放,它似乎会阻止 UI 并且动画被冻结,直到歌曲播放一两秒。这是在模拟器上,仅供参考。我想知道它是否与您开始播放时 AVAudio 吐出的(可能的)错误/日志有关?有什么方法可以在不影响 UI/动画的情况下播放音频文件?
如果我注释掉 [_audioPlayer play],动画运行正常,所以问题似乎不在 initWithData: 调用中。
以下也是 ios 5 记录的问题:
2012-01-05 14:32:27.427 Lovebind[53526:207] Error loading /System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn: dlopen(/System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.433 Lovebind[53526:207] Error loading /System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn: dlopen(/System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.556 Lovebind[53526:207] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.562 Lovebind[53526:207] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.572 Lovebind[53526:207] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.578 Lovebind[53526:207] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.589 Lovebind[53526:207] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-01-05 14:32:27.595 Lovebind[53526:207] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn: dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: _CFXMLNodeGetInfoPtr
Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
in /System/Library/Frameworks/Security.framework/Versions/A/Security
【问题讨论】:
【参考方案1】:其实我会把我的cmets(现已删除)变成官方回答:
1) 当您将CoreFoundation.framework
添加到您的项目时,您的“加载错误”行应该会消失。
和
2) 在单独的线程上播放你的声音,我怀疑你的阻塞问题会消失。
实现这一点的最简单方法是:
- (void)playingAudioOnSeparateThread: (NSString *) path
if(_audioPlayer)
[_audioPlayer release];
_audioPlayer = nil; // doing this while a sound is playing might crash...
NSLog(@"start playing audio at path %@", path);
NSError *error = nil;
NSData *audioData = [NSData dataWithContentsOfFile:path];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
if (error == nil)
[_audioPlayer play];
- (void)playAudio:(NSString *)path
[NSThread detachNewThreadSelector: @selector(playingAudioOnSeparateThread:) toTarget: self withObject: path];
我没有检查过这段代码的完整性或错误检查。那是你的工作。 :-)
【讨论】:
如何在单独的线程上播放声音?有没有一种简单的方法,还是我必须手动创建一个新线程之类的?谢谢,Michael,我也会尝试添加 CoreFoundation,看看是否有帮助。 我编辑了我的答案给你一个代码 sn-p。我不确定您的项目中已经包含哪些框架,因此请尝试添加一些额外的音频框架,看看是否能解决愚蠢的缺失符号错误。 很有希望,但在我的情况下不起作用...iOS 6...尝试过压缩和非压缩的 caf 文件...(我正在播放 iPod 音乐播放器,使用闪避..完全杀死帧率)以上是关于AVAudioPlayer 阻塞和 iOS 5 问题的主要内容,如果未能解决你的问题,请参考以下文章
AVAudioPlayer 后台任务无法在 iOS 5.0.1 设备上运行,但在 iOS 5 模拟器上运行