Android Question WebViewExtras2 - how to play an online audio file?

LucaMs

Expert
Licensed User
I tried:

B4X:
   Dim url As String
url = "https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3"

Dim jscript As StringBuilder
jscript.Initialize
jscript.Append($"var audio = new Audio('${url}');"$)
jscript.Append(CRLF)
jscript.Append("audio.play();")
Log(jscript.ToString)

WebViewExtras1.ExecuteJavascript(jscript.ToString)
without success... of course :D:(
 

LucaMs

Expert
Licensed User
Use ExoPlayer.
Given that it's not something I need, the b4x developer needs to play the audio file from within a WebViewExtras2, as he can access the audio file only after he has logged in.
 
Last edited:

José J. Aguilar

Well-Known Member
Licensed User
Hi LucasMs:

Take a look to this sample.

https://www.b4x.com/android/forum/t...ring-with-jsignature-and-webviewextras.96451/

I have no time right now to test (I'm at work, shhh), but I've added your code after my code in Button2_Click.
B4X:
Sub Button2_Click
    WebViewExtras1.ExecuteJavascript("$('#signature').jSignature('reset');")
    Label1.Text = "Base30"
    ImageView1.Bitmap = Null
   
    Dim url As String
    url = "https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3"

    Dim jscript As StringBuilder
    jscript.Initialize
    jscript.Append($"var audio = new Audio('${url}');"$)
    jscript.Append(CRLF)
    jscript.Append("audio.play();")
    Log(jscript.ToString)

    WebView
I've added in the html the javascript function:
B4X:
  function play(){
       var audio = document.getElementById("audio");
       audio.play();
                 }
And in the html part: (to test if the mp3 format was a problem)
B4X:
<audio id="audio" src="http://dev.interactive-creation-works.net/1/1.ogg" ></audio>
I've added to Button1_Click:
B4X:
    Javascript="B4A.CallSub('Play', True, "")"
    WebViewExtras1.ExecuteJavascript(Javascript)
Well, If I press buttons, it doesn't sound, but if I sign, press the "Undo last stroke" and then press button2, it sounds the mp3. So it seems what is working is your code. Your turn to test and know why ;).
I attach the project.
 

Attachments

LucaMs

Expert
Licensed User
Do you know how you can, alternatively, download the audio file?
Also this test did not work:
B4X:
    Dim jscript As StringBuilder
   jscript.Initialize
   jscript.Append("function downloadURL(url, name)" & CRLF)
   jscript.Append("{" & CRLF)
   jscript.Append($"var link = document.createElement("a");"$ & CRLF)
   jscript.Append("link.download = name;" & CRLF)
   jscript.Append("link.href = url;" & CRLF)
   jscript.Append("link.click();" & CRLF)
   jscript.Append("}" & CRLF)
   jscript.Append($"downloadURL(${url},${name});"$ & CRLF)
   Log(jscript.ToString)
   WebViewExtras1.ExecuteJavascript(jscript.ToString)


P.S. UM... I "suspect" that the last command, "downloadURL..." should be written in a HTML page!
 

José J. Aguilar

Well-Known Member
Licensed User
I’m still working but, watching my example, why don’t you try to write the javascript in the html file, test it in a browser, and once it’s working try to call from B4A?
 

José J. Aguilar

Well-Known Member
Licensed User
Hi again:

I've made a small example from the top. If you press the B4A button, the sound it's not played. But if you press the play html button firts, then the B4A button always works.
So it seems that the sound is downloaded or something like that once you play it.
 

Attachments

LucaMs

Expert
Licensed User
Thank you, "solved" so:

B4X:
Sub btnPlay_Click
    Dim sbURL As StringBuilder
    sbURL.Initialize
   
    sbURL.Append("<!DOCTYPE html>")
    sbURL.Append("<html>")
    sbURL.Append("<body>")

    sbURL.Append("<script>")
    sbURL.Append("function downloadURL(url, name){")
    sbURL.Append($"    var link = document.createElement("a");"$)
    sbURL.Append("    link.download = name;")
    sbURL.Append("    link.href = url;")
    sbURL.Append("    link.click();")
    sbURL.Append("}")
    sbURL.Append($"downloadURL('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3', 'miofile');"$)
    sbURL.Append("</script>")

    sbURL.Append("</body>")
    sbURL.Append("</html>")
   
    WebView1.LoadHtml(sbURL.ToString)
End Sub
 

LordZenzo

Well-Known Member
Licensed User
Hi again:

I've made a small example from the top. If you press the B4A button, the sound it's not played. But if you press the play html button firts, then the B4A button always works.
So it seems that the sound is downloaded or something like that once you play it.
This is another shorter way to play audio.
Thank you, "solved" so:

B4X:
Sub btnPlay_Click
    Dim sbURL As StringBuilder
    sbURL.Initialize
 
    sbURL.Append("<!DOCTYPE html>")
    sbURL.Append("<html>")
    sbURL.Append("<body>")

    sbURL.Append("<script>")
    sbURL.Append("function downloadURL(url, name){")
    sbURL.Append($"    var link = document.createElement("a");"$)
    sbURL.Append("    link.download = name;")
    sbURL.Append("    link.href = url;")
    sbURL.Append("    link.click();")
    sbURL.Append("}")
    sbURL.Append($"downloadURL('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3', 'miofile');"$)
    sbURL.Append("</script>")

    sbURL.Append("</body>")
    sbURL.Append("</html>")
 
    WebView1.LoadHtml(sbURL.ToString)
End Sub
nice
very useful
 
Top