The Android phones have a setting that allows you to force the notification volume to match the ringer volume, I can't seem to find that setting anywhere in SDK, can someone help me find out how to get and set it that specific setting?
You have to use setAudioStreamType(AudioManager.STREAM_RING) in MediaPlayer Class
http://d.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int)
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(...)
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.prepare();
mediaPlayer.start();
Related
I am trying to get my media player to work with the alarm volume and not media volume. I have tried this, but it is not working, any suggestions? Also, am I am looking to change the sound file at some point, can the solution be used with a variable?
val mediaPlayer1 = MediaPlayer.create(context2, R.raw.geese)
mediaPlayer1.setAudioAttributes(AudioAttributes.Builder().setUsage((AudioAttributes.USAGE_ALARM)).build())
mediaPlayer1.start()
mediaPlayer1.setLooping(true)
I want to get the system volume level to use programatically, the one you set with the hardware keys when being outside any application.
I tried with below method but it doesnt do anything
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.notif);
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int volume_level = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
mediaPlayer.setAudioStreamType(volume_level);
mediaPlayer.start();
What do I need to change?
You have to change this line
mediaPlayer.setAudioStreamType(volume_level);
to this:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
setAudioStreamType()
Used to identify the type of audio stream
AudioManager.STREAM_ALARM is for alarm
AudioManager.STREAM_DTMF is for touch sounds
AudioManager.STREAM_MUSIC is for music
AudioManager.STREAM_NOTIFICATION is for notification
AudioManager.STREAM_RING is for phone ring
AudioManager.STREAM_SYSTEM is for system sounds
AudioManager.STREAM_VOICE_CALL is for phone calls
Please reffer offical doc here to set or get the system volume of android device.
To set volume level to 20 you can use following code:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
I have an application and I need to block the ringtone and notification sounds/vibrations, and play a custom sound while the phone is in incoming call state.
The way I'm trying to do that is to set the ringer mode into silent mode, when my application starts:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
It does mute the ringtone and notifcation sounds.
However, when I'm trying to play my custom sound when the phone is incoming call state, using the music stream:
MediaPlayer mp= MediaPlayer.create(context, R.raw.my_ringtone);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setLooping(false);
mp.start();
It doesn't work on some devices (one of them is LG G2).
I'm sure there is a way to solve this issue, because other applications/games do manage to play sounds when the ringtone/notifications is in vibration mode.
Any help would be appreciated.
Thanks in advance!
So the another solution was you can play the ringtone here snippet given below
int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
if(volume==0)
volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, volume,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringTonePath));
if(ringtone!=null){
ringtone.setStreamType(AudioManager.STREAM_ALARM);
ringtone.play();
isRinging = true;
}
isRinging set as flag if you want to programatically stop the playing or you can check the by isPlaying() of ringtone to stop playing.
How can I set volume level from ringtone instead of media volume level on MediaPlayer?
Use setAudioStreamType(int) to set the media type to type STREAM_RING, then it should use the ringer volume instead of the default STREAM_MUSIC.
Note that you must do this before the media is prepared, so you'll have to prepare it manually with setDataSource instead of using MediaPlayer.create().
Since Lollipop, setAudioAttributes() is to be used over setAudioStreamType(int).
A catch that I had missed was that setAudioAttributes won't work if MediaPlayer was created as MediaPlayer.create()
Please refer to this answer: https://stackoverflow.com/a/51008956/10045546
I am using the following code to play audio file.
I have tested the audio file on Android phone player & its playing quite loud.
When I am trying to play the same audio file from the following code , its very feeble.
Is there any problem with my code ? Can I increase the volume of the media file by changing any value ?
While testing , the volume of the Android device has been put to maximum value.
Kindly provide your inputs/sample code.
Thanks in advance.
public void playAlertSound() {
MediaPlayer player = MediaPlayer.create(this, R.raw.beep);
player.setLooping(false); // Set looping
player.setVolume(0.90f, 0.90f);
// Begin playing selected media
player.start();
// Release media instance to system
player.release();
}
Try player.setVolume(1.0f, 1.0f); instead; or just leave off that line entirely. You can also try scaling up the value past 1.0, although that's not really recommended.
You shouldn't call player.release() immediately. Try calling that in your onPause() or onDestroy() methods instead.
You might try using AudioManager.getStreamMaxVolume() to get the maximum volume and use it:
AudioManager audio =
(AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
int max = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
player.setVolume(max, max);
I'm not sure though if setVolume() expects absolute levels or multipliers from 0.0f to 1.0f. It mentions logarithmic adjustment, so you might try something closer to 1.0f like 0.95f or 1.0f itself.