当单击按钮等时,我正在我的应用程序中播放“音效”,并且我在让代表在 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]
所以如果有人知道我需要导入什么类或我需要添加框架以使其正常工作,那将非常有义务!
谢谢