gpt4 book ai didi

java - 如何在 Android API O 中设置音频焦点 ChangeListener?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:11 25 4
gpt4 key购买 nike

由于 requestAudioFocus(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint) 在 API O 中已弃用,您如何使用 AudioFocusRequest 设置音频焦点更改监听器

最佳答案

https://developer.android.com/guide/topics/media-apps/audio-focus Official Link for the resource

 AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAudioManager.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
//Handle Focus Change
}
}).build()
);
} else {

mAudioManager.requestAudioFocus(focusChange -> {

//Handle Focus Change for earliest version of oreo
},
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}

关于java - 如何在 Android API O 中设置音频焦点 ChangeListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44873148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com