Android 类 AudioManager 中的音频焦点是什么?

发布于 2024-11-18 11:31:25 字数 188 浏览 5 评论 0原文

AudioManager 类有一个方法 abandonAudioFocus() 和另一个方法 requestAudioFocus()。我想知道什么是音频焦点?如果 AudioManager 对象获得音频焦点或失去音频焦点会发生什么?

谢谢你!

The AudioManager class has a method abandonAudioFocus() and another method requestAudioFocus(). I wonder what is audio focus? And what happens if an AudioManager object gets the audio focus or lost the audio focus?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随风而去 2024-11-25 11:31:25

它与使用扬声器时的优先级有关,以防止同时播放许多内容或被覆盖。如果您requestAudioFocus(),您就声明您想要控制权。然后,您可以使用 onAudioFocusChange(int focusChange) 进行监听,看看是否有其他东西尝试播放声音。您可能会被迫失去焦点(例如在打电话时),但稍后您可以重新获得焦点。完成后您应该abandonAudioFocus()

It has to do with priority when using the speakers, to prevent playing many things at once or being overridden. If you requestAudioFocus(), you're declaring that you want control. You can then listen with onAudioFocusChange(int focusChange) to see if anything else tries to play a sound. You may forcefully lose focus (like during a phone call) but then you can gain it later. You should abandonAudioFocus() when you're finished.

征棹 2024-11-25 11:31:25
  AudioManager am = (AudioManager)getContext().getSystemService(AUDIO_SERVICE);
                        AudioManager.OnAudioFocusChangeListener focusChangeListener =
                                focusChange -> {
                    };
                        int result = am.requestAudioFocus(focusChangeListener,
                                AudioManager.STREAM_MUSIC,
                                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
                        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                            mp = new android.media.MediaPlayer();
                            mp.setOnCompletionListener(mediaPlayer -> {
                                am.abandonAudioFocus(focusChangeListener);
                            });
                            mp.setDataSource("/data/data/" + getContext().getPackageName() + "/rasa.wav");
                            mp.prepare();
                            mp.start();
                        }
  AudioManager am = (AudioManager)getContext().getSystemService(AUDIO_SERVICE);
                        AudioManager.OnAudioFocusChangeListener focusChangeListener =
                                focusChange -> {
                    };
                        int result = am.requestAudioFocus(focusChangeListener,
                                AudioManager.STREAM_MUSIC,
                                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
                        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                            mp = new android.media.MediaPlayer();
                            mp.setOnCompletionListener(mediaPlayer -> {
                                am.abandonAudioFocus(focusChangeListener);
                            });
                            mp.setDataSource("/data/data/" + getContext().getPackageName() + "/rasa.wav");
                            mp.prepare();
                            mp.start();
                        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文