Android Question Do While rt.RunMethod ("isPlaying", Null) does not work

Helio

Member
Hello everyone. I want to stop a ringtone from looping after it ends. I tried the code below but it doesn't exit the loop. What am I doing wrong?

B4X:
Sub getRingtone As JavaObject
    jo.InitializeStatic("android.media.RingtoneManager")
    jo2.InitializeContext
    Dim u As Uri
    u.Parse(rm.GetDefault(rm.TYPE_ALARM))
'    u.Parse("content://media/internal/audio/media/49")
    Log(u)
    Dim rt As JavaObject
    rt.InitializeContext
    rt = jo.RunMethodJO("getRingtone", Array(jo2, u))
    Log(rt)
    Return rt
End Sub

Sub PlayRingtone
    rt.RunMethod("play", Null)
    Do While rt.RunMethod("isPlaying", Null) = True
        Sleep(20)
    Loop
    Log("finished playing here")
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
ringtone will ring indefinitely. notification is more controllable.
your code is all messed up.
i can make the ringtone stop. you either need to set a long-ish sleep time or set up a little counter.
this will let your ringtone play once (or more if you like) and then stop it. my example (attached)
uses a little counter. you have to fiddle with the sleep() call to get it right. a timer would works just as well.
anyway, example below with sleeping loop.
btw, the so-called "setlooping()" method appears to be a noop.

update: i've done a routine using a timer. that version is attached as a project. sleep/loop as been commented out, but code is still there.
 

Attachments

  • ringtone.PNG
    44.3 KB · Views: 15
  • ringtone.zip
    9.3 KB · Views: 7
Last edited:
Upvote 0

Helio

Member
Thanks for trying to help me, drgottjr. But I want a screen close when the sound actually ends. I managed to do it with MediaPlayer, but it turns out I couldn't play the default sound with it. The URI returned is different from the others.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…