volume control with in application in android? - android

I am preparing an app.My app have one options button with sound on and sound off.For that i used following code,
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Unfortunately, the volume setting for that application would still reflect on the system volume setting for the stream type the application is using. What I would like to know is how to set the volume for that application only without affect the system setting.
Is it possible in android have full control over the volume of the app? Is there any method that could do that? Must I do this with code? please help me

You could use AudioTrack, a lower level sound API. It has an setStereoVolume() which operates independent of the system volume.

Related

Increase media player volume only when music is playing

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

How to Change Sound Programmatically on Android

I have a media player which plays song files. However, no matter how I try to initialize its volume, the only way to change it is manually with the volume buttons. I've tried
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, 0); // Sets volume to max
and even
mMediaPlayer.setVolume(1, 1);
but none work. I've used this code in the past without problem. I've tried my app on both 5.1.1 and 7.1.1 and no luck. It doesn't matter whether the phone's volume starts in a muted state or not. I checked and maxVolume is non-zero (I've tried just hardcoding numbers too). How can I set the initial volume programmatically? The media player starts playing automatically. (I've tried calling this within the media player's onPrepared listener too in case it made a difference. It doesn't.) I also checked whether the phone volume is "fixed". It's not.
How can I get my player to start playing at max volume (no matter what the phone was set for)?
I found the problem. I had the stream wrong. Instead of STREAM_ALARM it should have been STREAM_MUSIC. The list of streams can be found here:
https://developer.android.com/reference/android/media/AudioManager.html

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

Audio control in Android app

I am working in an android app and I need to make an optionMenu with an option to disable/enable the sounds in the app.
Is it possible in android have full control over the volume of the app?
Is there any method that could do that?
Must I do this with code?
Thanks
You can find useful information here. According to that answer, it's not possible. Just don't play the sounds.
By using AudioManager you can get current volume level and max volume level. At the time of button click/ option select you can control the volume level also. Here is the code.I hope this will help you to set volume.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
To Set Volume
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,vol_level, 0);

How to list down Android phone settings menu items in my application?

I wanted to list down android phone settings with in application.
Any ideas?
What kind of settings you need to list down??
There are many settings, and Android SDK provide the APIs to access there status.
For example, if you require to get the volume settings you may use AudioManager etc ...
Edit
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // also get for diffrent streams here
System.out.println(maxVolume);

Categories

Resources