Example usage for android.media AudioManager FLAG_PLAY_SOUND

List of usage examples for android.media AudioManager FLAG_PLAY_SOUND

Introduction

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

Prototype

int FLAG_PLAY_SOUND

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

Click Source Link

Document

Whether to play a sound when changing the volume.

Usage

From source file:Main.java

public static void setVolume(Context context, int volume) {
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
}

From source file:Main.java

public static void setCurrentVolume(int percent, Context mc) {
    AudioManager am = (AudioManager) mc.getSystemService(Context.AUDIO_SERVICE);
    int maxvolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    am.setStreamVolume(AudioManager.STREAM_MUSIC, (maxvolume * percent) / 100,
            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
    am.setMode(AudioManager.MODE_INVALID);
}

From source file:Main.java

public static void setRingVolume(Context context, int ringVloume) {
    if (ringVloume < 0) {
        ringVloume = 0;//from  w  ww. j av a 2  s .  c  o  m
    } else if (ringVloume > 7) {
        ringVloume = ringVloume % 7;
        if (ringVloume == 0) {
            ringVloume = 7;
        }
    }

    ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(AudioManager.STREAM_RING,
            ringVloume, AudioManager.FLAG_PLAY_SOUND);
}

From source file:com.darly.im.ui.BaseFragment.java

/**
 * ?,??//  w  w w . j a  v  a  2s.  co m
 * @param keyCode
 * @param event
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (streamVolume >= mMusicMaxVolume) {
            LogUtil.d(LogUtil.getLogUtilsTag(BaseFragment.class), "has set the max volume");
            return true;
        }
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume + mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume - mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

From source file:com.gao.im.ui.BaseFragment.java

/**
 * ?,??//from   w  w  w .j  a  v a 2 s  .  c o  m
 * @param keyCode
 * @param event
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if (streamVolume >= mMusicMaxVolume) {
            LogUtil.d(LogUtil.getLogUtilsTag(BaseFragment.class), "has set the max volume");
            return true;
        }
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume + mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }
    if ((event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) && mAudioManager != null) {
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int mean = mMusicMaxVolume / 7;
        if (mean == 0) {
            mean = 1;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume - mean,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

From source file:com.pixate.freestyle.fragment.ButtonsFragment.java

/** Set the volumn controling to seek bar */
private void setSeekBar(SeekBar seek) {
    AudioManager audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    seek.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_RING));
    seek.setProgress(m_nAlarmVolum);//from www .  ja  va  2 s . c  o  m
    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean arg2) {
            m_nAlarmVolum = progress;

            AudioManager audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            audioManager.setStreamVolume(AudioManager.STREAM_RING, m_nAlarmVolum,
                    AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND);
        }
    });
}

From source file:nodomain.freeyourgadget.gadgetbridge.activities.FindPhoneActivity.java

public void playRingtone() {
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (mAudioManager != null) {
        userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    }/*w w  w. j ava 2s. c  om*/
    mp = new MediaPlayer();

    Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

    try {
        mp.setDataSource(this, ringtoneUri);
        mp.setAudioStreamType(AudioManager.STREAM_ALARM);
        mp.setLooping(true);
        mp.prepare();
        mp.start();
    } catch (IOException ignore) {
    }
    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);

}

From source file:nodomain.freeyourgadget.gadgetbridge.activities.FindPhoneActivity.java

public void stopSound() {
    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, userVolume, AudioManager.FLAG_PLAY_SOUND);
    mp.stop();
    mp.reset();
    mp.release();
}

From source file:com.glanznig.beepme.view.MainActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_UP) {
            audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
        }/*from w w w  .  j a va2  s . c  o m*/
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

From source file:org.wso2.iot.agent.AlertActivity.java

/**
 * This method is used to start ringing the phone.
 *///from  w w w  .  j a v a 2s. com
@TargetApi(21)
private void startRing() {
    if (audio != null) {
        ringerMode = audio.getRingerMode();
        ringerVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING),
                AudioManager.FLAG_PLAY_SOUND);

        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);

        if (defaultRingtoneUri != null) {
            defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

            if (defaultRingtone != null) {
                if (deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP) {
                    AudioAttributes attributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
                    defaultRingtone.setAudioAttributes(attributes);
                } else {
                    defaultRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
                }
                defaultRingtone.play();
            }
        }
    }
}