我使用代码播放.caf音频文件,相同的代码在iPhone 4s和iPad 2中运行良好但在iPhone 5中根本不播放...
这是代码:
-(IBAction)checkSound:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Lunatic.caf", [[NSBundle mainBundle] resourcePath]]];
NSLog(@"%@",url);
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
self.audioPlayer.numberOfLoops = 0;
self.audioPlayer.volume = 1;
NSLog(@"log audioplayer: %@",audioPlayer);
if (audioPlayer != nil)
{
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}
if (audioPlayer.playing == NO)
{
NSLog(@"not playing, error: %@",error);
}
if([[NSFileManager defaultManager] fileExistsAtPath:@"file://localhost/var/mobile/Applications/0D3F5169-8DB1-4398-A09A-DB2FBADF57EF/myApp.app/Lunatic.caf"])
{
NSLog(@"file exist");
}
else
{
NSLog(@"file doesn't exist");
}
}
这是日志输出:
2013-08-07 20:05:52.694 myApp [7006:907] file://localhost/var/mobile/Applications/0D3F5169-8DB1-4398-A09A-DB2FBADF57EF/myApp.app/Lunatic.caf 2013-08- 07 20:05:52.697 myApp [7006:907] log audioplayer:(null)2013-08-07 20:05:52.701 myApp [7006:907]没有播放,错误:错误Domain = NSOSStatusErrorDomain Code = -43“操作无法完成 . (OSStatus错误-43 . )“2013-08-07 20:05:52.703 myApp [7006:907]文件不存在
你可以看到audioplayer日志给出“null”,错误日志说错误-43 ....但我重复...用iphone 4s一切正常....
任何线索为什么会发生这种情况?他们在iPhone 5中用音频改变了什么吗?可能是我的iPhone 5中的一个设置阻止声音播放?任何帮助将不胜感激
1 回答
在打电话之前你打电话给
[audioPlayer prepareToPlay]
吗?还有你仔细检查audioRoutes以确保它正在播放正确的 Channels 吗?两个良好的健全性检查是:
1.)拨打
[audioPlayer isPlaying]
并查看是否在您拨打电话后返回是或否 .2.)设置
AVAudioPlayerDelegate
并等待audioPlayerDidFinishPlaying:
上的回调,看看它是否在音频长度后被调用 .http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html
编辑:由于您的文件实际上没有播放,这很可能意味着您正在调用的路径有问题 .
使用NSLog打印出您的路径,并使用以下命令检查文件是否确实存在: