作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一些代码可以在我的应用程序中实现音频闪避。当用户正在听一些音乐时,然后进入某个位置,将播放特定的音乐片段。发生这种情况时,正在播放的原始音乐“低声”(或变得更安静),而我的应用程序开始播放的新音乐片段开始播放。然而,在该音乐结束播放后,正在播放的原始音乐再次播放,但比之前柔和得多。有想法该怎么解决这个吗?下面是我的代码:
audioPlayer = AVAudioPlayer(data: NSData(contentsOfMappedFile: musicFilePath), error: nil)
//Before music is played, make sure background music is off and audio ducking is turned on
AVAudioSession.sharedInstance().setActive(false, withOptions: AVAudioSessionSetActiveOptions.allZeros, error: nil)
AVAudioSession.sharedInstance().setCategory("AVAudioSessionCategoryPlayback", withOptions: AVAudioSessionCategoryOptions.DuckOthers, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
//Allows audio player to play in the background & turn back on previously played music.
AVAudioSession.sharedInstance().setCategory("AVAudioSessionCategoryAmbient", withOptions: AVAudioSessionCategoryOptions.allZeros, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
我试图查看文档,但没有找到太多。任何帮助,将不胜感激。谢谢!
编辑:根据以下答案进行音频回避时代码的外观:
audioPlayer = AVAudioPlayer(data: NSData(contentsOfMappedFile: musicFilePath), error: nil)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.allZeros, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
AVAudioSession.sharedInstance().setActive(false, error: nil)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.allZeros, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
编辑 2:
audioPlayer = AVAudioPlayer(data: NSData(contentsOfMappedFile: musicFilePath), error: nil)
audioPlayer.delegate = self
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.allZeros, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.play()
然后在 audioPlayerDidFinishPlaying
方法中,我放置了隐藏音频的代码:
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool){
//Prepare to play after Sound finished playing
AVAudioSession.sharedInstance().setActive(false, error: nil)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, withOptions: AVAudioSessionCategoryOptions.allZeros, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
}
最佳答案
set audiosession active :YES when read , finish read , set back to NO at didfinish delegate
-(void)createAVSpeech : (NSString*) readString{
audioSession_ = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession_ setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers error:&setCategoryError];
NSError *activationError = nil;
[audioSession_ setActive:YES error:&activationError];
speech_ = [[AVSpeechSynthesizer alloc] init];
speech_.delegate = self;
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:readString];
NSString *readLang = @"en-US";
float speedrate = 1;
utterance.rate = AVSpeechUtteranceDefaultSpeechRate * speedrate;
utterance.pitchMultiplier =0.8;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:readLang];
[speech_ speakUtterance:utterance];
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"did finish speech");
[audioSession_ setActive:NO error:nil];
}
关于ios - Audio Ducking 后音乐更柔和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659046/
我是一名优秀的程序员,十分优秀!