Handling Android volume key
-
wrote on 14 Oct 2016, 13:23 last edited by
Hi!
Volume keys are not handled by QKeyEvent, so how to capture them?
-
Do you mean on some keyboards there is Volume keys to adjust sound level?
Those are often handled by keyboard driver. If you mean on android I guess its
another story.You should at least specify platform and Qt version.
-
wrote on 19 Oct 2016, 12:12 last edited by
@Devopia53
I would like to have c++ code after vol key was pressed
@mrjj
Of course I meant Android with Qt starting from 5.6 versions.I got some hint here:
http://lists.qt-project.org/pipermail/android-development/2016-October/000889.htmlWhen I deploy that I will write also here, but if You know more direct example, please let me know.
-
@Devopia53
I would like to have c++ code after vol key was pressed
@mrjj
Of course I meant Android with Qt starting from 5.6 versions.I got some hint here:
http://lists.qt-project.org/pipermail/android-development/2016-October/000889.htmlWhen I deploy that I will write also here, but if You know more direct example, please let me know.
-
Hmm, im not into android coding but in Desktop one might be able to use
http://doc.qt.io/qt-5/qwidget.html#nativeEventBut in link the
Override public boolean dispatchKeyEvent(KeyEvent event)
seems to be something else.It says that
"This code should be put into your applications' activity Java code. Your activity will need to inherit from QtActivity.
You'll also need to add static native methods to your activity which you call in the appropriate case.
That allows you to make the transition into the C++ part of your application which registers its functions with the JNI in JNI_OnLoad (see https://www.kdab.com/qt-android-episode-5/)."and that https://www.kdab.com/qt-android-episode-5/ says
"Why do we need JNI?"
"Because it is impossible for Qt to implement all Android features. "So it sounds to me you need to mix in some java for this (catch keys) - but info is some years old
so maybe nowadays there
is non java way ? -
wrote on 19 Oct 2016, 20:11 last edited by
@Devopia53
It was really very simple....
And makes impression....
I still can't breathe.....Thank You so much.
-
@Devopia53
It was really very simple....
And makes impression....
I still can't breathe.....Thank You so much.
wrote on 20 Oct 2016, 20:26 last edited byYour mileage may vary...
I put the qputenv() call in my camera app. It slowed the app, making viewfinder jerky & camera would not focus (tried in one environment only: Sony Xperia Z5 Compact, Android Marshmallow, Qt 5.8 beta 597) -- in other application types the problem would perhaps be not noticed...
-
wrote on 24 Oct 2016, 07:30 last edited by
Hi again,
If there still will be a need to handle Android volume after taking control on those keys, here is a way how to do it from Qt app.I made a Java class that cooperates with AudioManager - an Android native object to deal with i.e. stream volume:
package app.of.domain; import org.qtproject.qt5.android.QtNative; import android.content.Context; import android.media.AudioManager; /** Manages a volume level of output stream */ public class OutVolume { /** Common method to get Android audio manager object */ private static AudioManager am() { Context c = QtNative.activity().getApplicationContext(); return (AudioManager)c.getSystemService(c.AUDIO_SERVICE); } /** Displays native Android out volume control */ public static void show() { am().adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI); } /** Maximal value of volume (depends on device) */ public static int maxStreamVolume() { return am().getStreamMaxVolume(AudioManager.STREAM_MUSIC); } /** Current volume */ public static int streamVolume() { return am().getStreamVolume(AudioManager.STREAM_MUSIC); } /** Sets volume */ public static void setStreamVolume(int v) { am().setStreamVolume(AudioManager.STREAM_MUSIC, v, 0); } }
Now, by using goods of AndroidExtras Qt module, we may invoke native volume dialog from our QT code:
QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "show");
But also we may use Qt slider (QtWidgets or QtQuick) to control volume from it.
To set a proper range of the slider:
auto slider = new QSlider(this); slider->setRange(0, QAndroidJniObject::callStaticMethod<jint>("app/of/domain/OutVolume", "maxStreamVolume"));
Then to control volume we may connect valueChanged(int) signal of QSlider with our slot:
void OurClassWithSlider::volChangedSlot(int v) { QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "setStreamVolume", "(I)V", v); }
I hope someone can benefit from that.
-
Hi again,
If there still will be a need to handle Android volume after taking control on those keys, here is a way how to do it from Qt app.I made a Java class that cooperates with AudioManager - an Android native object to deal with i.e. stream volume:
package app.of.domain; import org.qtproject.qt5.android.QtNative; import android.content.Context; import android.media.AudioManager; /** Manages a volume level of output stream */ public class OutVolume { /** Common method to get Android audio manager object */ private static AudioManager am() { Context c = QtNative.activity().getApplicationContext(); return (AudioManager)c.getSystemService(c.AUDIO_SERVICE); } /** Displays native Android out volume control */ public static void show() { am().adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI); } /** Maximal value of volume (depends on device) */ public static int maxStreamVolume() { return am().getStreamMaxVolume(AudioManager.STREAM_MUSIC); } /** Current volume */ public static int streamVolume() { return am().getStreamVolume(AudioManager.STREAM_MUSIC); } /** Sets volume */ public static void setStreamVolume(int v) { am().setStreamVolume(AudioManager.STREAM_MUSIC, v, 0); } }
Now, by using goods of AndroidExtras Qt module, we may invoke native volume dialog from our QT code:
QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "show");
But also we may use Qt slider (QtWidgets or QtQuick) to control volume from it.
To set a proper range of the slider:
auto slider = new QSlider(this); slider->setRange(0, QAndroidJniObject::callStaticMethod<jint>("app/of/domain/OutVolume", "maxStreamVolume"));
Then to control volume we may connect valueChanged(int) signal of QSlider with our slot:
void OurClassWithSlider::volChangedSlot(int v) { QAndroidJniObject::callStaticMethod<void>("app/of/domain/OutVolume", "setStreamVolume", "(I)V", v); }
I hope someone can benefit from that.
@SeeLook
Thank you for sharing :)
1/11