Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A simultaneous playback of media stream and a local media file to BT headphone

It is necessary to set the attribute AudioAttributes.USAGE_VOICE_COMMUNICATION and request audio focus for a mediaplayer in application to play simultaneously a media stream from server and a local media file from device to Bluetooth headphone, for example

Code Block
languagejava
themeRDark
                                                        /**
                                                         * Audio focus example
                                                        playStream.play();

 */
                                                        MediaPlayer music = MediaPlayer.create(getBaseContext(), R.raw.sound1);
                                                        /**
music.setAudioAttributes(
                                                          *    Audio focus example
new AudioAttributes.Builder()
                                                              */      .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                                                        MediaPlayer music = MediaPlayer.create(getBaseContext(), R.raw.sound1            .build());
                                                        music.start();
                                                        AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
                                                            @Override
                                                            public void onAudioFocusChange(int focusChange) {
                                                                Log.d("AudioFocus,", "onAudioFocusChange="+focusChange);
                                                                music.setVolume(1, 1);
                                                            }
                                                        };
                                                        // Request audio focus for playback
                                                        int requestResult = Flashphoner.getAudioManager().getAudioManager().requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
                                                        Log.d("AudioFocus,", "requestResult="+requestResult);
                                                        // Abandon audio focus when playback complete
                                                        music.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                                            @Override
                                                            public void onCompletion(MediaPlayer mp) {
                                                                Flashphoner.getAudioManager().getAudioManager().abandonAudioFocus(audioFocusChangeListener);
                                                            }
                                                        });