音频文件无法在iPhone 5中播放

时间:2022-01-06 18:59:04

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...

我使用代码播放.caf音频文件,相同的代码在iPhone 4s和iPad 2中运行良好,但在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

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]没有播放,错误:错误Domain = NSOSStatusErrorDomain Code = -43“操作无法不完整。(OSStatus error -43。)“2013-08-07 20:05:52.703 myApp [7006:907]文件不存在

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....

你可以看到audioplayer日志给出“null”,错误日志说错误-43 ....但我重复...用iphone 4s一切正常....

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

任何线索为什么会发生这种情况?他们在iPhone 5中用音频改变了什么吗?这可能是我的iPhone 5中的设置阻止声音播放?任何帮助将不胜感激

1 个解决方案

#1


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?

你有没有在调用play之前调用[audioPlayer prepareToPlay]?还有你仔细检查audioRoutes以确保它正在播放正确的频道吗?

Two good sanity checks are to:

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

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

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

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

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

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:

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

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

}

#1


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?

你有没有在调用play之前调用[audioPlayer prepareToPlay]?还有你仔细检查audioRoutes以确保它正在播放正确的频道吗?

Two good sanity checks are to:

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

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

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

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

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

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:

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

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

}