Fixing HTML5 Audio Problems in iOS and Android Mobile Browsers to Overcome the Limitations
In one in all my earlier posts I mentioned a number of the points (or limitations) with HTML5 audio help in iOS and Android cell and pill platforms. In this text I’m going to strive to try a number of the methods we are able to overcome these limitations and quirks.
Swap Out Audio Source
We mentioned the issue of single audio stream limitation the place you may play from a number of sources. This may be mounted by swapping out the present audio supply for one more when required:
What’s the one factor each developer needs? More screens! Enhance your coding expertise with an exterior monitor to extend display screen actual property.
var audio = doc.getElementById('music');
audio.play();
// Sometime Later
audio.src = 'music_2.mp3';
audio.play();
Note: There might be a slight delay initially whereas the brand new audio file hundreds! Hence this ain’t a clear resolution actually, however a hack. There’s a extra acceptable hack to repair this preliminary delay downside together with the limitation referred to as Audio Sprites.
Audio Sprites
You could already learn about CSS Sprites the place you mix all of your photos right into a single picture after which use them with correct pre-defined background positions for varied parts (backgrounds for html parts) in your webpage. Similarly you may mix all of your audio information right into a single sprite with a 1 second hole/pause after every half. You’ll have to make an observation of the beginning and finish instances of every half (or simply the beginning time and the size) in a JS object after which play them as required.
When the time that equals to the size of the monitor has elapsed, simply pause the audio. It requires a easy logic, both use a setTimeout or add a timeupdate occasion handler.
// tracks object
var tracks = {
normal_jump: {
from: 0, to: 0.4
},
super_jump: {
from: 1, to: 2.2
},
space_belt: {
from: 3, to: 6.6
},
enemy: {
from: 7, to: 9
},
game_over: {
from: 10, to: 12.1
}
};
// identify is the audio half identify
var identify = 'normal_jump';
// following code may be inside a operate
var monitor = tracks[name];
audio_sprite.currentTime = monitor.from;
audio_sprite.play();
setTimeout(operate() {
audio_sprite.pause();
}, (monitor.to * 1000) - (monitor.from * 1000));
// or use timeupdate
var handler = operate() {
if (this.currentTime >= identify.to)
this.pause();
};
audio_sprite.addEventListener('timeupdate', handler, false);
Again, this ain’t clear and you continue to can not play a number of sounds or a number of elements concurrently which is a typical requirement in video games. For instance, at a specific cut-off date it’s possible you’ll want to supply the sounds for participant actions, enemies taking pictures and the participant grabbing a powerup, on the similar time.
Autoplay Issue
There’s no repair to this subject, not even on iOS 6. You have to attend for consumer interplay (contact, click on, and so on. occasions) with a view to begin enjoying audio. Maybe you may current some choices on the sport splash display screen the place the consumer can choose (with a click on or faucet) whether or not to start out enjoying audio or not.
Trying out AppCache
Decided to offer HTML5 AppCache a shot to strive repair audio delay points on ios and android.
On iOS 5 and 6 (chrome) and Jelly Bean (inventory browser and chrome) there are delays initially once you play an audio file. But with appcache the problem is totally eradicated. So superior! Although, once you load an audio for the primary time in iOS Safari, the preliminary delay might be seen. Hence, the issue is just not solely solved. Infact generally the audio doesn’t begin enjoying in any respect, regardless of what number of instances you hit the play button (in all probability as a result of it’s not downloaded or one thing comparable) which received’t result in save something in appcache. So till the audio is downloaded solely, appcache received’t be triggered, which implies subsequent requests will load assets over the wire (not from appcache).
Android has a bizarre subject on this case, when wifi is disconnected, play management will change to pause management (on play) however no audio might be produced and generally there’s no progress bar.
Conclusion: AppCache is just not a viable resolution to this downside but. It doesn’t assist with video games when you’re enjoying a number of audio information by way of JS (because of causes talked about right here). It does repair one downside although, which is audios don’t delay anymore once you add the app to residence in iOS Safari and launch from residence display screen which used to occur earlier than for the primary time as audio doesn’t downloads on web page load.
Web Audio API
This is by far the best means however then has its cons. It’s neither supported in iOS 5 and beneath nor within the android inventory and chrome browsers. This means we are able to atleast use it to repair audio help in iOS 6. I felt like writing a complete new publish on it, so be happy to learn right here.
Conclusion
As far as HTML5 audio help goes, I’ve just about given up on iOS 5 and android browsers for my video games. I’ve resorted to utilizing instruments like CocoonJS to transform my HTML5 video games to native iOS and Android apps that not solely fixes audio limitations but in addition permits me to place my app on Appstore and Google Play! Tools like Cocoon fixes audio limitations and boosts the efficiency of your video games massively by using the native capabilities of the system – {hardware} accelerated canvas and audio help.
Recommended from our customers: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch.
HI-FI News
by way of Code Theory https://codetheory.in
April 2, 2024 at 04:20AM
-
Product on sale
Audiophile Vinyl Records Cleaning BundleOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Product on sale
Easy Start Vinyl Records Cleaning KitOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Vinyl Records Cleaner Easy Groove Concentrate€34.95 incl. VAT -
Easy Groove Super Set€199.00 incl. VAT -
Easy Groove Enzycaster – vinyl records prewash cleaner€34.95 incl. VAT -
Easy Groove Extreme MK. II- vinyl records prewash cleaner€45.00 incl. VAT -
Vinyl Records Cleaner – Easy Groove Virgin Concentrate€35.00 incl. VAT -
Easy Groove Spray&Wipe vinyl records cleaner€34.95 incl. VAT








