处理音频中断AVAudioPlayer

时间:2011-08-19 21:57:08

标签: iphone delegates avaudioplayer avaudiosession

当我点击按钮时,我正在我的应用程序中播放“声音效果”,并且我一直遇到一些困难,让代表在AVAudioPlayer类中正常工作以保持音乐播放后的音乐。我有框架导入和委托设置,但我似乎无法让AVAudioSession共享实例工作...我知道我错过了一些东西,但似乎无法在苹果文档中找到一个明确的答案。这是我正在尝试实现此目的的代码。

#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]所以如果有人知道我需要导入什么类或我需要添加的框架来使这个工作它将非常有必要!

由于

0 个答案:

没有答案