objective-c - AVAudioPlayer 阻塞和 iOS 5 问题

标签 objective-c ios audio avaudioplayer ios5

我目前正在从远程服务器下载一个 .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) 当您将 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];
}

没有检查此代码的完整性或错误检查。那是你的工作。 :-)

关于objective-c - AVAudioPlayer 阻塞和 iOS 5 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8750861/

相关文章:

iphone - 将“设置首选项”的“键盘类型”更改为带小数点的数字键盘

ios - 检查 URL 中特定页面的可达性

iphone - AVMutableComposition 视频创建问题

ios - 第一次点火后的延迟计时器

c# - Windows-Phone-7:检查是否正在播放 soundEffectInstance 实例

ios - textView resignFirstResponder 没有触发 UIKeyboardWillHideNotification

ios - SpriteKit 和 UIViewController

ios - 从 deinit 调用 removeFromSuperview()

javascript - HTML5 音频的 jQuery 全局播放和暂停

java - 在java中暂停mp3