关闭。这个问题需要更多 focused .它目前不接受答案。
想改进这个问题?更新问题,使其仅关注一个问题 editing this post .
8年前关闭。
Improve this question
我可以在播放新声音时回避我的背景音频。但是,我无法再次将背景音频级别恢复到最大值。当我的代表试图“取消”时,它只是一直被躲避。对此的正常修复是 AudiosessionSetProperty,但在 iOS 7 中已弃用,Apple 并未在弃用警告或文档中提供任何提示。
我在加载 View 时调用此方法。
- (void) configureAVAudioSession
{
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
success=[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
if (!success)
{
NSLog(@"AVAudioSession error :%@",error);
}
else
{
}
success = [session setActive:YES error:&error];
if (!success) {
NSLog(@"Error setting active %@",error);
}
else
{
NSLog(@"succes settings active");
}
}
这是我播放音频的时候
-(void)playTimeOnGo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"just-like-magic"
ofType:@"mp3"]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self;
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
success=[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
这是我在完成音频以恢复背景音频和取消停靠音频时的代表
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{
[self configureAVAudioSession];
NSLog(@"playback ended");
}
那么如何在没有弃用 API 的情况下再次取消背景音乐呢?调用
[self configureAVAudioSession];
显然不起作用....
最佳答案
女士们先生们,我为您提供了一个未弃用的工作示例,说明如何在 iOS 7 中正确使用闪避。
在您的任何“ View 加载方法”中调用此方法,其中 bool 设置为 YES。这将混合背景音频并准备闪避
- (void) configureAVAudioSession:(bool) active
{
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
success=[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
if (!success)
{
NSLog(@"AVAudioSession error :%@",error);
}
else
{
}
success = [session setActive:active error:&error];
if (!success) {
NSLog(@"Error setting active %@",error);
}
else
{
//NSLog(@"success settings active");
}
}
用这种方法播放你的音频文件。观看闪避是如何发生的。每次播放新的音频警报时,请记住设置委托(delegate),就像我在此示例中所做的那样。不要在 View 加载方法中设置一次。
-(void)playTimeOnGo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
//alertName is the name of your audio file without extention. extenstions is, doh extenstion like "mp"
pathForResource:_dataManager.optionsSettings.setStarts.alertName
ofType:_dataManager.optionsSettings.setStarts.alertExtension]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self;
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
success=[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
代表将在播放后调用此方法并调高背景音乐的音量:) 请不要混淆我正在为此使用线程。如果您愿意,您可以调用该方法,但这会使主线程停止大约一秒钟甚至两秒钟。所以没关系,不要使用线程,只需调用
[self configureAVAudioSession:NO];
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag
{
_playBackFinishedDelegateThead = [[NSThread alloc] initWithTarget:self selector:@selector(configureAVAudioSession:) object:NO];
[_playBackFinishedDelegateThead start];
}
此示例已 100% 测试并在我的应用程序中运行。
关于background - ios 7 不推荐从鸭子恢复背景音乐的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440635/