开发者

why is abandonAudioFocus not working in Android Music app

开发者 https://www.devze.com 2023-04-11 13:45 出处:网络
Im using this code to get AudioFocus and it works ok with AndroidMusic app ( the one preinstalled ) int result = audioManager.requestAudioFocus(meService, AudioManager.STREAM_MUSIC,AudioManager.AUDIO

Im using this code to get AudioFocus and it works ok with

Android Music app ( the one preinstalled )

int result = audioManager.requestAudioFocus(meService, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);    

However when i release it with this code

audioManager.abandonAudioFocus(meService);

The Android Music app ( the one preinstalled ) does not continue playing.

if i use the AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK it works

but Android Music app is not lowering the volume enough.

Any ides why Android Music app is not resum开发者_Python百科ing playback?

im using api8 and using the onAudioFocusChange


You should release it in MediaPlay.onCompleteListener(){}


After trying 4 players non of them are ducking. I might be doing something wrong and would like to see that. I answer my own question that we have to live with this.


This way works fine for me.

public class HomeActivity extends Activity implements AudioManager.OnAudioFocusChangeListener {


 // Other stuff

    @Override
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            Log.i("HomeActivity", "Audio focus granted.");
        }else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            Log.i("HomeActivity", "Audio focus failed.");
        }
    }
}

Request AudioFocus:

private void requestAudioFocus(){
        AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        // Request audio focus for playback
        am.requestAudioFocus(this,
                // Use the music stream.
                AudioManager.STREAM_MUSIC,
                // Request permanent focus.
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}

Abandon AudioFocus:

private void abandonAudioFocus(){
        AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        am.abandonAudioFocus(this);
}

Hope this would help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消