Media volume instead of phone volume in Android

I am making a simple music app for Android. It works fine, but when I want to change the volume of multimedia, I can not do it directly from the application. If I press the up / down button, it changes the volume of phone calls, so I have to go to another application (game), and when I press these buttons again, the multimedia volume changes.

Do you know any way to choose that these volume buttons work only for the volume of multimedia in my application, and not for the whole phone?

Thank you very much

+4
source share
3 answers

I do not know if there is a default method for this, but you can always do this:

public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) { //magic goes here } else { return super.onKeyDown(keyCode, event); } } 
+2
source

What is easiset, is it

http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream%28int%29

just call setVolumeControlStream from your onCreate activity and pass STREAM_MUSIC or whatever you start your sound on

+2
source

The AudioManager class you are looking for is AudioManager . I believe you need a specific setStreamVolume method. The type of stream will be AudioManager.STREAM_MUSIC You can read about it [here] [1].

Edit: I think I cannot make this a hyperlink .... so here is a link to a top-level class ... strange.

[1]: http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int , int, int)

+1
source

Source: https://habr.com/ru/post/1337802/


All Articles