Handling Android volume key



  • Hi!

    Volume keys are not handled by QKeyEvent, so how to capture them?



  • @SeeLook

    Hi.

    You wanna get the event where? cpp or java?


  • Qt Champions 2016

    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.



  • @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.html

    When I deploy that I will write also here, but if You know more direct example, please let me know.



  • @SeeLook

    Ok! very simple! :)

    // in your main.cpp
    
    [...]
    int main(int argc, char *argv[])
    {
        qputenv("QT_ANDROID_VOLUME_KEYS", "1"); // "1" is dummy
        QApplication a(argc, argv);
    
    [...]
    }
    

  • Qt Champions 2016

    Hmm, im not into android coding but in Desktop one might be able to use
    http://doc.qt.io/qt-5/qwidget.html#nativeEvent

    But 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 ?



  • @Devopia53
    It was really very simple....
    And makes impression....
    I still can't breathe.....

    Thank You so much.



  • Your 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...



  • @mvuori
    qputenv() just sets system environment variable which can be quite innocent and silent or blow up something...
    I hope this one of mine was rather kind of quiet :-)
    But thanks for warning. Better blow on a cold....



  • 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.


  • Qt Champions 2016

    @SeeLook
    Thank you for sharing :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.