mute Audio Focus - Android Media

Android examples for Media:Auto Player

Description

mute Audio Focus

Demo Code


import android.annotation.TargetApi;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;

public class Main {

  @TargetApi(Build.VERSION_CODES.FROYO)//  w w w  .  j a v a  2 s. c o  m
  public static boolean muteAudioFocus(Context context, boolean bMute) {
    if (context == null) {
      return false;
    }

    boolean bool = false;
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    if (bMute) {
      int result = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
      bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
    } else {
      int result = am.abandonAudioFocus(null);
      bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
    }
    return bool;
  }
}

Related Tutorials