Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. how to stop ringtone in qt android
Forum Updated to NodeBB v4.3 + New Features

how to stop ringtone in qt android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
13 Posts 4 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gotronics
    wrote on last edited by gotronics
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • G gotronics

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      G 6 Replies Last reply
      2
      • G Offline
        G Offline
        gotronics
        wrote on last edited by
        #3

        @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")

        1 Reply Last reply
        0
        • raven-worxR raven-worx

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

          G Offline
          G Offline
          gotronics
          wrote on last edited by
          #4

          @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")

          1 Reply Last reply
          0
          • raven-worxR raven-worx

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

            G Offline
            G Offline
            gotronics
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • raven-worxR raven-worx

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

              G Offline
              G Offline
              gotronics
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • raven-worxR raven-worx

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

                G Offline
                G Offline
                gotronics
                wrote on last edited by
                #7

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

                raven-worxR 1 Reply Last reply
                0
                • G gotronics

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

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @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

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  G 1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    @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

                    G Offline
                    G Offline
                    gotronics
                    wrote on last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • raven-worxR raven-worx

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

                      G Offline
                      G Offline
                      gotronics
                      wrote on last edited by
                      #10

                      @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

                      1 Reply Last reply
                      0
                      • raven-worxR raven-worx

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

                        G Offline
                        G Offline
                        gotronics
                        wrote on last edited by
                        #11

                        @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

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Ruiz
                          Banned
                          wrote on last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Massimiliano11
                            wrote on last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved