我想为iPhone编写应用程序,播放音频提醒序列,并与其他应用程序产生的声音进行良好的沟通。
更具体地说,我想实现以下几点:
使用下面的代码,我能够实现1.和2.但我不能同时实现3.和4.。
- (void)viewDidLoad
{
[super viewDidLoad];
audioSession = [AVAudioSession sharedInstance];
}
// Following function is used to start reproducing sounds and also called
// for the next sounds
- (IBAction)soundPlayShort:(id)sender
{
NSURL * url = @"reminder sound url here";
// soundPlayer is defined in .h file as AVAudioPlayer
if (soundPlayer == nil) {
// initialize soundPlayer
soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
soundPlayer.delegate = self;
}
[soundPlayer playAtTime:soundPlayer.deviceCurrentTime + 10];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self soundPlayShort:self];
}如果我按照下面的代码更新audioPlayerDidFinishPlaying,其他声音将继续(第3点),但序列中的下一个提醒不再调用(与第2点相关)。
- (void)restoreOtherSounds
{
[audioSession setActive:NO
withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:nil];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self restoreOtherSounds];
[self soundPlayShort:self];
}我还没有意识到如何恢复提醒序列,例如在来电之后。(第4点)
即使是部分答案也可能对我很有帮助。提前谢谢。
发布于 2013-07-11 18:09:12
与第2点相关,在soundPlayShort中初始化AVAudioPlayer之前,请尝试将音频会话设置为活动:
[[AVAudioSession sharedInstance] setActive:YES error:nil];
https://stackoverflow.com/questions/12388163
复制相似问题