How can I get the Android phone speaker sound amplitude? - android

I'm trying to render a graph based on the music that is playing on Android phone. But I could not figure out a way to know how loud the song is currently playing. thanks!

// getSystemService is a Context method
AudioManager audioManager =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
// see other AudioManager.STREAM_* constants
int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

Related

After Unmute phone when call came only vibrate phone, not able to play ringtone

I'm building an Android app that unmute phone when an Incoming call came in phone.
I use BroadcastReceiver to receive incoming call events. I switch phone from mute mode to Ring Mode when BroadcastReceiver receive incoming call events.
And expect Phone will vibrate and Play ringtone.
But Phone only vibrate, can't play ringtone though phone ring set to max sound.
I found many apps on play store those can unmute before call and play ringtone and vibrate both. Example: One App Link
My code Below:
public void onReceive(Context context, Intent intent) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
if (intent.getAction()!=null && intent.getAction().equals("android.intent.action.PHONE_STATE")){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume/2, AudioManager.FLAG_PLAY_SOUND);
}
}
}
I'm working on something very similar. You actually have to play a ringtone after overriding the audio settings. But you only need to do this on Android 6.0+ Get an instance of the ringtone and play it when phone state is ringing else just stop it.
public void onReceive(Context context, Intent intent) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(context, uri);
if (intent.getAction()!=null && intent.getAction().equals("android.intent.action.PHONE_STATE")){
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume/2, AudioManager.FLAG_PLAY_SOUND);
r.play();
}else {
r.stop();
}
}
}
Problems you'll run into with this implementation. You will get multiple ringtones playing because the onReceive method is called many times during Phone State changes. The provides a different context each time onReceive is called and creates a different instance of Ringtone each time. I launched a background service to fix this issue so I could hold a single reference to a context and thus a single reference to Ringtone.
Some devices may not have volume control and may operate at a fixed volume, and may not enable muting or changing the volume of audio streams.
isVolumeFixed()
This method will return true on such devices.

mediaPlayer.setVolume didn't work when a phone in a silence

I customised an alarm clock, but it didn't work when the phone was in silent mode.
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.example);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();
What could be the problem?
Volume of MediaPlayer depends on volume of AudioManager for STREAM_MUSIC.
For instance, set volume to max for stream music, before play your sound, and maybe after re-set value to old value (0):
This solution not respect choice of user. He doesn't to be disturb if he put device in silent mode !
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

Changing stream volume of ALARM STREAM when call is being handled

My app requires me to modify the volume of alarm stream when incoming call has been received, and voice transmission between two parties is going on.
I used this code:
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,volume,AudioManager.FLAG_PLAY_SOUND);
Log.d("Track","Volume set to : "+audioManager.getStreamVolume(AudioManager.STREAM_ALARM));
The log message shows me that volume is being set properly. However, if I check alarm volume in the phone, it hasn't updated.
What should I do to make sure update happens properly in this scenario?
Try these snippets:
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
// or
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_SHOW_UI);
// or
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_VIBRATE);
Source: https://www.b4x.com/android/forum/threads/programatically-change-volume.7754/

Android mute/unmute phone

My goal is to support 2 operations:
mute phone (possibly with vibrations enabled/disabled), so when a call or sms is received it won't make noise
unmute phone and restore the volume to the state before muting phone
How can I do this? What permissions are required in AndroidManifest?
This is the permission for vibrate into the manifest file
<uses-permission android:name="android.permission.VIBRATE" />
this is for to put the device in silent mode with vibrate
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
this is for to put into the ringing mode
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
public void changeRingerMode(Context context){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
/**
* To Enable silent mode.....
*/
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
/**
* To Enable Ringer mode.....
*/
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
If what you want is to disable sound and restore the sound setting to previous state, this worked for me.
static int ringstate = 0;
private void soundOn(boolean off){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if(off)
{ //turn off ringing/sound
//get the current ringer mode
ringstate = audio.getRingerMode();
if(ringstate!=AudioManager.RINGER_MODE_SILENT)
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);//turn off
}
else
{
//restore previous state
audio.setRingerMode(ringstate);
}
}
This should do.

stop camera click sound programmatically in android

I am using android's default camera to click images from within my app. I want to stop the click sound that it does on clicking an image. Is there a way to stop that click sound programtically?
Thanks.
I was able to successfully use the trick to turn the volume down. I did this just before taking the picture:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
And this just after getting the first message back:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
To disable sound use this code:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int streamType = AudioManager.STREAM_SYSTEM;
mgr.setStreamSolo(streamType, true);
mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mgr.setStreamMute(streamType, true);
To enable sound use this code:
mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
streamType = AudioManager.STREAM_SYSTEM;
mgr.setStreamSolo(streamType, false);
mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mgr.setStreamMute(streamType, false);
For my Nexus S running 4.0.3, AudioManager.STREAM_SYSTEM does not prevent camera shutter sound from emitting,
but AudioManager.STREAM_MUSIC works!
You can mute and unmute your device using the code below:
final AudioManager mode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//Silent Mode Programatically
mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//Normal Mode Programatically
mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

Categories

Resources