Monitor the volume of ringtones in the Android system (ringtones, volumes, mute, and speakers)

Source: Internet
Author: User

The Android program obtains and sets the ringtone and volume.
Obtain the ringtones and volume of the android mobile phone through a program. Similarly, you can set the ringtone and volume easily!

AudioManagermAudioManager = (AudioManager) getSystemService (Context. AUDIO_SERVICE );
// Call volume

Int max = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_VOICE_CALL );
Int current = mAudioManager. getStreamVolume (AudioManager. STREAM_VOICE_CALL );
Log. d ("VIOCE_CALL", "max:" + max + "current:" + current );
// System volume

Max = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_SYSTEM );
Current = mAudioManager. getStreamVolume (AudioManager. STREAM_SYSTEM );
Log. d ("SYSTEM", "max:" + max + "current:" + current );
// Ringtone volume

Max = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_RING );
Current = mAudioManager. getStreamVolume (AudioManager. STREAM_RING );
Log. d ("RING", "max:" + max + "current:" + current );
// Music volume

Max = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_MUSIC );
Current = mAudioManager. getStreamVolume (AudioManager. STREAM_MUSIC );
Log. d ("MUSIC", "max:" + max + "current:" + current );
// Sound volume prompt

The method for setting the volume is also very simple. AudioManager provides the method:
Public void setStreamVolume (int streamType, int index, int flags)

StreamType is the ringtone type, such as AudioManager. STREAM_VOICE_CALL and AudioManager. STREAM_SYSTEM. The index is the volume size.

Falgs is the flag.

Set Vibration:
MVibrator = (Vibrator) mContext. getSystemService (Service. VIBRATOR_SERVICE );
Long [] pattern = {150,100}; // OFF/ON/OFF/ON...
MVibrator. vibrate (pattern,-1 );

Mute:
Set the system sound to 0.

// Set mute during a call

System. out. println ("isMicrophoneMute =" + audioManager. isMicrophoneMute ());
AudioManager. setMicrophoneMute (! AudioManager. isMicrophoneMute ());

// Set hands-free during calls

System. out. println ("isSpeakerphoneOn =" + audioManager. isSpeakerphoneOn ());
AudioManager. setSpeakerphoneOn (! AudioManager. isSpeakerphoneOn ());

// Do not forget the modified permissions.



Max = mAudioManager. getStreamMaxVolume (AudioManager. STREAM_ALARM );
Current = mAudioManager. getStreamVolume (AudioManager. STREAM_ALARM );
Log. d ("ALARM", "max:" + max + "current:" + current );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.