📜 ⬆️ ⬇️

Qt Android and system audio control dialog

This, of course, does not mean that you can catch the event of a key pressed in the application and just set the sound level. The fact is that you can work with the application and in the background listen to music running on the player. Or just fix the file system once - and it will always work. It is necessary that when you press the buttons for adjusting the sound level on the device, the system sound level changes (well, there, jumped out the volume control, etc .; well, you understood me).

At the time of this writing, there was nothing on the Russian-language sites. I found it only on some remote overseas forum, and it was not fully spelled out, I had to bring it to my mind myself.

So let's get started.

You must go to the daddy where Qt is installed. Here it is attentive: not SDK, not where compiled programs, namely Qt are stored.
')
There we go in daddy 5.. (Where X is the version number, I have 5.2.1). Here we go to the folder where the name of the collector that you use for Android is specified (I have this android_armv7), and then we go through the following path of nested folders: \ src \ android \ java \ src \ org \ qtproject \ qt5 \ android \ bindings \

Here we are on the spot. In the folder, open the QtActivity.java file for editing.
After the line:
import java.io.DataInputStream; 

Approximately 36th line, insert the following code:
 import java.lang.Object; 

After the line:
 import android.graphics.Canvas; 

Approximately 62nd line, insert the following code:
 import android.media.AudioManager; 

Next, where different objects are declared (I did this in line 112), you need to insert the following code:
 private AudioManager audio; 

And finally, find the onKeyDown function in the code and at the beginning of it insert the code:

 switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); return true; } 

This, in my opinion, is the only piece taken from the article on the forum.

In general, the function will be as follows:

 public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); return true; } if (QtApplication.m_delegateObject != null && QtApplication.onKeyDown != null) return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyDown, keyCode, event); else return super.onKeyDown(keyCode, event); } 

Well that's all. Now rebuild your projects for Android and enjoy the sound level dialogue when it is adjusted.

Just in case, I provide a link to the forum where it was found. Everything works, checked.

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


All Articles