Audio control in Android app - android

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

Related

Android: MediaPlayer setVolume function not working

I am trying to play sound via media player on STREAM_SYSTEM
using
mediaPlayer.setVolume(1.0f , 1.0f);
or
mediaPlayer.setVolume(0.90f , 0.90f);
But it is playing on vol as per Hardware volume controls in settings.
Basically this line has no effect.
I have added
permissions "Modify..." as well in Manifest
But i saw links in stackoverflow where this API works.
I don't wanna go to API
AudioManager.setStreamVol()....
as it sets stream vol for all apps
What is problem that this setVol API is not working??
Can anybody give a link to working sample??
I faced the same issue. I thought that if I use 0.5f as parameter, I could tell the difference. I tried 0.2f, still no difference. So I thought there was a problem indeed. I finally tried 0.02f just in case, and finally I had a lower volume.
Sample:
replace your
mediaPlayer.setVolume(0.90f , 0.90f);
with
mediaPlayer.setVolume(0.09f , 0.09f);
You must put the volume value between 0.0f and 1.0f. So 1.0f is 100% of the volume. Because of that your line has not effect.
AudioManager audioManager;
int actualVolume;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actualVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, actualVolume + 1, AudioManager.FLAG_SHOW_UI);
The above code is taken from my working project, where it is used to increase the volume of the system (I used it for TV). You can use actualVolume - 1 to lower down the volume.

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

MediaPlayer.setVolume() function doesn't seem to work

I need to set media volume to MAX in my app to play a buzzer.
I am trying to do it by using media.setVolume() function but it doesn't seem to work.
I have already tried
mediaPlayer.setVolume(1.0f, 1.0f);
I have also tried
int MAX_VOLUME = 1000;
final float volume = (float) (1 - (Math.log(MAX_VOLUME - 999) / Math.log(MAX_VOLUME)));
mediaPlayer.setVolume(volume, volume);
None of the above worked for me.
Somebody pls help me on how to set media volume to full using MediaPlayer.setVolume(float, float) function.
MediaPlayer.setVolume(float, float) sets the volume of the given MediaPlayer instance. This volume is 1.0f (max) by default. It doesn't change the global media volume which is what I wanted to accomplish originally.
I found a solution that simply sets the global media volume.
Useful Remark: I found many answers on stackoverflow.com for setting max volume level or changing volume, most of them used alarm stream (STREAM_ALARM) to do so. I think using alarm stream would not be a good option if you are playing audio casually.
The global volume of a stream type (music in this case) can be changed using the following code.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Now, play your media object as a Music Stream :
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Make sure that you request MODIFY_AUDIO_SETTINGS permission in your application's manifest.
<uses-permission
android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Note: This only sets Media (Music) Volume to the max. To set other Volumes like Ringer, use STREAM_RING.
Thanks, #MrTristan for your advice, it was really helpful.
make sure you've got MODIFY_AUDIO_SETTINGS set as a permission you request in your app if that's the type of volume you're looking to set.
Note that the passed volume values are raw scalars in range 0.0 to 1.0
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)
Why don't you try just using mp.setVolume(1.0, 1.0).
setVolume() doesn't work properly, so you should set stream volume by AudioManager.
This one has a bad effect too and it is that you change the entire stream volume of the user device!
And user doesn't like this, so you should change it back to the default value.
But how?!
Define your AudioManager:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Store current volume and set stream value to its Maximum value:
int currentVolume = Objects.requireNonNull(am).getStreamVolume(AudioManager.STREAM_NOTIFICATION);
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
Define AudioAttributes.Builder and set the stream type for it:
AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
audioAttributes.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION);
Set audio attributes for your MediaPlayer before calling prepare() or prepareAsync():
mediaPlayer.setAudioAttributes(audioAttributes.build());
Finally set a on completion listener for mediaPlayer and change the volume to its default:
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, currentVolume, 0);
}
});
Finish! You could handle the problem. :)

volume control with in application in 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.

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