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
                                                        playStream.play();/**
                                                         /** Audio focus example
                                                         */
Audio focus example                                                      MediaPlayer music =  */MediaPlayer.create(getBaseContext(), R.raw.sound1);
                                                        MediaPlayer music.setAudioAttributes(
= MediaPlayer.create(getBaseContext(), R.raw.sound1)                                                               new AudioAttributes.Builder()
                                                                    .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                                                                    .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);
                                                            }
                                                        });