Android Question Stop A Ringtone

AllyAndroid

Member
Licensed User
Longtime User
Code: Copy
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:

Code: Copy
   jo2.RunMethod("stop",Null)

and

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

But I keep getting an error saying that the stop method could not be found.
 
Upvote 0
Here are two ways that I tried it.

Code: Copy
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

Code: Copy
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
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....

Code: Copy
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
Code: Copy
ringtone.RunMethod("stop", Null)
 
Upvote 0
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...
Code: Copy
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
AllyAndroid friend has solved the problem.
I use the same code and I have the same problem
 
Upvote 0

Similar Threads

  • Locked
  • Question Question
Replies
6
Views
4K
  • Locked
  • Question Question
Replies
1
Views
2K
Replies
10
Views
6K
  • Question Question
Android Question Stop a ringtone
Replies
7
Views
5K
  • Question Question
Android Question Is ringtone playing
Replies
3
Views
3K
Back
Top