简体   繁体   English

音频文件没有播放iphone sdk

[英]Audio file is not playing with iphone sdk

I am trying to play audio file in my iphone app.I have used this code 我正在尝试在我的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];

But will running this app i am getting this error.Can anyone please help me to solve this.I am not getting that why this error is occurring. 但是运行这个应用程序我得到这个错误。任何人都可以帮我解决这个问题。我不知道为什么会出现这个错误。

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)

Thank you for your answer.. 谢谢您的回答..

This seems to be a problem with the AVAudio frameworks when using the iOS simulator only. 当使用iOS模拟器时,这似乎是AVAudio框架的问题。 Testing on actual hardware (an iPad 2 & iPad 1, in my case) does not produce these same errors 在实际硬件上测试(在我的情况下,iPad 2和iPad 1)不会产生同样的错误

This error is just console noise from a System framework, you should ignore it, it doesn't affect you. 这个错误只是来自系统框架的控制台噪音,你应该忽略它,它不会影响你。 If your app is crashing or failing to play the real reason is elsewhere. 如果您的应用程序崩溃或无法发挥,真正的原因是在其他地方。

and please also add CoreFoundation framwork.

First you have to Add The AVFoundation Framework 首先,您必须添加AVFoundation框架

For that follow this Step 为此,请遵循此步骤

  • Expand the Targets branch in the Groups & Files panel of the project 在项目的“组和文件”面板中展开“目标”分支
  • Control-click or right-click the AudioPlayer item 按住Control键单击或右键单击AudioPlayer项
  • Choose Add > Existing Frameworks… 选择添加>现有框架...
  • Click the + button at the bottom left beneath Linked Libraries 单击链接库下方左下角的+按钮
  • Choose AVFoundation.framework and click Add 选择AVFoundation.framework并单击“添加”
  • AVFoundation.framework will now be listed under Linked Libraries. AVFoundation.framework现在将列在链接库下。 Close the window 关上窗户

Now in .h file Include this framework And Create instance of avaudio player 现在在.h文件中包含此框架并创建avaudio播放器的实例

#import <AVFoundation/AVFoundation.h>

and in interface section 并在界面部分

AVAudioPlayer *audioPlayer;

Now in Your .M file Use this code where you want to play .mp3 file. 现在位于.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];

I think this is Useful for AudioPlayer... 我认为这对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 is My SoundArray. soundArry是我的SoundArray。 pageNo is My PageControl. pageNo是我的PageControl。

I think this is Useful for Audio play: 我认为这对音频播放很有用:

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM