Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to stop ringtone in qt android



  • please may you assist me i need to stop the ringtone after 2 seconds but the function to do so does not work and i have discovered that its all the public methods not working

    i am using qt 5.11.1
    (SDK Version: 26.1.1, NDK Version: 10.4.0)
    jdk1.8.0_181

    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
        if ( activity.isValid() )
        {
            jint notifcationType = QAndroidJniObject::getStaticField<jint>("android/media/RingtoneManager", "TYPE_RINGTONE");
            QAndroidJniObject notification = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", "getDefaultUri", "(I)Landroid/net/Uri;", notifcationType);
            if ( notification.isValid() )
            {
                // Note that package and class names needs to be separated with '/' and not '.'
                
                QAndroidJniObject ring = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager",
                                                                                   "getRingtone",
                                                                                   "(Landroid/content/Context;Landroid/net/Uri;)Landroid/media/Ringtone;",
                                                                                   activity.object<jobject>(),
                                                                                   notification.object<jobject>());
                if ( ring.isValid() )
                {
                    QString str;
                    ring.callMethod<void>("setStopPreviousRingtone", "(Z)V", true);
                    ring.callMethod<void>("play");
                    QThread::msleep(2000);
                    QMessageBox::information(this, "", str.setNum(ring.callMethod<jfloat>("android/media/Ringtone","getVolume", "()F")));
                    if(ring.callMethod<jboolean>("android/media/Ringtone","isPlaying", "(Z)V")){
                        QMessageBox::information(this, "", "playing");
                    }
                    else {
                        QMessageBox::information(this, "", " not playing");
                    }
                    
                    
                    QThread::msleep(2000);
                    ring.callMethod<void>("stopPreviousRingtone");
                    //ring.callObjectMethod("android/media/Ringtone","setVolume", "(F)V", 0);
                }
            }
        }
    }
    

    @raven-worx: added code tags


  • Moderators

    @gotronics
    at what point does your code exactly fail?
    here if ( ring.isValid() )?

    Also note that many methods require to be called from Android's UI thread (which Qt code doesn't by default!).
    For this you can use QtAndroid::runOnAndroidThreadSync()

    But also its very likely that the console output (or LogCat) already gives some clues what is going wrong your case.



  • @gotronics said in how to stop ringtone in qt android:

    ring.callMethod<void>("stopPreviousRingtone");
    //ring.callObjectMethod("android/media/Ringtone","setVolume", "(F)V", 0);

    if(ring. is valid) is ok because it rings or the phone rings very well

    the problem is to stop it after 2 seconds by the
    the following functions are not working

    ring.callMethod<void>("stopPreviousRingtone");

    ring.callMethod<void>("setStopPreviousRingtone", "(Z)V", true);

    ring.callMethod<jboolean>("android/media/Ringtone","isPlaying", "(Z)V")



  • @raven-worx
    thank you for the reply

    @gotronics said in how to stop ringtone in qt android:

    ring.callMethod<void>("stopPreviousRingtone");
    //ring.callObjectMethod("android/media/Ringtone","setVolume", "(F)V", 0);

    if(ring. is valid) is ok because it rings or the phone rings very well

    the problem is to stop it after 2 seconds by the
    the following functions are not working

    ring.callMethod<void>("stopPreviousRingtone");

    ring.callMethod<void>("setStopPreviousRingtone", "(Z)V", true);

    ring.callMethod<jboolean>("android/media/Ringtone","isPlaying", "(Z)V")



  • This post is deleted!


  • This post is deleted!


  • @raven-worx
    sorry if you check again my code i corrected were i had initially commented
    ring.callMethod<void>("play");


  • Moderators

    @gotronics
    the JNI signature for your isPlaying call is wrong. Should rather be ()Z (not (Z)V)

    Beside that i do not see the methods setStopPreviousRingtone and stopPreviousRingtone in the Ringtone class



  • @raven-worx
    setStopPreviousRingtone and stopPreviousRingtone are from RingToneManager
    so used incorrectly in RingTone class. I have corrected there and i can now stop the ringtone using this ring.call("stop", "()V");
    and ring.callMethod<jboolean>("isPlaying", "()Z")) now working
    thank you very much.

    i still have challenge on the following

    QString str;
      QMessageBox::information(this, " Volume", str.setNum(ring.callMethod<jfloat>("getVolume", "()F")));
    

    i am getting 0 even though the ringtone volume is way above 0 level



  • @raven-worx
    setStopPreviousRingtone and stopPreviousRingtone are from RingToneManager
    so used incorrectly in RingTone class. I have corrected there and i can now stop the ringtone using this ring.call("stop", "()V");
    and ring.callMethod<jboolean>("isPlaying", "()Z")) now working
    thank you very much.

    i still have challenge on the following

    QString str;
    QMessageBox::information(this, " Volume", str.setNum(ring.callMethod<jfloat>("getVolume", "()F")));
    i am getting 0 even though the ringtone volume is way above 0 level



  • @raven-worx

    i have been going through android api and discovered that getVolume() was introduced in API 28 its not working because i am using API19
    thank you very much


Log in to reply