简体   繁体   中英

Audio file doesn't play in iPhone 5

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM