Skip to content

Instantly share code, notes, and snippets.

@dokinkon
Last active January 31, 2019 09:26
Show Gist options
  • Save dokinkon/9fc1d376874c07cb75bc8a02abf61e16 to your computer and use it in GitHub Desktop.
Save dokinkon/9fc1d376874c07cb75bc8a02abf61e16 to your computer and use it in GitHub Desktop.
fun exampleOfAudioFocus() {
// AUDIOFOCUS_GAIN : 嘗試取得非特定時間長度的Audio focus
// AUDIOFOCUS_GAIN_TRANSIENT, 取得一個短時間的focus,例如導航的提示音
// AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, 取得一個短時間的focus,但是可以跟其他App播放的聲音一起播放,例如導航的提示音,蓋在背景音樂上
// AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE, 預期取得一個獨佔的focus,佔有這個focus的期間,其他App應該安靜,例如語音筆記或是語音辨識
// 要播放聲音前,先取得Audio focus
// Audio focus改變時,會透過Listener通知
val gain = AudioManager.AUDIOFOCUS_GAIN
val request = AudioFocusRequest.Builder(gain)
.setOnAudioFocusChangeListener(object: AudioManager.OnAudioFocusChangeListener {
override fun onAudioFocusChange(change: Int) {
// TODO 處理Focus改變事件
}
})
.build()
audioManager.requestAudioFocus(request)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment