Set the volume of an android application
In order for the user to control the volume , my android application has a menu consisting of a slider that provides int val开发者_如何学Pythonues from 0 to 10 , when dragged. After I obtain a value , I must set the volume to the corresponding value chosen by the user , and well , this is the part that I don't know to implement and I 'd like to find about it.
Use the AudioManager class. Essentially the code goes as follows:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(streamType, volume, flags);
The problem is that the volume of the device isn't necessarily mapped from 0 to 10 as you have in your slider. On my emulator, it's from 0 to 7. So what you need to do is getStreamMaxVolume(...)
to know what your max is, and then work out your value as a fraction of that. As an example, if your user chooses volume 8 out of 10, that's equivalent to 0.8 * 7 = 5.6, which you should round to 6 out of 7.
The "stream" refers to things like ringer volume, notification volume, music volume, etc. If you want to change the volume of the ringer, you need to make sure all your commands have AudioManager.STREAM_RING
as the streamType.
精彩评论