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_181QAndroidJniObject 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
-
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_181QAndroidJniObject 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
@gotronics
at what point does your code exactly fail?
hereif ( 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 workingring.callMethod<void>("stopPreviousRingtone");
ring.callMethod<void>("setStopPreviousRingtone", "(Z)V", true);
ring.callMethod<jboolean>("android/media/Ringtone","isPlaying", "(Z)V")
-
@gotronics
at what point does your code exactly fail?
hereif ( 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.
@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 workingring.callMethod<void>("stopPreviousRingtone");
ring.callMethod<void>("setStopPreviousRingtone", "(Z)V", true);
ring.callMethod<jboolean>("android/media/Ringtone","isPlaying", "(Z)V")
-
@gotronics
at what point does your code exactly fail?
hereif ( 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
at what point does your code exactly fail?
hereif ( 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
at what point does your code exactly fail?
hereif ( 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.
@raven-worx
sorry if you check again my code i corrected were i had initially commented
ring.callMethod<void>("play"); -
@raven-worx
sorry if you check again my code i corrected were i had initially commented
ring.callMethod<void>("play");@gotronics
the JNI signature for yourisPlaying
call is wrong. Should rather be()Z
(not(Z)V
)Beside that i do not see the methods
setStopPreviousRingtone
andstopPreviousRingtone
in the Ringtone class -
@gotronics
the JNI signature for yourisPlaying
call is wrong. Should rather be()Z
(not(Z)V
)Beside that i do not see the methods
setStopPreviousRingtone
andstopPreviousRingtone
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
-
@gotronics
at what point does your code exactly fail?
hereif ( 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.
@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 -
@gotronics
at what point does your code exactly fail?
hereif ( 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.
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 -
This post is deleted!