Android Question Having trouble figuring out how to change ringtone

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I'm having a problem figuring out how to change the ringtone on a phone.

Here is the coding I have used so far:

This coding is in the main module.

B4X:
Sub IconButtonSoundFiles_Click()
    bm.Initialize(LoadBitmap(File.DirAssets,"sound files Pressed.png"))
    IconButtonSoundFiles.setIcon(False,bm)

    CC.Show("audio/*", "Choose an audio file")
End Sub

B4X:
Sub ContentChooser_Result (Success As Boolean, Dir As String, FileName As String)

    If Success = True Then
        SoundFiles.strUriChosen = FileName
        StartActivity(SoundFiles)
    End If
End Sub

This is the coding in the SoundFiles module. What I want to do here is show the title of the audio file instead of the uri of the chosen file in Activity_Create.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("SoundFileProcessing")
    Activity.Title = "Ringtone: " & strUriChosen
End Sub

Here is where I try to change the ringtone.

B4X:
    bm.Initialize(LoadBitmap(File.DirAssets,"Ringtone Pressed.png"))
    IconButtonSetAsRingtone.setIcon(False,bm)

    If UserWantsToChangeSound("Ringtone") = True Then
       
        rm.SetDefault(rm.TYPE_RINGTONE, strUriChosen)
           
        ToastMessageShow("Your default Ringtone has been changed.", False)
       
        Activity.Finish
    End If
End Sub

Instead of the ringtone being changed to the one that was chosen, some sort of default ringtone is used no matter what file I choose with the ContentChooser.

I'm not sure what I'm doing wrong with this coding.

All help will be appreciated.

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You must read the documentation:

SS-2016-04-04_16.37.02.png
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
He Erel,

Does it mean I need to use AddToMediaStore prior to using SetDefault?

I thought all I needed was to use the uri returned by ContentChooser.Show
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I noticed AddToMediaStore requires a file name but all I have is a uri because the media chooser returns a uri. How do I get a filename and path to copy into the ringtone media store?

A demo app or tutorial will really help me on this.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I tried this but got a "string can not be converted to InputStream" error.

B4X:
    Dim out As OutputStream
    out = File.OpenOutput(File.DirDefaultExternal, "Temp.mp3" ,False )
    File.Copy2(strUriChosen, out)
    out.Close

I'm lost. Please upload a demo app so I can learn how to do this.

Thanks.
 
Upvote 0
Top