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和文件是否存在。
- 使用Google地图API在100个半径范围内使用相同产品的人的GPS位置
- Swift编程第8部分开始-类型转换,安全解压缩可选项和访问控制
- 带有不带cellForRowAtIndexPath的静态单元的UITableView。 如何设置清晰的背景?
- 铿锵声:错误:链接器命令失败,退出代码1(使用-v看到调用)时,在xcode上进行unit testing
- 蜂窝数据的iOS网页错误,但不是通过Wifi? AT&T蜂窝networking最近的变化?
- 在Mapbox iOS SDK上dynamic更改MGLPointAnnotation图像
- Swift中的网络层
- 我可以在Xcode 9中添加对Swift 2的支持吗?
- iOS / Swift:viewDillLoad和viewWillAppear之间的函数我应该查询一个数据库?