AVAudioPlayer不适用于iPhone 5

我正在开发一个iPhone应用程序。在这个应用程序中,我正在使用AVAudioPlayer播放audio。 这是我播放声音的代码。

NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; resourcePath = [resourcePath stringByAppendingString:@"/sound.mp3"]; NSError* err; player_ = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:resourcePath] error:&err]; if( err ) { } else { [player_ prepareToPlay]; [player_ play]; int playingLoops = 0; player_.numberOfLoops = playingLoops; } 

这是在iphone设备除了iPhone5的woking罚款。虽然我在iPhone 5上运行的应用程序,我得到以下错误,然后应用程序崩溃。

 2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error '!obj' trying to fetch default input device's sample rate 2012-11-26 09:02:28.484 test[728:1dd03] <0xb0081000> Error getting audio input device sample rate: '!obj' 2012-11-26 09:02:28.494 test[728:1dd03] <0xb0081000> AQMEIOManager::FindIOUnit: error '!dev' 

我也面临同样的问题给出同样的错误。 当试图解决这个问题,我注意到在代码中没有问题。 我们需要更新媒体播放器或安装Flash播放器,它的工作正常。

您必须将AVAudioPlayerDelegate添加到.h文件,并将以下代码添加到.m文件中

 NSString* resourcePath =[[NSBundle mainBundle]pathForResource:@"sound" ofType:@"mp3"]; NSError* err; AVAudioPlayer *audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath: resourcePath] error:&err]; audioplayer.delegate=self; if( err ) { NSLog(@"%@",err); } else { [audioplayer prepareToPlay]; [audioplayer play]; } 

此代码正在为我工​​作,并接受如果它的工作的答案。

安布,尝试下面的代码。在我的结束它工作正常。

 NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]]; NSLog(@"File path is:->%@",[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]); NSFileManager* manager; manager = [NSFileManager defaultManager]; if([manager fileExistsAtPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]]) { printf("file Exists...\n\n"); } else printf("File not exist...\n\n"); NSError *er; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; if (audioPlayer == nil) NSLog([er description]); else [audioPlayer play]; 

无论如何,让我知道。 而且,请检查您的控制台的完整path和文件是否存在。