Android Question (Resolved ) Why are some MP3 files "accepted" by the application and others not?

Marc DANIEL

Well-Known Member
Licensed User
Long press on the name of an MP3 file

Player1.Prepare:
If File.Exists(File.DirInternal,SongName) = True Then
            Dim sources As List
            sources.Initialize
            sources.Add(Player1.CreateFileSource(File.DirInternal, SongName))
            Sleep(5000)
            Player1.Prepare(Player1.CreateListSource(sources))
            JukeBox.Player = Player1
            Label1.Text = " Music Title: " & SongName
            Player1.Play

OR

Other method:
Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName))
            JukeBox.Player = Player1
            Label1.Text = " Music Title: " & SongName
            Player1.Play
 
Last edited:

Marc DANIEL

Well-Known Member
Licensed User
Nothing happens, no error message, the file name is displayed, but the file isn't loaded and played by Player1.
In fact, I want to understand why "Sample.mp3" is loaded and played but not "Primavera.mp3."


 
Last edited:
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
Sub CLV1_ItemLongClick:
Sub CLV1_ItemLongClick(Index As Int, Value As Object)
    Dim MusicGenre As Int
    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 = " & suffix)
    Log ("File Name = " & SongName)
   
    If SongName.EndsWith(".mp3") Or SongName.EndsWith(".MP3")Then MusicGenre = 1
    If SongName.EndsWith(".wav") Or SongName.EndsWith(".WAV")Then MusicGenre = 2
    If SongName.EndsWith(".flac") Then MusicGenre = 3
    If SongName.EndsWith(".mid") Then MusicGenre = 4
    If SongName.EndsWith(".gp3") Then MusicGenre = 5
   
    Select MusicGenre
        Case 1
            'Playing the sound files in the application
            Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName))
            JukeBox.Player = Player1
            Label1.Text = " Music Title: " & SongName
            Player1.Play
           
        Case 2
            'Playing the sound files in the application
           
            If File.Exists(File.DirInternal,SongName) = True Then
                Dim sources As List
                sources.Initialize
                sources.Add(Player1.CreateFileSource(File.DirInternal, SongName))
                Sleep(5000)
                Player1.Prepare(Player1.CreateListSource(sources))
                JukeBox.Player = Player1
                Label1.Text = " Music Title: " & SongName
                Player1.Play
            End If
            'Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName))
            'JukeBox.Player = Player1
            'Label1.Text = " Music Title: " & SongName
            'Player1.Play
                       
        Case Else
                       
            'Play sound files outside the app through your own devices
           
            Chooser.Show("audio/*", "Choose audio file")
            Wait For Chooser_Result (Success1 As Boolean, Dir1 As String, FileName1 As String)
            If Success1 Then
                Dim InStr As InputStream = File.OpenInput("ContentDir",FileName1)
                Dim OutStr As OutputStream = File.OpenOutput(File.DirInternal, SongName,False)
                File.Copy2(InStr,OutStr)
                OutStr.Close
            Else
                MsgboxAsync("If your music file is not playing," & CRLF & "you can open it directly outside" & CRLF & "this application by opening the"& CRLF & "folder chosen by you in the" & CRLF & "external storage","Advice")
            End If
       
    End Select
   
End Sub

The code is still there, dear Klaus
 
Last edited:
Upvote 0

Marc DANIEL

Well-Known Member
Licensed User
I think it has to do with the MP3 file type, there must be some significant difference that explains the difference in processing between the two files chosen as examples. I just wanted to know if there is anything I can do to fix this problem.

 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
The code is still there, dear Klaus
Yes, i downloaded the project again and now i find the code.
The Sub CLV1_ItemLongClick(Index As Int, Value As Object) is a bit different than the code in post #6.
But, anyway, the problem is in this line:
Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName))
In the project you never copy any file to File.DirInternal, therefore the file does not exist and is not played.
It seems that SimpleExoPlayer, if a file does not exit, just ignores it without any error message.
You probably once copied the file Sample.mp3 to File.DirInternal and of course it is found.
When i tested the project i added some mp3 files to the external storage and nothing happened even Sample.mp3.

Adding the code below just before Player1.Prepare(Player1.CreateFileSource(File.DirInternal,SongName)) works.
The code copies the file from the external storage to File.DirInternal.
I am not sure if this is what you want, having with time a lot of files in File.DirInternal.
Maybe you could remove the files at some point, when you exit the program for example.

B4X:
If File.Exists(File.DirInternal, SongName) = False Then
    Dim iStream As InputStream, oStream As OutputStream
    Dim oFile As ExternalFile

    oFile = Storage.FindFileOrCreate(Storage.Root, SongName)

    If oFile.IsInitialized Then
        iStream = Storage.OpenInputStream(oFile)
        oStream = File.OpenOutput(File.DirInternal, SongName, False)
        File.copy2(iStream, oStream)
        iStream.Close
        oStream.Close
    End If
End If

Maybe there are other means to access files from the external storage but i have never used it and have no experience with it.
 
Upvote 3

Marc DANIEL

Well-Known Member
Licensed User
Fantastic Klaus, I made the change you recommended and it works great!!!
You're the best, Klaus!

And what's more, Klaus, your system also accepts "FLAC" files in addition to "MP3" and "WAV"!
Only "MID" files are not recognized and therefore not played, but in this case, I use the "Chooser Show" procedure to play these files with the smartphone's personal devices.

New files >>> B4A files - APK file
 
Last edited:
Upvote 0
Top