Example usage for android.media AudioManager abandonAudioFocus

List of usage examples for android.media AudioManager abandonAudioFocus

Introduction

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

Prototype

public int abandonAudioFocus(OnAudioFocusChangeListener l) 

Source Link

Document

Abandon audio focus.

Usage

From source file:Main.java

/**
 * Utility method to abandon audio focus given an AudioManager object
 * @param am the AudioManager object/* w w w. j  av  a2s  .  c  o m*/
 * @param afChangeListener the foucs listener associated to the AudioManager
 */
public static void abandonFocus(AudioManager am, Object afChangeListener) {
    am.abandonAudioFocus((AudioManager.OnAudioFocusChangeListener) afChangeListener);
}

From source file:Main.java

public static void resumeExtraMusic(Context context) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    am.abandonAudioFocus(null);
}

From source file:net.gcompris.GComprisActivity.java

public static void abandonAudioFocus() {
    // Abandon audio focus
    Context mContext = m_instance.getApplicationContext();
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.abandonAudioFocus(null);
}

From source file:org.fdroid.enigtext.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {/*from   ww  w.  jav a  2 s .  c om*/
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        if (!sp.getBoolean(ApplicationPreferencesActivity.IN_THREAD_NOTIFICATION_PREF, true)) {
            return;
        }

        String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:com.securecomcode.text.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {//from  w w  w  .  j av a  2  s  .  c  om
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        String ringtone = TextSecurePreferences.getNotificationRingtone(context);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context, Recipients recipients) {
    try {/*from  w  w w  .j  a  v  a  2  s. c om*/
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        Uri uri = recipients.getRingtone();

        if (uri == null) {
            String ringtone = TextSecurePreferences.getNotificationRingtone(context);

            if (ringtone == null) {
                Log.w(TAG, "ringtone preference was null.");
                return;
            } else {
                uri = Uri.parse(ringtone);
            }
        }

        if (uri == null) {
            Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
            return;
        }

        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

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

@Override
public void onDestroy() {
    super.onDestroy();
    if (BuildConfig.DEBUG)
        android.util.Log.d("HeadsetTurnOn", "service destroy");
    stopForeground(true);// w w w. jav  a2 s.co m
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    am.unregisterMediaButtonEventReceiver(new ComponentName(ListeningService.this, Receiver.class));
    am.abandonAudioFocus(focusChangeListener);
}

From source file:com.reallynourl.nourl.fmpfoldermusicplayer.backend.MediaService.java

private boolean abandonAudioFocus() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int result = audioManager.abandonAudioFocus(this);
    if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Toast.makeText(getApplicationContext(), "Failed to remove audio focus.", Toast.LENGTH_LONG).show();
        return false;
    }//from w ww .java 2s  .  c o  m
    return true;
}

From source file:github.daneren2005.dsub.util.Util.java

public static void abandonAudioFocus(Context context) {
    if (focusListener != null) {
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.abandonAudioFocus(focusListener);
        focusListener = null;//from   w  ww  .java  2s. c  om
    }
}

From source file:org.amahi.anywhere.service.AudioService.java

private void tearDownAudioPlayerRemote() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    ComponentName audioReceiver = new ComponentName(getPackageName(), AudioReceiver.class.getName());

    audioManager.abandonAudioFocus(this);
    audioManager.unregisterMediaButtonEventReceiver(audioReceiver);
    audioManager.unregisterRemoteControlClient(audioPlayerRemote);
}