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

LucaMs

Expert
Licensed User
Longtime 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:(
 

josejad

Expert
Licensed User
Longtime 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

  • Test.zip
    57.4 KB · Views: 309
Upvote 0

josejad

Expert
Licensed User
Longtime User
Ok, that's the only time I've used WebViewExtras2, but if both of you have some problem I will try to test it in detail
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Tried.
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

On my Huawei :( - Android 7.0 it does not play. This dialog appears:

upload_2019-5-24_14-7-24.png



P.S. saw: alert('Playing');
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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!
 
Upvote 0

josejad

Expert
Licensed User
Longtime 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?
 
Upvote 0

josejad

Expert
Licensed User
Longtime 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

  • AudioTest.zip
    9.8 KB · Views: 250
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0
Top