我已經嘗試了這裏找到的所有不同的選項,甚至使用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