Change Sound of media player in android
I am trying to change sound of media player. Default sound in beep now wants to change the beep sound. I have checked AudioManager property but not change.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
I think this is what you might be interested in:
1, increase/decrease the volume of the sound: Link Here
Basically Override the onKeyDown and then call mp.setVolumn(float leftVolume,float rightVolume);
2, mute the sound if the phone was set to silent mode
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
mp.setVolumn(0,0);
break;
}
Related
I have audios which only play when certain conditions are met. I want to increase the audio volume only when it plays, and once it finishes the volume should be set to default system volume. Currently I am setting volume to max, which works ok when audio is playing but keeps the system volume to that level even after audios are finished. Which makes a very bad situation for user when say a call comes it makes the device very noisy.
I am confused to achieve this. I have also read that using audioManager to set volume is not a good practice and it has side effects.
Here is something which I was trying to control the volume:
public void resume() {
AudioManager audioManager = (AudioManager) MobieFitSdkApplication.singleton().getSystemService(Context.AUDIO_SERVICE);
if (player != null) {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
player.setVolume(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
}
}
So how can I only increase volume when audios are playing?
-Do one thing first of all get the current volume of System:
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
after your audio complete or when you release media player at that time set the default volume which you get before the audio play.
am.setStreamVolume(AudioManager.STREAM_MUSIC,volume_level,0);
I have achieved what I have asked in my question by taking some help from the above answers.
First I am getting the original system volume and once the audio starts I am setting the system volume to 70%.
int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxDeviceVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 0.7f;
int seventyVolume = (int) (maxDeviceVolume*percent);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
Once the audio ends I am resetting the device volume to the original.
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);
I'm creating a quiz app just for learning, and I've got a problem.
In my mainactivity I have a media player. It plays music even going through other activities, and I have a mute button to stop the music. I also have a sound effect when I successfully hit the right answer (on a question which is in other activity). But I don't know how to mute this SFX along with the background music when I press the mute button. Any hints? ;)
If it has a way to mute my entire application will be so good. I found some ways to mute, but this way will mute the entire system. And it's
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
If you want to mute only the running apps sound, then you better do this:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); //Use your mediaplayer instance instead of this. Also maintain a single instance of mediaplayer for the entire app.
mp.start();
For Mute
mp.setvolume(0,0);
& Unmute or full volume
mp.setvolume(0,1);
Reference:
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)
You can just use AudioManager.setStreamMute(). Feel free to use the code below.
//mute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//unmute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
Try this. It may be of help to you.
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
I play an audio file from a URL using MediaPlayer. The file plays fine, however after playback (if the app hasnt't been explicitly killed), alarm ringtone is silent. What do I need to clear/reset on end of playback to make this not happen?
Code for the file I am using to control the voicemail playback.
http://hastebin.com/ehijobuwoc.coffee
You can try getStreamVolume at start of playback then use setStreamVolume to set it back after your playback has finished.
http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
Ok have found something that works. Solves both the alarm issue and allows me to play the voicemail through the earpiece.
I needed to setMode back to what it was when I started, in onStop():
// set mode back to normal, and speakerphone back to true.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(prevMode);
am.setSpeakerphoneOn(true);
and in init:
private void initMediaPlayer() {
// Make the media play through earpiece instead of through speaker(default)
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
prevMode = am.getMode();
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(false);
Thanks #DreadfulWeather for leading me to this answer, with some helpful questions.
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.