Android AudioManager - top ranked examples from Open Source projects

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

This code example shows how to use the following methods:getAction, getKeyCode, isCanceled, isTracking, handleKeyUp
        getAudioManager().preDispatchKeyEvent(event, AudioManager.USE_DEFAULT_STREAM_TYPE);
    } 
 
    public boolean dispatchKeyEvent(KeyEvent event) {
 
        final int action = event.getAction();
        final int keyCode = event.getKeyCode();
 
        if (action == KeyEvent.ACTION_DOWN) {
            return onKeyDown(keyCode, event);
        } else { 
            return onKeyUp(keyCode, event);
        } 
    } 
     
    boolean onKeyDown(int keyCode, KeyEvent event) {
        /* **************************************************************************** 
         * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES. 
         * See the comment in PhoneWindow.onKeyDown 
         * ****************************************************************************/ 

8
CodeRank
This code example shows how to use the following methods:getRingerMode, setMode, setStreamVolume
      //retry player creation to pick up changed ringtones or audio server restarts 
      player = createPlayer();
    } 
 
    AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    int ringerMode = audioManager.getRingerMode();
 
    if (shouldVibrate()) { 
      Log.i(TAG, "Starting vibration");
      vibrator.vibrate(VIBRATE_PATTERN, 1);
    } 
 
    if (player != null && ringerMode == AudioManager.RINGER_MODE_NORMAL ) {
      audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
      audioManager.setMode(AudioManager.MODE_RINGTONE);
      try { 
        if(!player.isPlaying()) {
          player.prepare();
          player.start();
          Log.d(TAG, "Playing ringtone now...");
9
CodeRank
This code example shows how to use the following methods:getStreamMaxVolume, getStreamVolume
    initBar(system, AudioManager.STREAM_SYSTEM);
    initBar(voice, AudioManager.STREAM_VOICE_CALL);
  } 
   
  private void initBar(SeekBar bar, final int stream) {
    bar.setMax(mgr.getStreamMaxVolume(stream));
    bar.setProgress(mgr.getStreamVolume(stream));
     
    bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      public void onProgressChanged(SeekBar bar, int progress,
                                    boolean fromUser) {
        mgr.setStreamVolume(stream, progress,
                            AudioManager.FLAG_PLAY_SOUND);
      } 
       
      public void onStartTrackingTouch(SeekBar bar) {
        // no-op 
      } 
       
      public void onStopTrackingTouch(SeekBar bar) {
7
CodeRank
This code example shows how to use the following methods:isBluetoothA2dpOn, isWiredHeadsetOn
                Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0) != 0) {
            return false; 
        } 
 
        // Always speak if the user is listening through headphones. 
        if (mAudioManager.isWiredHeadsetOn() || mAudioManager.isBluetoothA2dpOn()) {
            return false; 
        } 
 
        // Don't speak since this key is used to type a password. 
        return true; 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:getRingerMode, getStreamMaxVolume, getStreamVolume
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
This code example shows how to use the following methods:isSpeakerphoneOn, setSpeakerphoneOn, setWiredHeadsetOn
        return mAudioMode == AudioMode.DEVICE;
    } 
 
    public boolean isSpeakerOn() 
    { 
        Log.d( TAG, " - isSpeakerphoneOn: " + mAudioManager.isSpeakerphoneOn() );
        return mAudioManager.isSpeakerphoneOn();
    } 
 
    public void toggleSpeaker() 
    { 
        if ( isSpeakerOn() ) 
        { 
            turnOnHeadset(); 
        } 
        else 
        { 
            turnOnSpeaker(); 
        } 
    } 

7
CodeRank
This code example shows how to use the following methods:getAction, getKeyCode, isCanceled, isTracking, handleKeyUp
        getAudioManager().preDispatchKeyEvent(event, AudioManager.USE_DEFAULT_STREAM_TYPE);
    } 
 
    public boolean dispatchKeyEvent(KeyEvent event) {
 
        final int action = event.getAction();
        final int keyCode = event.getKeyCode();
 
        if (action == KeyEvent.ACTION_DOWN) {
            return onKeyDown(keyCode, event);
        } else { 
            return onKeyUp(keyCode, event);
        } 
    } 
     
    boolean onKeyDown(int keyCode, KeyEvent event) {
        /* **************************************************************************** 
         * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES. 
         * See the comment in PhoneWindow.onKeyDown 
         * ****************************************************************************/ 

8
CodeRank
This code example shows how to use the following methods:setRouting
		// Changes the default audio output device to speaker or earpiece  
		AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE);
		if (output == (2))
			audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
		else if (output == (1)){
			audiMgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
		}else 
			Log.e("AudioHandler setAudioOutputDevice", " unknown output device");	
	} 
	 
	protected int getAudioOutputDevice(){ 
		AudioManager audiMgr = (AudioManager) mCtx.getSystemService(Context.AUDIO_SERVICE);
		if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_EARPIECE)
			return 1; 
		else if (audiMgr.getRouting(AudioManager.MODE_NORMAL) == AudioManager.ROUTE_SPEAKER)
			return 2; 
		else 
			return -1; 
	} 
} 
7
CodeRank
This code example shows how to use the following methods:setStreamVolume
    bar.setProgress(mgr.getStreamVolume(stream));
     
    bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      public void onProgressChanged(SeekBar bar, int progress,
                                    boolean fromUser) {
        mgr.setStreamVolume(stream, progress,
                            AudioManager.FLAG_PLAY_SOUND);
      } 
       
      public void onStartTrackingTouch(SeekBar bar) {
        // no-op 
      } 
       
      public void onStopTrackingTouch(SeekBar bar) {
        // no-op 
      } 
    }); 
  } 
} 
6
CodeRank

Related Android AudioManager Questions & Answers:

See Code Examples for Android AudioManager Methods: