Android Question Stop A Ringtone

AllyAndroid

Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   PlayRingtone(rm.GetDefault(rm.TYPE_NOTIFICATION))
End Sub

Sub PlayRingtone(url AsString)
   Dim jo As JavaObject
   jo.InitializeStatic("android.media.RingtoneManager")
   Dim jo2 As JavaObject
   jo2.InitializeContext
   Dim u As Uri
   u.Parse(url)
   jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("play", Null)
End Sub

Using the above code, how do I stop the ringtone from playing? I have tried using the following:

B4X:
   jo2.RunMethod("stop",Null)

and

B4X:
   jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("stop", Null)

But I keep getting an error saying that the stop method could not be found.
 

AllyAndroid

Member
Licensed User
Longtime User
Here are two ways that I tried it.

B4X:
Sub Class_Globals
  Private jo As JavaObject
  Private jo2 As JavaObject
  Private u As Uri
End Sub

Sub PlayRingtone
  jo.InitializeStatic("android.media.RingtoneManager")
  jo2.InitializeContext
  u.Parse(Starter.rm.GetDefault(Starter.rm.TYPE_ALARM))
  jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("play", Null)
End Sub

Sub stopRingtone
  jo2.RunMethod("stop",Null)
End Sub

B4X:
Sub Class_Globals
  Private jo As JavaObject
  Private jo2 As JavaObject
  Private u As Uri
End Sub

Sub PlayRingtone
  jo.InitializeStatic("android.media.RingtoneManager")
  jo2.InitializeContext
  u.Parse(Starter.rm.GetDefault(Starter.rm.TYPE_ALARM))
  jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("play", Null)
End Sub

Sub stopRingtone
  jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("stop", Null)
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
jo.RunMethodJO("getRingtone", Array(jo2, u)).RunMethod("play", Null)
here is your problem. in the link i showed you find that you need a reference to the ringtone to stop it playing. the ringtone has the play/stop methods...

but you are gtting a reference and directly call play without storing the ringtone-reference.
But you need this reference to stop playing....

B4X:
Dim jo As JavaObject
jo.InitializeStatic("android.media.RingtoneManager")
Dim jo2 As JavaObject
jo2.InitializeContext
Dim u As Uri
u.Parse(rm.GetDefault(rm.TYPE_RINGTONE))
ringtone =jo.RunMethodJO("getRingtone", Array(jo2, u))
ringtone.RunMethod("stop", Null)

with this reference to ringtone you should be able to stop when calling
B4X:
ringtone.RunMethod("stop", Null)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I said that code is incomplete
If you have read the link and understand the prpblem then you should know what to do to get it work.

Here...
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim rm As RingtoneManager
  Private jo, jo2 As JavaObject
  Private rt As JavaObject
    Private u As Uri

    Private ButtonPlay As Button
    Private ButtonStop As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    rt = getRingtone ' Get ref for ringtone
   
End Sub
Sub PlayRingtone
  rt.RunMethod("play", Null)
End Sub
Sub StopRingtone
  rt.RunMethod("stop", Null)
End Sub
Sub getRingtone As JavaObject
  jo.InitializeStatic("android.media.RingtoneManager")
    jo2.InitializeContext
  Dim u As Uri
  u.Parse(rm.GetDefault(rm.TYPE_ALARM))
    'u.Parse()
  Dim rt As JavaObject
    rt.InitializeContext
  rt = jo.RunMethodJO("getRingtone", Array(jo2, u))
    Return rt
End Sub
Sub ButtonPlay_Click
    PlayRingtone
End Sub
Sub ButtonStop_Click
    StopRingtone
End Sub
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
AllyAndroid friend has solved the problem.
I use the same code and I have the same problem
 
Upvote 0

Similar Threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…