20

I have a javascript that plays audio in the browser, using the html5 <audio> tag. It works fine in the iPhone browser, but not in Android. (Testing using a htc desire with android 2.1.) Anyone know why?

My code:

   function playHTML5(sound, soundcv){
                // sound = url to m4a audio file
                // soundcv = div in which the audioplayer should go

  var audio = document.createElement('audio');
  audio.src = sound;
  audio.controls = "controls";
  if (currentSound != null){
   soundcv.replaceChild(audio,currentSound);
  } else {
   soundcv.appendChild(audio);
  }
  currentSound = audio;
 }

By the way, I am also trying to enlarge the audio button that shows up in the iphone (the default one is quite small), with no luck so far - would be grateful for any ideas!

Pekka
  • 442,112
  • 142
  • 972
  • 1,088

9 Answers9

14

The latest Android browser in FroYo does not yet support the HTML5 audio element. This is not a bug, rather a feature that has yet to be implemented. It may come in Gingerbread.

Update: The Gingerbread browser has the audio element.

Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58
  • 2
    http://html5test.com shows no support for PCM, MP3, AAC, Ogg Vorbis or WebM audio in my FroYo Browser. – Pierre-Antoine LaFayette Jul 21 '10 at 17:05
  • 1
    Thanks for your answer. But if this is not a bug, then the documentation is obviously unclear, to put it mildly - it should state clearly that audio is not supported. – Anders Sundnes Løvlie Aug 11 '10 at 15:02
  • How is the documentation unclear? No browser supports html5 100% at the moment. You don't expect them to document everything they don't support that is coming down the w3c pipeline? The audio tag is a new feature that the latest browsers have just begun to adopt. Just because the iPhone supports it doesn't make it a bug on Android. – Pierre-Antoine LaFayette Aug 11 '10 at 17:05
  • I was going to give you a quote from the documentation - but then I remembered that I never really found any documentation at all. So I guess the precise wording should be that the documentation is either non-existing, or very hard to find. Speaking about documentation for the android browser, that is - not the android app platform. However, if you do know where to find that documentation, I would be extremely grateful if you could pass me the link! – Anders Sundnes Løvlie Aug 19 '10 at 17:43
  • You can find documentation for the WebKit engine itself but the Android port's code is its documentation. It would be nice to have documentation but sometimes when the code is changing so fast it is a waste of effort to try and maintain docs. It's not like the public interface to the WebView which pretty much has to be well documented. – Pierre-Antoine LaFayette Aug 19 '10 at 20:14
  • 1
    well, could be. at least that gives the answer to the question you asked - "how is the documentation unclear?" Personally I think web apps should be the future - rather than native apps - and I would think google looks favorably on that view... if so, they should offer web developers the same level of documentation they offer devs of native apps, in my view. – Anders Sundnes Løvlie Aug 23 '10 at 21:26
  • Droid Does™. If only that were true! – jocull Dec 16 '10 at 18:13
13

Here is link to Google Android bug, which filed for lack of support of AUDIO tag in Android. Please, vote on it.

http://code.google.com/p/android/issues/detail?id=10546

BTW, there is work around, which let you play natively MP3 in Android 1.6 .. 2.2, like that:

<video src="test.mp3" onclick="this.play();"></video>
Slavik
  • 131
  • 1
  • 3
  • 2
    Wow. Really, Google? It works if you put it in a video tag. How badly did they screw this up? – Judah Gabriel Himango Dec 09 '11 at 21:43
  • I am running Android 2.3.6 on my Samsung Mini but I still couldn't get my little mp3 to play using the video tag nor the audio tag. I tested it in chrome and it works. Can you confirm video tag work-around? Any other reading you can link to about it? Thanks :) – Reinsbrain Nov 16 '12 at 18:42
4

I cannot figure this out either. BUT, this test page created by Apple plays HTML5 audio on the Android: http://www.apple.com/html5/showcase/audio

how did apple do it?

Rick
  • 41
  • 1
1

I was having trouble on my Kindle Fire, and found one small thing which seemed to fix it.

<audio>
  <source src="someclip.mp3" type="audio/mpeg" />
</audio>

I had mistakenly been using "audio/mp3". I'm using a player without its native controls (using HTML5/JS).

Kevin
  • 11
  • 1
1

I got this working on a project using trigger.io deploying to a Nexus 7 running Android Jellybean, but the audio only plays if I use absolute URLs at the moment. I'm not sure what the relative path is yet to use audio from within my project, but hopefully this will help somebody out...

(function() {

    var currentSound;
    // I have a div called "page"
    var soundcv = document.body;

    // This only works for one sound for simplicity's sake
    function playSound(sound){

        if (currentSound == null){
            var audio = document.createElement('audio');
            audio.src = sound;
            soundcv.appendChild(audio);
            currentSound = audio;
        }

        currentSound.play();
    }

    // This isn't a real audio file URL - replace it with one of your own
    playSound('http://example.com/example.mp3');
})();
gavD_UK
  • 395
  • 5
  • 11
  • 1
    Amir from Trigger.io got in touch with me and he solved this issue - if you use createElement('video') then it all works! Take a look at [the mobile game Soft Paws, tag 0.1, on my Github](https://github.com/gavD/soft-paws/tree/v0.1) which runs in Trigger.io and features sound – gavD_UK Aug 23 '12 at 21:55
1

Have you checked your audio format ?

Jla
  • 11,304
  • 14
  • 61
  • 84
  • Yes, I am using the exact same media files in a native app that runs fine on android. 3gp audio, part of the core media files. So unless these for some reason can not be played in the html5 tag, even though they can be in native apps, that should not be the problem? – Anders Sundnes Løvlie Jun 18 '10 at 12:59
  • Hm, seems you are right though: There may be a difference between what formats android supports in general, and which it supports in the audio tag: http://www.w3schools.com/html5/html5_audio.asp – Anders Sundnes Løvlie Jun 18 '10 at 13:15
  • @anderslov Have you tried on different browsers. I don't know android much so I'm not sure what options you have. – Jla Jun 18 '10 at 13:46
  • I ended up setting up a test page to find out about the formats, and concluded that support for the audio element in the browser is simply broken. Details here: http://stackoverflow.com/questions/3100706/what-audio-formats-are-supported-by-the-android-browser – Anders Sundnes Løvlie Jun 28 '10 at 08:26
0

I've been tinkering all day on this and I finally got mine to work on my old Samsung Droid browser with the yahoo webplayer:

<a href="somefolder/file.mp3"></a>
<script type="text/javascript" src="http://webplayer.yahooapis.com/player.js"></script>

Works great puts a little arrow icon next to the text that you can click to play your mp3. Thanks Yahoo.

I forgot to mention that the audio does NOT show up in my Chrome or Firefox browser when testing this locally. Works great on my phone browser though.

Robert Beltran
  • 495
  • 3
  • 9
0

I know this is a hack, but it works everywhere (as far as I know)!

You can use the video element and convert your mp3s into mp4 and ogg files (ogg for firefox on android). You could also style it so it doesn't display any uneccesary default players. See code below:

<video id="audioTest" width="1" height="1" style="top: -100px; left: -100px; position: absolute;" preload >
    <source src="audio.mp4" type="video/mp4">
    <source src="audio.ogv" type="video/ogg">
</video>

You could then play it in your js code using:

var audioTest = document.getElementById("audioTest");
audioTest.addEventListener("ended",onSoundComplete,false);
audioTest.play();
Nikos
  • 1
  • does the preloading work in this case? my tests seem to indicate that it does not preload the mp3 properly when i use mp3 inside of the video tag – gaugau Nov 19 '19 at 16:00
0

in body

<audio id="myAudio" src="audio/bomba.mp3"></audio>

in javascript during the event

document.getElementById('myAudio').play();