Audio file doesn't play in iPhone 5

人走茶凉 提交于 2019-12-12 17:28:45

问题


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


回答1:


Have you called [audioPlayer prepareToPlay] prior to calling play? Also have you double checked the audioRoutes to ensure it is playing out of the proper channel?

Two good sanity checks are to:

1.) Call [audioPlayer isPlaying] and see if it returns yes or no after you call play.

2.) Set the AVAudioPlayerDelegate and wait for the callback on audioPlayerDidFinishPlaying: and see if it gets called after the length of your audio.

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

EDIT: Since you're file actually isn't playing, that most likely means you have something wrong with the path you're calling.

Print out your path with an NSLog and also check to see if the file actually exists using:

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

}


来源:https://stackoverflow.com/questions/18109273/audio-file-doesnt-play-in-iphone-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!