2

I use a code for playing a .caf audio file, the same code works good in an iPhone 4s and an iPad 2 but doesn't play at all in a iPhone 5...

this is the code:

-(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");
}


 }

and this is the log output:

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

As you can see the audioplayer log gives "null" and the error log says error -43.... but I repeat... with iphone 4s everything works good....

Any clue why is this happening? Did they change something with audio in iPhone 5? Could it be a setting in my iPhone 5 that's preventing the sound to play? Any help would be greatly appreciated

4

1 回答 1

1

你在打电话[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])
{

}
于 2013-08-07T17:03:21.430 回答