Html5 audio on native apache cordova android application

Does the sound tag work on my own Android application?
I’ve been looking for some time, but I can’t find a message that tells me whether it is supported or not, or what I need to do to make it work.

<audio id="Multimedia1" controls="controls" width="100%" height="30px"> <source src="file:///android_asset/www/Resources/audio.mp3"> Your browser does not support audio. </audio> 

It does not download audio. I tried different ways, for example:

 Resources/audio.mp3 www/Resources/audio.mp3 file:///android_asset/www/Resources/audio.mp3 

If I view index.html with a browser (no native application), the sound is played with a relative path.

But I just can't get it to work with apache cordova app.

Any ideas? Thanks in advance!

+4
source share
2 answers

I had the same problem with audio tags from HTML5 in Cordova 2.3.0. What I ended up with is simply reproducing them using JavaScript:

Here audio.js is placed in the js folder:

 function playAudio(src) { src = '/android_asset/www/' + src; var media = new Media(src, success, errorThrown); media.play(); } function success() {} function errorThrown(e) { alert('Error while playing the sound!'); } 

And then in your HTML code you:

 <script src="js/audio.js"></script> 

And to play the sound, add this to the <script> block:

 playAudio("media/your_sound.wav"); 
+3
source

Same problem: I found that the problem is that in order to use the .play () function in HTML5 audio, it must be run inside a user interface event handler (for example, a click), as described here:

https://code.google.com/p/chromium/issues/detail?id=178297

This is a common policy in mobile browsers to try to avoid sites that will automatically launch material and record data. This https://gist.github.com/blairvanderhoof/9380545 can be used to configure the web view used by the application to get around this. I am working on a plugin that will change this behavior during installation.

-Mike

+2
source

Source: https://habr.com/ru/post/1491737/


All Articles