OGeek|极客世界-中国程序员成长平台

标题: iphone - 音频文件在 iPhone 5 中无法播放 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:23
标题: iphone - 音频文件在 iPhone 5 中无法播放

我使用一个代码来播放 .caf 音频文件,相同的代码在 iPhone 4s 和 iPad 2 上运行良好,但在 iPhone 5 上根本无法播放...

这是代码:

-(IBAction)checkSoundid)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 中的设置阻止了声音播放?任何帮助将不胜感激



Best Answer-推荐答案


在调用 play 之前,您是否调用了 [audioPlayer prepareToPlay]?您是否还仔细检查了 audioRoutes 以确保它在正确的 channel 中播放?

两个良好的健全性检查是:

1.) 调用 [audioPlayer isPlaying] 并在调用 play 后查看它是否返回 yes 或 no。

2.) 设置 AVAudioPlayerDelegate 并等待 audioPlayerDidFinishPlaying: 上的回调,看看它是否在您的音频长度之后被调用。

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

编辑: 由于您的文件实际上没有播放,这很可能意味着您调用的路径有问题。

使用 NSLog 打印出您的路径,并使用以下命令检查文件是否实际存在:

if([[NSFileManager defaultManager] fileExistsAtPath:yourPath])
{

}

关于iphone - 音频文件在 iPhone 5 中无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18109273/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4