Playing html5 audio in android browser - javascript

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!

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.

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>

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?

Have you checked your audio format ?

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).

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');
})();

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:
<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.

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();

in body
<audio id="myAudio" src="audio/bomba.mp3"></audio>
in javascript during the event
document.getElementById('myAudio').play();

Related

How can I speed up html audio in Safari?

I'm playing audio using the HTML audio player, and I've noticed that Safari is very slow to load and play the audio (about 5.5 seconds vs. .5 seconds for Chrome). Is there a way I can speed this up?
I've seen discussion (like here and here) about AudioContext as a possible solution, but I'm not clear on how AudioContext works--what would be the code to implement it? I want to keep the html audio tag, bc of its features.
[EDIT: Actually, having seen the way that safari changes the look of the audio player on different devices, I'm ok just playing the audio through javascript (audio.play()...). But it's still slow on safari either way.]
(I'm using angular, but I don't think that makes a difference for this purpose)
relevant code:
html:
<audio controls id="audioId" (loadeddata)="onCanPlayFunction()" >
<source src="{{coolAudio}}" type="audio/mpeg">
Your browser does not support the audio player.
</audio>
typescript:
playAudio() {
this.coolAudio = 'audioFile.m4a'
}
onCanPlayFunction() {
let audioElement = <HTMLVideoElement>document.getElementById('audioId');
audioElement.play()
}

A-Frame Audio not working in VR-mode only

I'm currently working on a small project with a friend of mine. We're playing around with a VR simulation that requires audio instructions. Everything seems to work fine in the web version. The web version also works on mobile devices. However, when you enter VR-mode on any mobile device the audio doesn't play.
We have tried chrome, safari and firefox. None of them seem to work. We have tried different versions of A-Frame but that hasn't worked either. No errors are given, so I have no idea what could be wrong.
This is what we currently have in our index.html:
<audio id="js--instructie2" controls>
<source src="objects/instructie2.mp3" type="audio/mp3">
</audio>
<audio id="js--instructie3" controls>
<source src="objects/instructie3.mp3" type="audio/mp3">
</audio>
as for our main.js:
var instructie1 = document.getElementById("js--instructie1");
var instructie2 = document.getElementById("js--instructie2");
var instructie3 = document.getElementById("js--instructie3");
var instructie4 = document.getElementById("js--instructie4");
var instructie5 = document.getElementById("js--instructie5");
var instructie6 = document.getElementById("js--instructie6");
var uitlegArray = [instructie1, instructie2, instructie3, instructie4, instructie5, instructie6];
uitlegArray[uitleg].play();
I thought that Audio would work fine in VR-mode given the fact everything works perfectly in the webversion. But appearently it's not that simple.
For everyone having the same issue: Try out the howler plugin. I started trying out different methods and finally solved the issue. I eventually ended up having to use howler.min.js . It's basically an audio library. Still have no idea how that resolved the issue though, so I'm sorry but I can't provide an answer on why it didn't work to begin with.
You can read more about it on their website:
https://howlerjs.com/

HTML 5 AudioElement won't play mp3 Livestreams in safari on IOS 11 devices

I'm a web developer at a radio broadcaster. Since the release of IOS 11, we received several user complaints that our audio live streams can't be played on IOS 11 devices anymore. To embed the streams in our websites we use the HTML5 AudioElement. When debugging the javascript on an iPhone whit IOS 11 we recognized that calling the audio elements play() method resulted in a MediaError of ErrorCode 4 (MEDIA_ERR_SRC_NOT_SUPPORTED). All other devices (Android, Windows and IOS 10 and below) play the streams without any problem.
I created a little codepen example
<audio controls>
<source src="http://hr-hrinfo-
live.cast.addradio.de/hr/hrinfo/live/mp3/128/stream.mp3"
type="audio/mpeg;codecs="mp3"">
Your browser does not support the audio element.
</audio>
https://codepen.io/ampersand83/pen/pWwgKm in which I just create an AudioElement via the AudioTag and hand the source Tag one of our stream URLs.
Devices running IOS 10 and below play the streams without any problem as well as current android or windows devices. However, a device running IOS 11.0.1 can't play the stream. I can't find any information on why this wouldn't be possible anymore. Does anybody have an idea why our streams won't work anymore and can give us advice on what we can do to make them work again?
This issue is down to Apple WebKit changing in the latest iOS release. For users running KH Icecast a fix can be expected soon (hopefully!) https://github.com/karlheyes/icecast-kh/issues/172
Adding a simple audio control like this
<audio controls>
<source src="https://swr-swr3-live.sslcast.addradio.de/swr/swr3/live/mp3/128/stream.mp3" type="audio/mpeg;codecs="mp3"">
</audio>
…does not play.
While this one does:
<audio controls>
<source src="http://mp3-live.swr3.de/swr3_m.m3u">
</audio>
The only difference I can spot is that on an MP3 it fails while a M3U playlist works.
On iOS 10 and below as well as on current macOS Safari, it works both.
Also Chrome on iOS 11 fails (same Webkit engine?!)
I saw this same issue when trying to load a local HTML file with audio controls into a UIWebView. However, my file was an .m4a audio file. I found that just removing type did the trick. So my HTML looked like this:
<div class="audio"><audio controls><source src="my_local_audio_file.m4a"></audio></div>
Using type=audio/mp4 also worked for me. So I'm guessing it's an incompatible mime type and just removing the type would be the best option.
Hope this helps.

Play audio tag with javascript in firefox/internet explore/chrome

http://www.storiesinflight.com/html5/audio.html
I'm trying to accomplish a sound script from above link, which should play a audio file. The article author claim it's compatible in firefox. But it's not playing in mine. It does however works in Chrome. Tried internet explore and not luck there either.
Are there any obvious reasons for this?
<audio id="audiotag1" src="path.wav" preload="auto"></audio>
Play 5-sec sound on single channel
<script type="text/javascript">
function play_single_sound() {
document.getElementById('audiotag1').play();
}
</script>
IE 11 does not support .wav files, so if you want to support IE you'll have to use either .mp3 or .ogg. As for why it didn't work in Firefox, a problem might be that .wav files are very large so depending on when you clicked on the link the audio file may not have loaded yet.
It's better to use the canplay event so you know when the audio file has loaded and can be played.
var audio = document.getElementById('audiotag1');
audio.addEventListener('canplay', function() {
audio.play();
});
Which version of IE are you testing it on?
IE often lags in terms of support for features.
Check out this chart. Maybe your version of IE doesn't have support for audio.
UPDATE
Adding this meta tag might help
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
Installing MediaPack might help too
Were using .wav, tried with .mp3 and it works in all three browsers.

Playing HTML5 Videos in Browser -- What Works for Most Android Devices?

I'm trying to get HTML5 videos to play on mobile devices. They seem to run fine on the latest version of iOS, but I'm getting a lot of inconsistency with Android devices.
I'm using video.js and listening for a click on a thumbnail image, which in return replaces the element with a HTML5 video and plays it automatically. The following code flat out doesn't work on the Android emulator (clicking on a thumbnail does nothing). When I try it on my own Droid Razr, it begins to load the video and then the browser freezes up. This happens in the native browser and Chrome, which tells me it's something native to the device.
$(".video").live("click", function(e) {
e.preventDefault();
$(this).replaceWith("<video id='" + $(this).data("video-id") + "' class='video-js' preoload='auto' width='100%' height='100%' poster='" + $(this).data("video-poster-url") + "'><source type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"' src='" + $(this).data("video-url") + "'></video>");
video = _V_($(this));
video.ready(function() {
this.play();
this.requestFullScreen();
});
});
The HTML ends up being:
<video id='fv3530' class='video-js' preoload='auto' width='100%' height='100%' poster='/posters/fv3530.jpg'>
<source type='video/mp4; codecs="avc1.42E01E, mp4a.40.2" src='/videos/fv3530.mp4'>
</video>
Does anybody know why this would cause an Android device to totally freeze up, and what I can do to make videos run consistently on most Android devices?
Thanks!
Please se this question: HTML5 <video> element on Android
Apparently, there is a possibility to use HTML5 videos with android, according to http://developer.android.com/about/versions/android-2.0-highlights.html
But there are some strict parameteres in order to make this work (see checked response to the question).
In your case, It is not easy to handle events with the emulator, especially html5 etc, so you should allways use a real device for testing (really recommanded). In which concerns your phone, the possibly reason why it is not working is that yu are using a non supported video codec.
Remove the type attribute from the source declaration. This usually causes issues with Android, older versions especially.
Your source declaration should simply be:
<source src='/videos/fv3530.mp4'>
I have a small site which has a working HTML5 video on Android (just tested it on my HTC Desire running Android 2.2).

Categories

Resources