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

Discussion in 'Android Questions' started by LucaMs, May 23, 2019.

  1. LucaMs

    LucaMs Expert Licensed User

    I tried:

    Code:
    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:(
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Use ExoPlayer.
     
  3. LucaMs

    LucaMs Expert Licensed User

    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: May 24, 2019
  4. José J. Aguilar

    José J. Aguilar Active 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.
    Code:
    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:
    Code:
    function play(){
           var audio = document.getElementById(
    "audio");
           audio.play();
                     
    }
    And in the html part: (to test if the mp3 format was a problem)
    Code:
    <audio id="audio" src="http://dev.interactive-creation-works.net/1/1.ogg" ></audio>
    I've added to Button1_Click:
    Code:
    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.
     

    Attached Files:

    LucaMs likes this.
  5. LucaMs

    LucaMs Expert Licensed User

    Thank you, José, I (or the person interested) will try your test.
     
  6. José J. Aguilar

    José J. Aguilar Active Member Licensed 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
     
    LucaMs likes this.
  7. LucaMs

    LucaMs Expert Licensed User

    Tried.
    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');
     
  8. LucaMs

    LucaMs Expert Licensed User

    :D
     
  9. José J. Aguilar

    José J. Aguilar Active Member Licensed User

    Ok, I will try later. I used that "Playing" alert to know if the script was called. My mobile runs Android 7.1.1
     
  10. LucaMs

    LucaMs Expert Licensed User

    Do you know how you can, alternatively, download the audio file?
    Also this test did not work:
    Code:
    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!
     
  11. José J. Aguilar

    José J. Aguilar Active 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?
     
  12. José J. Aguilar

    José J. Aguilar Active 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.
     

    Attached Files:

    LordZenzo and LucaMs like this.
  13. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    This is another shorter way to play audio.
     

    Attached Files:

  14. LucaMs

    LucaMs Expert Licensed User

    Thank you, "solved" so:

    Code:
    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 and José J. Aguilar like this.
  15. LordZenzo

    LordZenzo Well-Known Member Licensed User

    nice
    very useful
     
    José J. Aguilar and LucaMs like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice