A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: The performance of sound.play()

  1. #1
    Member
    Join Date
    Jan 2009
    Posts
    90

    The performance of sound.play()

    I'm continually frustrated by the (performance) cost of sound in Flash. I can optimize my app all I want, but in the end, playing a sound is the most time consuming thing that ever happens in a frame.

    PHP Code:
    var start:int flash.utils.getTimer();
    this.sound.play();
    var 
    end:int flash.utils.getTimer();
    trace(end-start); 
    On my computer, using Flash 9, this averages about 7ms. While I recognize sound isn't going to be instantaneous, 7ms is a lot of time when you only have 20-30 ms to work with per frame.

    So I started searching the web, and found surprisingly few discussions of the topic. I guessed this was either:

    a) I'm the first person to ever notice this. (unlikely)
    b) There's something with my computer that causes sound calls to be exceptionally slow. (probably not, I've tested on two computers)
    c) There's something with my version of Flash that causes sound calls to be exceptionally slow.
    d) There's something with either my sound files or Flash code that causes the call to be inefficient.
    e) It's a well known problem and I just don't know what to Google for.

    Any ideas? More importantly, any tips on improving performance?

  2. #2
    Wait- what now? tidenburg's Avatar
    Join Date
    Dec 2005
    Posts
    1,471
    When I made a DDR-style game a while back, I found that sound.play(); cause all the notes to jump and eventually had to cave and stream the sound. Not ideal, but it did make the syncing alot more managable at least.

    No idea why it happens or how to fix it though.
    "I'd only told them the truth. Was that so selfish? Our integrity sells for so little, but it is all we really have. It is the very last inch of us, but within that inch, we are free."

  3. #3
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    If it really is a problem, why not just start the sound before any action takes place and set volume to 0, then just increase volume when needed instead of calling play();

  4. #4
    Senior Member hatu's Avatar
    Join Date
    Jan 2007
    Posts
    480
    I've never heard of that problem, how crazy amount of sounds are you playing for that to be a problem?
    http://hatu.biz
    Portfolio & games

  5. #5
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    I've made Flash metronomes before, and you can never get a completely steady beat at a high tempo...it's something with Flashes sound capabilities in general I think, and a long time problem.

    Like MikeMD suggested, maybe start playing the sound already, and then increase the volume. Of course, if the sound is like an effect or something, then you obviously need to catch the beginning, at which point you don't have any other option than just calling .play();

    The only advice I can give you is what helped my metronome. You need to compress the sound files you're using as much as possible, and make sure you don't have any unneeded milliseconds in the sound file. This helps alot, since I think the way Flash works is by "streaming" the sound files from the .swf, and it has to load in the sound every time you call play();...another reason why Flash should support multi-threading...

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  6. #6
    Member
    Join Date
    Jan 2009
    Posts
    90
    The problem is, as Pazil suggests, that the sounds are effects in a game. So they need to play from their start more or less when called. So volume control is out.

    But my issue isn't really with the slight delay of calling sounds, it's that the call to sound.play() is slow on the relative scale of things.

    I'm curious, can anyone else test a call to sound.play() on their machine and report back how many ms it takes?

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I tried your code and get same result. However, I have not noticed any slowdown in actual games. Maybe if you want to start 100 sounds at once it would affect the gameplay but then again, Flash is limited to 12 (or was it 24) sounds playing same time.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    Hi guys, I've the same problem to solve. I wrote a game to promote my band where you play drums with your keyboard and of course sound.play() performance is critical. In practice mode, when you press a key a sound is played and execution stops for a while. In normal game mode there is a song playback and no sound is produced by the drums. The more you play like you should, the more points you earn.

    to have an idea of the problem, you can launch it via Facebook:
    http://apps.facebook.com/francescosimulator

    or directly:
    http://www.francescosimulator.com

    Here's what I learned:

    - like Pazil says, edit your sound (I used for example Cubase) making sure you don't have any milliseconds of silence in the sound file
    - use wav files instead of mp3: mp3 always have a silence before the sounds starts
    - This problem seems to affect windows only: if you try with mac it works PERFECTLY.This is maybe due to a different management of sound events.
    - Be shure you're using the latest version of the compiler. I made an update and performances improved a little bit.

    let me know if have good news about it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center