Audio Player in Webviewer not automatically playing

I have been trying to automatically play a wav file I generate in javascript in Webviewer but no success. The only way I can get the file to play is setting the attributes as below. I have the wav file image in blob b. I tried adding autoplay attribute but that did not work. I tried adding play(); but also no avail. It works fine on my windows PC but not on my android tablet. If I get rid of the controls, then it wont play. I would like to get it to play automatically.

const url = URL.createObjectURL(b);
var x = document.createElement("AUDIO");
x.setAttribute("src",url);
x.setAttribute("controls", "controls");
x.setAttribute("loop", "loop");
x.setAttribute("autoplay", "autoplay"); //tried to add this but no affect
document.body.appendChild(x);
x.play(); //does not start the play

The problem is that the autoplay attribute does not seem to have any affect when I add it. Adding x.play(); does not make it play. However if I add a button and put x.play(); in a function and call the function on an onclick event, then the button makes it play. It seems it needs to run the whole script and then load. I tried to put in a timer of 500ms that then runs the x.play(); but that does not work either.

Hello Bill

You are creating an HTML file containing your javascript? I wondering if you can save that file and then give the path to the App Inventor Player.

I am creating a HTML file with the javascript. The file is already stored as an asset in AI designer. I don't think the App Inventor Player will take a HTML file. The HTML file is for the Webviewer. I think there is an idiosyncrasy with the Webviewer and the audio player. I have a mf() function that alerts "mf" and has the Play command.
When I press the button, mf is alerted and then plays. When the timer fires, mf is alerted and it does not play. I even mad a trigger function to fire the button onclick. The timer fired the trigger function, which fired the mf function displaying "mf" but still did not play. Pressing the button displayed "mf" and played. So frustrating. Maybe there is a hidden onfocus that causes the webviewer to enable the player somehow. Don't know.

Since App Inventor uses the very least version of the WebView class, a lot of features are missing from the webviewer.
You should use any webview extension.

I can get it to work if I touch the page. That must trigger some sort of onfocus so that the play command works. How I can not trigger the onfocus by javascript to make it work? I can focus the button but that does not make the play work when I click the button via javascript. Because I create content in the javascript and then give it the src, the page has already loaded so the autoplay will not work. The x.play(); command does not work until there has been some user page interaction. If I could trigger the onfocus event, then maybe it will work. If you have ideas on how to do that, I appreciate your input.

You can try this extension:

Simply enable AutoPlayMedia and see what happens.

The CustomWebView worked. Thank you. One trap I fell into was that alerts did not come up and hung the program. When I removed the alerts, then it ran as expected. Yay!

Well almost was the solution. The version that I loaded was version 4. That worked but it did not allow me to remove the view so each time it increased the displayed page. Version 5,7,8 did not work for me at all. 5 would not even run the app. Version 7 and 8 gave i=me a consistant runtime error everytime I tried to create the viewer.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.