AudioManager requestAudioFocus - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for AudioManager requestAudioFocus method.

This code example shows how to use the following methods:getRingerMode, requestAudioFocus, setMode
                .getSystemService(Context.AUDIO_SERVICE);
    } 
 
    private synchronized void ringIncoming() { 
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        int ringerMode = mAudioManager.getRingerMode();
        mVibrator = (Vibrator) mContext
                .getSystemService(Context.VIBRATOR_SERVICE);
        if (mCallInProgress) {
            // Notify with single vibrate. 
            mVibrator.vibrate((long) 200);
        } else { 
            if (AudioManager.RINGER_MODE_NORMAL == ringerMode) {
                mAudioManager.setMode(AudioManager.MODE_RINGTONE);
                ring(uri, AudioManager.STREAM_RING);
            } else if (AudioManager.RINGER_MODE_VIBRATE == ringerMode) {
 
                // Start immediately 
                // Vibrate 400, break 200, Vibrate 400, break 1000 
                long[] pattern = { 0, 400, 200, 400, 1000 };

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:requestAudioFocus
	/** 
	 * Calls {@link AudioManager#requestAudioFocus(AudioManager.OnAudioFocusChangeListener, int, int)} 
	 */ 
	public static void requestAudioFocus(AudioManager manager)
	{ 
		manager.requestAudioFocus(sAudioFocus, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
	} 
 
	@Override 
	public void onAudioFocusChange(int type)
	{ 
		PlaybackService service = PlaybackService.sInstance;
		if (service != null) {
			service.onAudioFocusChange(type);
		} 
	} 
 
	/** 
	 * Calls {@link VelocityTracker#getYVelocity(int)}. 
	 */ 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:registerMediaButtonEventReceiver, requestAudioFocus
        } 
         
        public void register() 
        { 
            try { 
                audio_manager.registerMediaButtonEventReceiver(receiver_name);
                audio_manager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
                running = true;
            } catch (Exception e) {
                // Nothing 
                e.printStackTrace();
            } 
        } 
 
        public void unregister() 
        { 
            try 
            { 
                audio_manager.unregisterMediaButtonEventReceiver(receiver_name);
                audio_manager.abandonAudioFocus(this);
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:requestAudioFocus
    } 
 
    /** Requests audio focus. Returns whether request was successful or not. */ 
    public boolean requestFocus() { 
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
            mAM.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    } 
 
    /** Abandons audio focus. Returns whether request was successful or not. */ 
    public boolean abandonFocus() { 
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAM.abandonAudioFocus(this);
    } 
 
    /**  
     * Called by AudioManager on audio focus changes. We implement this by calling our 
     * MusicFocusable appropriately to relay the message. 
     */ 
    public void onAudioFocusChange(int focusChange) {
        if (mFocusable == null) return;
        switch (focusChange) {
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:registerMediaButtonEventReceiver, requestAudioFocus
				.getSystemService(Context.AUDIO_SERVICE);
	} 
 
	@Override 
	public void onPlaybackStarted() { 
		mAudioService.requestAudioFocus(mAudioFocusChangeListener,
				AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
		mAudioService.registerMediaButtonEventReceiver(getEventReceiver());
	} 
 
	@Override 
	public void onPlaybackStopped() { 
		mAudioService.abandonAudioFocus(mAudioFocusChangeListener);
		mAudioService.unregisterMediaButtonEventReceiver(getEventReceiver());
	} 
 
	private ComponentName getEventReceiver() {
		if (mEventReceiver == null) {
			mEventReceiver = new ComponentName(mContext,
					BroadcastReceiver.class);
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:requestAudioFocus
        mediaPlayerBridge.setOnPreparedListener(listener);
        mediaPlayerBridge.setOnSeekCompleteListener(listener);
        mediaPlayerBridge.setOnVideoSizeChangedListener(listener);
 
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        am.requestAudioFocus(
                listener,
                AudioManager.STREAM_MUSIC,
 
                // Request permanent focus. 
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
        return listener;
    } 
 
    /** 
     * See media/base/android/media_player_listener.cc for all the following functions. 
     */ 
    private native void nativeOnMediaError( 
            int nativeMediaPlayerListener,
            int errorType);
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:abandonAudioFocus, requestAudioFocus, unregisterMediaButtonEventReceiver
        return true; 
    } 
 
    private boolean requestAudioFocus() { 
        Log.d(TAG, "Requesting audio focus.");
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == this.audioManager.requestAudioFocus(this,
                AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    } 
 
    private boolean abandonAudioFocus() { 
        Log.d(TAG, "Abandoning audio focus.");
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == this.audioManager.abandonAudioFocus(this);
    } 
 
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN:
                Log.d(TAG, "Re/gained focus.");
                this.startPlaybackAtFullVolume();
                break; 

5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:requestAudioFocus
import android.net.wifi.WifiManager.WifiLock; 
import android.os.IBinder; 
import android.os.PowerManager; 
import android.util.Log; 
import android.widget.Toast; 
 
import java.io.IOException; 
 
/** 
 * Service that handles media playback. This is the Service through which we perform all the media 
 * handling in our application. Upon initialization, it starts a {@link MusicRetriever} to scan 
 * the user's media. Then, it waits for Intents (which come from our main activity, 
 * {@link MainActivity}, which signal the service to perform specific operations: Play, Pause, 
 * Rewind, Skip, etc. 
 */ 
public class MusicService extends Service implements OnCompletionListener, OnPreparedListener,
                OnErrorListener, MusicFocusable,
                PrepareMusicRetrieverTask.MusicRetrieverPreparedListener { 
 
    // The tag we put on debug messages 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:requestAudioFocus
        mediaPlayerBridge.setOnPreparedListener(listener);
        mediaPlayerBridge.setOnSeekCompleteListener(listener);
        mediaPlayerBridge.setOnVideoSizeChangedListener(listener);
 
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        am.requestAudioFocus(
                listener,
                AudioManager.STREAM_MUSIC,
 
                // Request permanent focus. 
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
        return listener;
    } 
 
    /** 
     * See media/base/android/media_player_listener.cc for all the following functions. 
     */ 
    private native void nativeOnMediaError( 
            int nativeMediaPlayerListener,
            int errorType);
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:requestAudioFocus
    this.service = new WeakReference<StreamingPlaybackService>(service);
  } 
 
  @Override 
  public void requestFocus() { 
    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
  } 
 
  @Override 
  public void onAudioFocusChange(final int focusChange) {
    final StreamingPlaybackService service = this.service.get();
    if (service == null) { return; }
    switch (focusChange) {
    case AudioManager.AUDIOFOCUS_LOSS:
      service.stop();
      break; 
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
      service.calmDown();
      break; 
5
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related AudioManager requestAudioFocus Questions & Answers:

requestAudioFocus Method Overview
Request audio focus.
See Code Examples for other Android AudioManager Methods: