Tim Joyce
Tim Joyce

Reputation: 4517

Sound latency in AS3

I am triggering short sounds dynamically from the library for a game (Specifically Air for Android). When the user clicks a button the sound can take up to 600ms to actually play. I have set it for any silence before the actual sound by calling the sound like so:

private var channel:SoundChannel = new SoundChannel();
private var buttonClickSound:Sound = new button_click();

public function buttonDownClick() {
    channel = buttonClickSound.play(63);
}

Still getting a ridiculous amount of latency.

Things I have tried: converting MP3 to wav placing Sound Clip in the timeline calling Sound Clip from urlLoader

All return the same results. I know there are threads here that talk about this but none have offered a real solution that I can find. Is there no way to cache the sound or store it in a buffer?

Thanks for looking.

Upvotes: 0

Views: 1643

Answers (2)

AivarasFX
AivarasFX

Reputation: 1

Sound latency is a bug in Flash exporting to AIR.

Easy way around is to make some background music playing, then sound device doesn't go to sleep. If you don't want music you can make some silent sound playing forever. This solves the problem with sound delay for other short sounds.

[Embed(source="sfx.mp3")]
const snd_sfx:Class;

const sfx:Sound = new snd_sfx();

const SilentSoundTransform:SoundTransform = new SoundTransform(0);

function playSoundSilentlyEndlessly(evt:Event = null):void
{
    sfx.play(0, 1000, SilentSoundTransform).addEventListener(Event.SOUND_COMPLETE, playSoundSilentlyEndlessly, false, 0, true); // plays the sound with volume 0 endlessly
}

playSoundSilentlyEndlessly();

Upvotes: 0

user562566
user562566

Reputation:

Important update

The flash CS5 AIR for android functionality was never out of beta. And sadly as adobe is famous for, the functionality has been discontinued for flash CS5 so that the full working/updated version will become a selling feature for CS6 or CS5.5. I have many, many feelings about this as a customer who has spent literally MANY THOUSANDS of dollars on adobe products but I'll keep this about information and not my burning rage towards adobe feelings. For proof of this see:

http://labs.adobe.com/technologies/flashpro_extensionforair/

So it is most likely that this is one of many bugs left unfixed in AIR for android in CS5. This is not a surprise, as when I bought CS4 master collection which promised AIR 1.5, even AIR 1.5 was left broken and all bug fixes were rolled into 2.0, which was only available for CS5. One of the biggest bugs they never fixed in CS4 AIR 1.5 was a major, crippling bug in webkit which nearly rendered any use of webkit in AIR non-functional. In fact when I worked for Webkinz.com, a major project was completely thrown out the window and I was actually ridiculed for proposing using AIR, touting all it's amazingness and then left looking like an idiot in front of all my peers when these bugs appeared. Literally I'm not kidding the head of my 180+ developer division centered me out in a meeting around a gigantic table with every major player in that company.

Anyway perhaps my update did bleed into somewhat of a rant but it's good information I think and a good lesson to be learned, which is always assume the worst with Adobe products. Fortunately, as mentioned in my answer, it's not a dead end and there are ways around their "business model."

Original Answer

It's a bug in AIR for Android:

http://forums.adobe.com/thread/753977

That post is dated, but I've seen several more around the same time frame and enough that I'd call it a fact. Have you updated flash CS5? ( Go to Help->Updates). To be honest I don't even know if they have patched/addressed this bug yet but your best bet of finding out is to grab the very latest Flex SDK and try manually packaging your SWF into an APK using it. Instructions:

http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac25d3d8c712b2d86751e-8000.html#WS901d38e593cd1bac25d3d8c712b2d86751e-7ffb

More detailed/tutorial-like instructions:

http://thulasiramsoft.wordpress.com/2011/01/16/build-apps-for-android-using-as3-without-a-device-using-flex-air/

If this is a bug this is where you're going to see the fix appear first. Try packaging your SWF into an APK manually using the above methods and ensure you've updated the AIR for android on the device to AIR 2.6 (latest, just go to the market to update). This will either fix the problem or eliminate it as a possibility both on the compiler and runtime side.

Upvotes: 2

Related Questions