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);
Related
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);
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/
I know the question can be regarded as "politically incorrect", but I'm designing an app which "by design" must get the attention of people within the maximum possible distance range, otherwise it will not be used... :-)
I'm currently using SoundManager class, and this is the code which plays my ogg clip:
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f);
}
The problem is that the sound volume I get the clip played with appears to be dependent by "Settings/Audio/Voulme" settings the user has set. Instead it appears to be indipendent by the hardware volume buttons setting.
Is there a way for an Android app to play a sound to the maximum physical volume allowed by the device?
I'd suggest using getStreamMaxVolume and setStreamVolume to do this:
int origionalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Then once you're done just set it back to the original volume.
I think I was beaten to the punch, ahh well :)
Some code that actually does this, I'm using the MediaPlayer rather than the soundpool as this gives you a play complete callback which doesn't appear to be present on the soundpool:
final AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
final int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource("content://media/internal/audio/media/97");
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);
}
});
Btw the with call mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f); the streamVolume values are actually floats 0 -> 1 that represent a percentage of the maximum value so you'd really just want to put in 1.0f there.
You can adjust the settings before playing the audio.
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.getStreamMaxVolume(), 0);
float count=100*.01f;
MediaPlayer mp=new MediaPlayer();
mp.setLooping(false);
mp = MediaPlayer.create(ActivityName.this, myUri);
mp.setVolume(count,count);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
mp.stop();
}
});
Both of these codes worked for me but I prefer the one from MediaPlayer
AudioManager audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp=new MediaPlayer();
mp.setLooping(false);
mp = MediaPlayer.create(HomeActivity.this, notification);
mp.setVolume(count,count);
mp.start();
});
That's the wrong code.
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
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.
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);