我已经尝试了这里找到的所有不同的选项,甚至使用NSData来加载音频文件。我已经尝试过mp3,m4a,caf文件,但没有任何工作。AVAudioPlayer不能在iOS7上工作
没有错误信息,什么都没有。
这是一个使用XCODE 5,iOS7的新应用程序。
这是我使用
- (void)playOnce:(NSString *)aSound
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:nil];
// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"m4a"];
// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundURL error:nil];
[newAudio prepareToPlay];
// set it up and play
[newAudio setNumberOfLoops:0];
[newAudio setVolume: 1];
[newAudio setDelegate: self];
[newAudio play];
}
任何帮助是极大的赞赏。我一直坚持这些太久了。
提前,谢谢。
播放音频文件究竟是不工作怎么办?你说没有错误,那么问题是什么?你没有听到音频吗?你在使用模拟器还是设备? –
实际上没有任何事情发生。没有声音,没有。无论是在模拟器和设备上。我用一个按钮来触发播放,按钮被禁用几秒钟,然后再次启用,但没有任何反应。没有任何声音。 – Elkucho
我还添加了以下代码,但仍然没有任何内容... AVAudioSession * audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [audioSession setActive:YES error:nil]; – Elkucho