简体   繁体   中英

How to make audio in swiffy HTML5 files play in mobile browsers?

I have taken this problem to various forums, and here once (where was it blocked as off-topic). It seems on-topic to me - a specific programming problem, a software algorithm, and a software tool commonly used by programmers.

I have a website of Flash SWF animations with sound. I converted the SWF to HTML5 using Google's swiffy . On desktop PCs, using Chrome, FF and IE, there is sound, but not in Mobile Safari and Android browsers . And not in Chrome on my iPad.

The audio is embedded in the code generated by swiffy, and it looks like it's MP3 encoded in base64 (see "data:audio/mpeg;base64," and "format":"MP3" in the html).

Since there is no developer forum for swiffy, and I get no replies from thefeedback form , I looked around to identify what's stopping Mobile Safari, Android browers and iPad Chrome playing the sound. For Safari, I find things like " You cannot preload sound files " but since the sound is embedded in swiffy, there's no sound file to preload. Why no sound in Android and iPad Chrome, both Google products, is also a mystery.

I imagine there's no hack that will solve the problem, if Google hasn't managed to, but insights are appreciated.

I know it's quite late for you but i think some people may also face the same problem and come to this post. So I would like to offer my answer here

As Victor said, you can use getURL to trigger sound outside. But if I use getURL, I would use Web Audio API in the javascript side. Because using Web Audio API, it is possible to play multi tracks simultaneously in mobile. I intergrated library SoundJS to my project.

However, as you have to change every audio frame into a getURL function, it actually costs you quite a lot of time. I found another optional idea that you can just edit the swiffy core once and for all, which use native audio object (but it also mean you can only play one sound at a time in mobile).

originally, swiffy creates native audio object to play base64 encoded sound. Because of the mobile limitation, it's silent if the sound is not triggered by an input event. However it's possible to reuse an active audio object.

First, I have a button to start the animation,which also create an audio object and play it (with a zero length base64 encoded sound);

audio = new Audio("data:audio/mpeg;base64,");
audio.play();

in swiffy core script, search keyword "new Audio", you will see the part that swiffy plays sound. Instead of createing a new audio object, make swiffy use your old audio object

audio.src = a.sound;
audio.play();

After that, without revising, your swf should be able to play sound even in mobile.

However, as it's quite like a hack. It may get some unexpected problem

You can communicate the swiffy flash with the main HTML file so, inside actionscript, you can send a getURL call to javascript where you have a hidden html5 mp3 player, something like:

getURL("Javascript:playSound();");

In HTML, inside the javascript function, something like:

 <audio controls>
 <source src="test.ogg" type="audio/ogg">
 <source src="test.mp3" type="audio/mpeg">
 </audio> 

I'm still working in this but I hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM