3

我正在尝试在我的 iphone 应用程序中播放音频文件。我已经使用了这个代码

#import <AVFoundation/AVFoundation.h>

 NSBundle *bundle = [NSBundle mainBundle];
    NSString *musicPath = [bundle pathForResource:@"audioSample" ofType:@"mp3"];
    NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
    NSError *error= [NSError errorWithDomain:@"Domain" code:0 userInfo:nil];

    AVAudioPlayer *aplayer= [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:&error];
    aplayer.delegate = self;
    [aplayer play];

但是运行这个应用程序我会收到这个错误。任何人都可以帮我解决这个问题。我不明白为什么会发生这个错误。

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: ___CFObjCIsCollectable
  Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
  Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
 in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-08-03 10:12:07.330 SampleAudioCode[591:12003] Error loading /Library/Audio/Plug-Ins/HAL/Digidesign CoreAudio.plugin/Contents/MacOS/Digidesign CoreAudio:  dlopen(/Library/Audio/Plug-Ins/HAL/Digidesign CoreAudio.plugin/Contents/MacOS/Digidesign CoreAudio, 262): Symbol not found: ___CFObjCIsCollectable
  Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
  Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
 in /System/Library/Frameworks/Security.framework/Versions/A/Security
2012-08-03 10:12:07.330 SampleAudioCode[591:12003] Cannot find function pointer NewDigiCoreAudioPlugIn for factory B8A063B5-2F3D-444A-88CB-D0B8F1B22042 in CFBundle/CFPlugIn 0xdc50f50 </Library/Audio/Plug-Ins/HAL/Digidesign CoreAudio.plugin> (bundle, not loaded)

谢谢您的回答..

4

4 回答 4

1

仅在使用 iOS 模拟器时,这似乎是 AVAudio 框架的问题。在实际硬件(在我的情况下是 iPad 2 和 iPad 1)上进行测试不会产生同样的错误

这个错误只是来自系统框架的控制台噪音,你应该忽略它,它不会影响你。如果您的应用程序崩溃或无法播放,真正的原因在其他地方。

and please also add CoreFoundation framwork.
于 2012-08-03T06:40:32.940 回答
0

首先你必须添加 AVFoundation 框架

为此,请按照此步骤

  • 在项目的 Groups & Files 面板中展开 Targets 分支
  • 按住 Control 键单击或右键单击 AudioPlayer 项目
  • 选择添加 > 现有框架...</li>
  • 单击链接库下方左下方的 + 按钮
  • 选择 AVFoundation.framework 并单击添加
  • AVFoundation.framework 现在将列在链接库下。关闭窗口

现在在 .h 文件中包含这个框架并创建 avaudio 播放器的实例

#import <AVFoundation/AVFoundation.h>

并在界面部分

AVAudioPlayer *audioPlayer;

现在在你的 .M 文件中,在你想播放 .mp3 文件的地方使用这个代码。

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);             
else 
    [audioPlayer play];
于 2012-08-03T05:16:43.667 回答
0

我认为这对 AudioPlayer 很有用...

-(void)play
{
    [audioPlayer stop];

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[soundArry objectAtIndex:pageNo.currentPage]] ofType:@"mp3"]]; 

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [audioPlayer play];


}

soundArry 是我的 SoundArray。pageNo 是我的 PageControl。

于 2012-08-03T05:41:13.997 回答
0

我认为这对音频播放很有用:

NSString *filePath=[[NSBundle mainBundle] pathForResource:@"audiofile" ofType:@"mp3"]; 

NSURL *url = [NSURL fileURLWithPath:filePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
      NSLog([error description]);               
else 
     [audioPlayer play];
于 2012-08-03T05:47:06.617 回答