Android Question Ringtonemanager stop ringtone

kisoft

Well-Known 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 tried many methods:
https://www.b4x.com/android/forum/threads/stop-a-ringtone.67764/#post-531595

https://www.b4x.com/android/forum/threads/stop-a-ringtone.67378/

without success, will anyone help?
 

DonManfred

Expert
Licensed User
Longtime User
See the threads you mention.
You can see that you need to hold a reference to the ringtone you get with
B4X:
jo.RunMethodJO("getRingtone", Array(jo2, u))
This Ringtoneobject you need to hold to play, stop it. Based on the code here you are NOT holding any reference....

See this post (you already saw it but do not use it right)
https://www.b4x.com/android/forum/threads/stop-a-ringtone.67764/#post-431733

See (AND UNDERSTAND) what i am doing there.
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
I analyzed your code, works very well. You have not helped in that thread, you will not help and in that.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You have not helped in that thread
sure i did. I posted the correct code which one needs to understand. The TO in the thread does understand so he resolved his issue.

As it is working for you too now i guess you did understand the code now too.
 
Last edited:
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
here is the code. sub stop_plya does not work:
B4X:
Sub Process_Globals
   
    Dim rm As RingtoneManager
    Dim ringtone As JavaObject
    Dim rt As JavaObject
    Dim ion As Object
    Dim dzw As String
   
End Sub
Sub Globals

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
        Activity.LoadLayout("1")   
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click                                         'plya ring
        PlayRingtone(rm.GetDefault(rm.TYPE_NOTIFICATION))
End Sub
  
Sub Button2_Click                                         'stop plya
    stop_plya
End Sub

Sub Button3_Click                                         'Ring selection
    ShowPicker
End Sub
   


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

Sub stop_plya                    
    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)
   
End Sub



'**************************************** Procedures for selecting the ringing tone   *******************************************

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub


Sub ion_Event (MethodName As String, Args() As Object) As Object
    If Args(0) = -1 Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        Dim jo As JavaObject = i
        Dim uri As String = jo.RunMethod("getParcelableExtra", _
       Array As Object("android.intent.extra.ringtone.PICKED_URI"))
        Log(uri)
        dzw = uri
       
    End If
    Return Null
End Sub
''************************* wywołanie wybór dzwonków**************************
Sub ShowPicker
    Dim i As Intent
    i.Initialize("android.intent.action.RINGTONE_PICKER", "")
    i.PutExtra("android.intent.extra.ringtone.TYPE", 1)
    StartActivityForResult(i)
End Sub
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
success everything works. thank you all for your help.

here the correct code
B4X:
Sub Process_Globals
   
    Dim rm As RingtoneManager
    Dim ringtone As JavaObject
    Dim rt As JavaObject
    Dim ion As Object
    Dim dzw As String
   
End Sub
Sub Globals

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
        Activity.LoadLayout("1")   
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click                                         'plya ring
        PlayRingtone(rm.GetDefault(rm.TYPE_NOTIFICATION))
End Sub
  
Sub Button2_Click                                         'stop plya
    stop_plya
End Sub

Sub Button3_Click                                         'Ring selection
    ShowPicker
End Sub
   


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

Sub stop_plya                    
    rt.RunMethod("stop", Null)
End Sub



'**************************************** Procedures for selecting the ringing tone   *******************************************

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub


Sub ion_Event (MethodName As String, Args() As Object) As Object
    If Args(0) = -1 Then 'resultCode = RESULT_OK
        Dim i As Intent = Args(1)
        Dim jo As JavaObject = i
        Dim uri As String = jo.RunMethod("getParcelableExtra", _
       Array As Object("android.intent.extra.ringtone.PICKED_URI"))
        Log(uri)
        dzw = uri
       
    End If
    Return Null
End Sub
''************************* wywołanie wybór dzwonków**************************
Sub ShowPicker
    Dim i As Intent
    i.Initialize("android.intent.action.RINGTONE_PICKER", "")
    i.PutExtra("android.intent.extra.ringtone.TYPE", 1)
    StartActivityForResult(i)
End Sub
 
Upvote 0

Similar Threads

Top