當我單擊按鈕時,我在應用程序中播放「聲音效果」,並且我一直在使代表在AVAudioPlayer類中正常工作以保持聲音之後播放音樂時遇到一些困難。我有框架導入和委託設置,但我似乎無法讓AVAudioSession共享實例工作...我知道我錯過了一些東西,但似乎無法找到任何地方甚至在蘋果文檔內的明確答案。這是我正在使用的代碼來完成這個。處理音頻中斷AVAudioPlayer
#import "AVFoundation/AVAudioPlayer.h
#import "MediaPlayer/MediaPlayer.h"
@interface HomeViewController : UIViewController <AVAudioPlayerDelegate, MPMediaPlayback
> {
當按下一個按鈕
-(IBAction)About
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"button" ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
AboutViewController *about = [[AboutViewController alloc] init];
[self presentModalViewController:about animated:YES];
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"move" ofType:@"caf"];
AVAudioPlayer* theAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path1] error:NULL];
theAudio.delegate = self;
[theAudio1 play];
}
代表試圖要麼打我的聲音作爲「環境噪聲」,或只是恢復播放我的聲音發生
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
{
if (flags)
{
AVAudioPlayer *audio = [[AVAudioPlayer alloc] init];
[audio setDelegate:self];
[audio play];
}
}
後,現在最後從我所瞭解的委託只是真正在我的情況下使用的代表,我有什麼將不會工作,因爲我需要使用這樣的[AVAudioSession SharedInstance]
,但我似乎無法得到AVAudioSession
工作!
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags
{
if (flags == AVAudioSessionFlags_ResumePlay)
{
[player play];
}
}
任何幫助或建議將不勝感激。我基本上只需要知道如何獲得[AVAudio SharedInstance]
,所以如果有人知道我需要導入什麼類或框架,我需要添加以獲得這個工作,這將是非常非常有責任!
感謝
非常感謝您的回覆!看起來這確實會有幫助!謝謝! – FreeAppl3
不用擔心,樂意幫忙!儘管接受答案,讓人們知道它是有幫助的。 – msgambel