【问题标题】:Audio file doesn't play in iPhone 5音频文件在 iPhone 5 中无法播放
【发布时间】:2013-08-09 04:15:00
【问题描述】:

我使用一个代码来播放 .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] 文件://localhost/var/mobile/Applications/0D3F5169-8DB1-4398-A09A-DB2FBADF57EF/myApp.app/Lunatic.caf 2013-08-07 20:05:52.697 myApp [7006:907] 日志音频播放器:(空) 2013-08-07 20:05:52.701 myApp[7006:907] 未播放,错误:错误域 = NSOSStatusErrorDomain 代码 =-43“操作无法完成。(OSStatus 错误 -43。)” 2013-08-07 20:05:52.703 myApp[7006:907] 文件不存在

如您所见,音频播放器日志给出“null”,错误日志显示错误 -43.... 但我再说一遍...... iphone 4s 一切正常......

任何线索为什么会发生这种情况?他们是否对 iPhone 5 中的音频进行了一些更改?会不会是我的 iPhone 5 中的设置阻止了声音播放?任何帮助将不胜感激

【问题讨论】:

    标签: iphone ios objective-c avaudioplayer


    【解决方案1】:

    您在致电 play 之前是否致电 [audioPlayer prepareToPlay]?您是否还仔细检查了 audioRoutes 以确保它在正确的频道外播放?

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

    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])
    {
    
    }
    

    【讨论】:

    • 我尝试添加 [audioPlayer prepareToPlay];在调用音频播放器播放之前...但没有任何反应...我还添加了一个检查以查看音频播放器是否正在播放并且它说它不是...我更新了问题以向您展示新代码...
    • 我更新了代码...仍然无法正常工作,但现在有一些错误...检查问题中的代码...
    • 根据你的错误有两件事:你的 audioPlayer 是 nil 并且文件不存在
    • 是的,我知道...但仍然...该文件确实存在,如果我在 iphone 4s 或 ipad 2 上测试它可以完美运行...这仅在我在 iPhone 5 上测试时发生
    • 打印出所有设备上的路径,然后进行比较。路径不同
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多