Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Audio in Android Webview

Tags:

java

html

android

I am a beginner in HTML and Java and I am attempting to play audio files in a simple web app on the android through the Web view using HTML and Java. I have succeeded in getting this to work on Android 4.0. It however will not work on 2.3.3 or 2.2. I've done a lot of researching and so far all I have found is that it is not supported. Can anyone confirm or deny this and possibly point me in the right direction? Here is what I have working on 4.0 but nothing else.

WebView engine = (WebView) findViewById(R.id.web_engine);

String audioTest =  "< html >" + <br/>
                "< body >" + <br/>
                "< audio id='egSound' preload='auto'  autoplay ='autoplay'>" + <br/>
                "< source src=' www.egSoundUrl.com '>" +                    
                "< /audio>" + <br/>
                "< /body>"  +  
                "< /html>";

engine.loadData(audioTest, "text/html", null);

How can I get the same code to work for 2.3.3? In my research I came across someone who was able to play the audio file using the video tag, but I was unable to get that working. Any shared knowledge will be greatly appreciated.

like image 584
CRhea Avatar asked May 10 '12 19:05

CRhea


2 Answers

Yep. Sadly HTML5 audio element is supported only from Gingerbread.

I read that the audio tag IS supported someway in 2.2 & 2.3, but for some very weird reason the mp3 & ogg codecs are not. Therefore, even if it's supported is useless on phones up to Android 2.3.

SOLUTION 1 The video tag workaround (1.6 to 2.2) is the following:

<video src="soundfile.mp3" onclick="this.play();"></video>

is the one you tried?

SOLUTION 2 Another possible solution (2.2+) is to implement a JQuery Player with Flash fallback, like UbaPlayer.

SOLUTION 3 Lastly.. there's a java workaround by another user of StackOverflow, here:

  • HTML5 audio tag on Android - answer by mdelolmo

..Hope this helps :)

like image 186
VoT Avatar answered Oct 21 '22 01:10

VoT


Just a quick one-liner for any new visitors - try using the following inside your webview settings code:

mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

It successfully fixes the audio playback problem!

like image 39
Username Avatar answered Oct 21 '22 00:10

Username