Android Question play audio file inside webview?

ilan

Expert
Licensed User
Longtime User
hi

i am trying to play a local audio file inside my webview but the file is not loaded.
i am able load the file from url but i want to allow the user to download the file and play it if he has not internet connection (offline play)

i use this in my webview:

HTML:
<!DOCTYPE html>
    <html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <head>
        <meta charset="UTF-8">
         <title>${title}</title>
    </head>
    <body>
        <div id="main">
            <img id="img" src="data:image/jpg;base64, ${base64image}"/>
            <div>
                <audio controls controlsList="nodownload" id="audioplayer">
                    <source src="${url}" Type="audio/mp3"> <------ HERE IS THE URL
                </audio>
            </div>
        </div>
    </body>
    </html>
    "$

i pass the url to the html code and try to play it.

the file url i get like this:
B4X:
        Dim url As String = xui.FileUri(File.Combine(xui.DefaultFolder,"mp3"),m.Get("filename"))
        Log("fileurl: " & url)

the log looks like this:

fileurl: file:///data/user/0/www.sagital.halachaan/files/mp3/9InfwtcS.mp3

but the player looks like it is not able to load the file (it is grey)

1731962578109.png

before i load the player i make sure that the file exists in the folder (xui.DefaultFolder)


if i load it from online url it works.

1731962607776.png


i read that it is not possible to access local files from webview but i dont think it is true since i can do it with images but audio files do not work. maybe the url is not correct?

thanx
 

JohnC

Expert
Licensed User
Longtime User
Is your app using the FileProvider (including manifest edits) so that it can properly share the file via a URI?


I think this post might relate to your issue:

 
Upvote 1

ilan

Expert
Licensed User
Longtime User
Is your app using the FileProvider (including manifest edits) so that it can properly share the file via a URI?


I think this post might relate to your issue:

thanx a lot using file provider worked for b4a.

now how do i use it for b4i?

thank you
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I have not tried to do this in a B4i app yet.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Upvote 0

ilan

Expert
Licensed User
Longtime User

hi erel

i tried the code in this thread but it seems like it doesnot work when the file is located in dirdocuments

this is the code i try:

B4X:
        If File.Exists(File.Combine(xui.DefaultFolder,"mp3"),m.Get("filename")) Then
        Dim url As String = "file://" & File.Combine(File.Combine(xui.DefaultFolder,"mp3"),m.Get("filename"))
        Log(url)

log:
file:///var/mobile/Containers/Data/Application/4CB9FCFC-BFEC-46AA-AC2A-37492333BB6B/Documents/mp3/J5vK0T9y.mp3

i tried in debug and release mode. both does not load the audio file.
for b4a i use file provider and it works fine.
i store the audio file in shared folder and load it from there but in b4i i am not able to load the file via html in my wenview.

any ideas what i am doing wrong?

thank you
 
Upvote 0
Top