Example usage for android.media AudioManager AUDIOFOCUS_REQUEST_FAILED

List of usage examples for android.media AudioManager AUDIOFOCUS_REQUEST_FAILED

Introduction

In this page you can find the example usage for android.media AudioManager AUDIOFOCUS_REQUEST_FAILED.

Prototype

int AUDIOFOCUS_REQUEST_FAILED

To view the source code for android.media AudioManager AUDIOFOCUS_REQUEST_FAILED.

Click Source Link

Document

A failed focus change request.

Usage

From source file:de.j4velin.headsetturnon.ListeningService.java

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    if (BuildConfig.DEBUG)
        android.util.Log.d("HeadsetTurnOn", "service start");
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    boolean failed = am.requestAudioFocus(focusChangeListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_FAILED;
    am.registerMediaButtonEventReceiver(new ComponentName(ListeningService.this, Receiver.class));

    if (BuildConfig.DEBUG)
        android.util.Log.d("HeadsetTurnOn", "request audiofocus failed? " + failed);

    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setOngoing(true).setContentText(getString(R.string.notification_text))
            .setContentTitle(getString(R.string.notification_title)).setSmallIcon(R.drawable.ic_notification)
            .setContentIntent(PendingIntent.getActivity(this, 0,
                    new Intent(this, Main.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0));

    startForeground(1, b.build());//from   ww  w  .  j  a v a 2 s  .  c  o  m

    return START_STICKY;
}

From source file:com.mylovemhz.simplay.MusicService.java

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
        stop();//from w w  w .  j  ava  2  s. com
        break;
    case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
        stopSelf();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        pause();
        break;
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
        lowerVolume();
        break;
    case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
    case AudioManager.AUDIOFOCUS_GAIN:
        restoreVolume();
        break;
    }
}

From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java

@SuppressLint("NewApi")
public void onPrepared(MediaPlayer mediaPlayer) {
    if (LOCAL_LOGV)
        log("onPrepared()", "v");
    if (mCurrentPlayerState.equals(ServiceRadioPlayer.STATE_RESTARTING)
            || mCurrentPlayerState.equals(ServiceRadioPlayer.STATE_COMPLETE)) {
        if (LOCAL_LOGD)
            log("newPlayer ready", "d");
    } else {/* w ww  . j a v a 2 s.c o m*/
        int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mAudioFocused = true;

            if (LOCAL_LOGV)
                log("mediaPlayer.start()", "v");
            mediaPlayer.start();
            mCurrentPlayerState = ServiceRadioPlayer.STATE_PLAYING;
            if (mInterrupted) {
                if (LOCAL_LOGV)
                    log("set interrupted = false", "v");
            }
            mInterrupted = false;

            //this.startForegroundNotification(getResources().getString(R.string.status_playing), getResources().getString(R.string.stop), true);
            this.updateNotification(getResources().getString(R.string.status_playing),
                    getResources().getString(R.string.stop), true);

            if (LOCAL_LOGV)
                log("start foreground notification: playing", "v");
            //Toast.makeText(this, "Playing", Toast.LENGTH_SHORT).show();
        } else if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
            mAudioFocused = false;

            String title = getResources().getString(R.string.error_title);
            String text = getResources().getString(R.string.error_audio_focus);
            log(text, "w");
            this.getErrorNotification(title, text);
        }

    }
}

From source file:androidx.media.widget.VideoView2.java

@SuppressWarnings("deprecation")
private void requestAudioFocus(int focusType) {
    int result;//from   w  ww.j a v  a  2 s  .com
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        AudioFocusRequest focusRequest;
        focusRequest = new AudioFocusRequest.Builder(focusType).setAudioAttributes(mAudioAttributes)
                .setOnAudioFocusChangeListener(mAudioFocusListener).build();
        result = mAudioManager.requestAudioFocus(focusRequest);
    } else {
        result = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, focusType);
    }
    if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
        mAudioFocused = false;
    } else if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocused = true;
    } else if (result == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
        mAudioFocused = false;
    }
}