Android Question Make Android Player Play Sounds with ContentChooser

Marc DANIEL

Well-Known Member
Licensed User
Longtime User
Hello. I am currently working on managing sounds recorded or downloaded on external storage on my Smartphone.
I can read and play MP3 sounds with exoplayer and for other sounds (3gp, flac, wav, mid) I want to use the Android player of my device (which reads and plays all these sounds very well) with ContentChooser but I systematically get the following message:
"No application can perform this action."

[CODE lang="b4x" title="Sub CLV1_ItemLongClick"]
Sub CLV1_ItemLongClick(Index As Int, Value As Object)
Dim suffix As String, ls As Int
SongName = ListSongName.Get(Index)
Storage.FindFile(Storage.Root,SongName)
ls = SongName.length
suffix = SongName.SubString2(ls-3, ls)
Log (suffix)
If suffix <> "mp3" Then
Chooser.Show(File.DirInternal,SongName)
Else
Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName))
JukeBox.Player = Player1
Label1.Text = " Music Title: " & SongName
Player1.Play
End If
End Sub[/CODE]


Screenshot_20250308-114727.png
 
Yes EREL, but I think I forgot something like that in the code. I had never used ContentChooser before

[CODE lang="b4x" title="Wait for Chooser_Result"]Dim Chooser As ContentChooser
Chooser.Initialize("Chooser")
Chooser.Show("audio/*", "Choose audio file")
Wait For Chooser_Result (Success As Boolean, Dir As String, FileName As String)

If Success Then
Dim InStr As InputStream = File.OpenInput("ContentDir",FileName)
Dim OutStr As OutputStream = File.OpenOutput(File.DirInternal, SongName,False)
File.Copy2(InStr,OutStr)
OutStr.Close
Else
MsgboxAsync("open file error","")
End If[/CODE]

But I get an error message on line 4 : "B4XMainPage - 212: A parameter name cannot be the same as the name of a global variable."
[CODE lang="b4x" title="Sub PlayList_Click" highlight="2-16"]
Sub PlayList_Click
CLV1.Clear
PCLV.NumberOfSteps = 50
PCLV.lblHint.Font = xui.CreateDefaultBoldFont(20)
PCLV.lblHint.TextColor = xui.Color_Black
If FirstTime = True Then
EnterFolder(cStoragePath)
Else
Storage.SelectDir(True)
Wait For Storage_ExternalFolderAvailable
If (Storage.Root.IsFolder And Storage.Root.Name <> "")Then
EnterFolder(Storage.Root)
End If
End If
End Sub


Private Sub EnterFolder (folder As ExternalFile)
Dim n As Int
ListSongName.Initialize
FoldersStack.Clear
CLV1.Clear
If FoldersStack.Size > 1 Then
CLV1.AddTextItem("..", UpItem)
End If
For Each f As ExternalFile In Storage.ListFiles(folder)
n = n + 1
If f.IsFolder Then
Else If IsMusicFile(f.Name) Then
Dim cs As CSBuilder
cs.Initialize.Append(f.Name).PopAll
ListSongName.Add(f.Name)
Log (f.Name)
CLV1.AddTextItem(cs, f)
End If
Next
PCLV.Commit
PCLV.B4XSeekBar1.Value = CLV1.Size - 1
End Sub

Sub CLV1_ItemLongClick(Index As Int, Value As Object)
Dim suffix As String, ls As Int
'Dim Success As Boolean
SongName = ListSongName.Get(Index)
Storage.FindFile(Storage.Root,SongName)
ls = SongName.length
suffix = SongName.SubString2(ls-3, ls)
Log (suffix)
If suffix <> "mp3" Then
Chooser.Show("audio/*", "Choose audio file")
Wait For Chooser_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
Dim InStr As InputStream = File.OpenInput("ContentDir", FileName)
Dim OutStr As OutputStream = File.OpenOutput(File.DirInternal,SongName,False)
File.Copy2(InStr,OutStr)
OutStr.Close
Else
MsgboxAsync("open file error","")
End If
Else
Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName))
JukeBox.Player = Player1
Label1.Text = " Music Title: " & SongName
Player1.Play
End If
End Sub

Private Sub IsMusicFile (Name As String) As Boolean
Dim n As String = Name.ToLowerCase
For Each extension As String In Array("3gp", "mp3", "flac", "mid", "wav")
If n.EndsWith(extension) Then Return True
Next
Return False
End Sub

[/CODE]
 
Last edited:
Upvote 0
But I get an error message on line 4 : "B4XMainPage - 212: A parameter name cannot be the same as the name of a global variable."
This means that you have a global variable named Success, Dir or FileName.

Change the signature:
Code:
Wait For Chooser_Result (Success1 As Boolean, Dir1 As String, FileName1 As String)
 
Upvote 0
I made the changes. EREL, you were right. There is no more error message and when I do a long press on the name "Jambalaya.mid" for example, I get the display of the files saved in the chosen directory on my external storage, see screenshot:

Screenshot_20250309-145859.png
ContentChooserDisplay.png


So, thanks again EREL for your precious and always sharp help! I will be able to continue working on this project.
Kind regards.
​
 
Last edited:
Upvote 0
Back
Top