Android Question Playing a MP3 file from External Storage

ac9ts

Active Member
Licensed User
Longtime User
I'm trying to figure out the best way to play a MP3 file located on my SDCard. This will be just for my use so the directory structure is known.

The directory structure is:

SDCard Root/
Music/
Artist/
Album/
Song.mp3

I'm currently using the ExternalStorage class and I can drill through the directories to get the ExternalFile for the selected Song.mp3.

My questions:

How can I use this to play the file (in either ExoPlayer or MediaPlayer)?

Would it be easier to just copy the SDCard file to an internal directory and play it from there?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private storage As ExternalStorage
    Private player As SimpleExoPlayer
End Sub

Public Sub Initialize
    
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    storage.Initialize(Me, "storage")
    player.Initialize("exoplayer")
End Sub


Sub Button1_Click
    storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable
    Dim music As ExternalFile = storage.FindFile(storage.Root, "2.mp3")
    If music.IsInitialized Then
        Dim uri As String = music.Native.RunMethodJO("getUri", Null).RunMethod("toString", Null)
        Log(uri)
        player.Prepare(player.CreateUriSource(uri))
    End If
End Sub

Sub ExoPlayer_Ready
    Log("ready")
    player.Play
End Sub

Sub ExoPlayer_Error (Message As String)
    Log("error: " & Message)
End Sub
 
Upvote 0
Top