Android Question [Solved]Should I use Ringtone Object or Mediaplayer object to play ringtone?

Rajesh kannan MJ

Member
Licensed User
Longtime User
Hi,

Should I use the Ringtone object or Mediaplayer object to play a particular ringtone?

Say for example, I have set ringtone and on a event I get the URI. Now I want to play it. How to do it please?

Thank you,
 

Rajesh kannan MJ

Member
Licensed User
Longtime User
Hi,

I wrote code to play and stop with @Erel code sample. I mean I am able to toggle default playing ringtone with "play" and "stop" .

But I have to play a stored content:// url with number,

Can anyone please reply?
 
Upvote 0

Rajesh kannan MJ

Member
Licensed User
Longtime User
Thanks Erel. It worked,

And I am just sharing my code here so anyone can use it. Please note that you need to optimize the code. Because, I have used many global variables 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 hg As PhoneEvents
    Dim RM As RingtoneManager
   
    Dim jo As JavaObject
    Dim jo2 As JavaObject
    Dim u As Uri
   
    Dim chosenRingTone As String
   
    Private lbl_batterylevel As Label
    Private btn_setringtone As Button
    Private btn_playringtone As Button
    Private btn_isplaying As Button
    Private btn_stopringtone 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")
    hg.Initialize("hg")
   
    jo.InitializeStatic("android.media.RingtoneManager")
    jo2.InitializeContext
    u.Parse("content://media/internal/audio/media/31")
    
    Log(RM.GetDefault(RM.TYPE_RINGTONE))
    jo2 = jo.RunMethodJO("getRingtone", Array(jo2, u))
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub RM_PickerResult (Success As Boolean, Uri As String)
    Log(Uri)
    chosenRingTone = Uri
   If Success Then
      Dim R As Reflector
      'Convert the selected Uri string to a Uri Object compatible with the getRingtone method.
      Dim RTUri As Object = R.RunStaticMethod("android.net.Uri","parse",Array As Object(Uri),Array As String("java.lang.String"))
      'Get the actual ringtone object from the Uri
      R.Target = R.RunStaticMethod("android.media.RingtoneManager","getRingtone",Array As Object(R.GetContext,RTUri),Array As String("android.content.Context","android.net.Uri"))
      'Get the title from the ringtone object
      Dim Name As String = r.RunPublicmethod("getTitle",Array As Object(R.GetContext),Array As String("android.content.Context"))
      'Silent returns Unknown ringtone
      Log("RingToneName="&Name)
   Else
      Log("Pickerresult Error")
   End If
  
End Sub

Sub btn_setringtone_Click
    RM.ShowRingtonePicker("RM",RM.TYPE_RINGTONE,True,chosenRingTone)
   
End Sub

Sub btn_playringtone_Click
    If jo2.RunMethod("isPlaying",Null) Then
         Log("Toggle stop")
         jo2.RunMethod("stop",Null)
    Else
        Log("Toggle Play")
        jo2.RunMethod("play",Null)
    End If
End Sub
 
Upvote 0

Traiser

Member
Licensed User
Longtime User
Hi, this code works great on android 5.
but In mediaplayer i use mp_complete for change icon on imageview when the ringtone is finished...
With this code how i detect when the ringtone is finished?
Thanks
 
Upvote 0

Traiser

Member
Licensed User
Longtime User
ok thanks.. but now i have another problem, with mediaplayer i used
B4X:
        Dim rf As Reflector
        rf.Target = Mp5
        rf.Target = rf.GetField("mp")
        rf.RunMethod2("setAudioStreamType","5","java.lang.int")
for change channel, now with android 5 this don't work. have you a solution? :)
 
Last edited:
Upvote 0
Top