MediaPlayer - use ringtone volume instead of media volume - android

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

Related

How do I set my media player in android studio kotlin to work with alarm volume?

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)

How do I get the system volume programatically

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);

Get MediaPlayer & AudioManager to play through earpiece instead of speaker?

Hey I'm trying to play a R.raw file thru my earpiece instead of the main phone speaker on my app but everyhting I throw at it isn't working. WIht my current code... it just plays very light static through the earpiece and not the R.raw I want.
Here is my code below and I set it in the manifest: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(false);
am.setMode(AudioManager.MODE_IN_CALL);
MediaPlayer tellSecret = MediaPlayer.create(this, R.raw.secret);
tellSecret.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
tellSecret.start();
Any thoughts?
Try setting the Audio Manager and the Media Player modes to be AudioManager.STREAM_MUSIC.
do not use 'create' api for media player, instead use 'setDataSource' and 'prepare' apis seperately. Also call 'setAudioStreamType' api on mediaplayer instance with STREAM_VOICE_CALL before calling 'setDataSource'.
In the end after starting media player,
call setSpeakerphoneOn(false) on audio manager.

How to play media file

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.

Android notification and ringer volumes, a question

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();

Categories

Resources