我使用一段代码来播放 .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] not playing, error: Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)" 2013-08-07 20:05:52.703 myApp[7006:907] file doesn't exist
如您所见,音频播放器日志显示“null”,错误日志显示错误 -43...。 但我再说一遍……用 iphone 4s 一切正常……
知道为什么会这样吗?他们是否改变了 iPhone 5 中的音频?会不会是我的 iPhone 5 中的设置阻止了声音播放?任何帮助将不胜感激
最佳答案
您是否在调用播放之前调用了[audioPlayer prepareToPlay]
?您是否还仔细检查了 audioRoutes 以确保它正在播放正确的 channel ?
两个良好的健全性检查是:
1.) 调用[audioPlayer isPlaying]
并查看调用播放后它是否返回yes 或no。
2.) 设置 AVAudioPlayerDelegate
并等待 audioPlayerDidFinishPlaying:
上的回调,看看它是否在您的音频长度后被调用。
编辑: 由于您的文件实际上没有播放,这很可能意味着您调用的路径有问题。
使用 NSLog 打印出你的路径,并使用以下方法检查文件是否确实存在:
if([[NSFileManager defaultManager] fileExistsAtPath:yourPath])
{
}
关于iphone - 音频文件无法在 iPhone 5 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18109273/