views:

441

answers:

1

I have written a game in Flash (AS3/Flex SDK/MXMLC) that streams music and sound files from my server.

I'm concerned about bandwidth - the audio components are nearly 3MB, and if the game becomes popular it will really rack up the bandwidth charges.

Is there any way to make Flash cache the audio components once they've been downloaded, instead of streaming them every time the game is loaded on the same PC?

I know about the SharedObjects ("cookies") but I don't think there's a way to store binary data in there... or is there?

+2  A: 

When you say "stream", I assume you mean progressive download over HTTP.

You'll want to ensure that your media assets are being sent from your server with the appropriate caching headers that meet your goals, for starters. If those are not cache friendly, then files won't be cached. Also, query strings will generally prevent client-side caching.

There is nothing really magical about the Flash player in this regard--everything should work as if this all was just a plain ol' web page.

You may want to consider hosting your large, static media assets on something cheap-ish (like Amazon S3 or CloudFront.) This could reduce the cost of bulk transfers that do not require application logic.

Update inspired by the comments: Here is a possible outline for you to follow in your quest...

  1. Figure out what kind of caching is and is not happening first
    1. Clear your browser cache
    2. Go through a use case in your game
    3. Go through it again, with the expectation of seeing the same content used
    4. Inspect web server log files. The first group of requests should all have HTTP 200 respond codes. For the second group of requests, "cachable" requests for files that have already been delivered you should see either nothing or HTTP 304 (a request just to see if the content cached is valid.) If you see identical sets of requests, all with 200 OK, then caching is not happening
  2. Learn more about caching
    1. Install a FireFox plug-in like TamperData, watch the caching headers
    2. Read the spec, know it. RFC 2616
    3. Experiment

I would not be surprised if you were caching content already but just didn't realize it!

Stu Thompson
What's "an appropriate caching header" for an MP3 file? In the flash code I get this file via a URL request - how would I then reference it once it was cached?
Andy Moore
First question: It depends what your needs are. Does your mp3 file change often? Or never? On a regular basis? Or??? Second question: That is the magic of caching! You don't have to worry about it. You request the resource (http://example.com/sound.mp3) and the browser manages the caching for you. You, as an HTML, Flex, or whatever coder do not have to worry about that. Just be sure you don't 'break' caching by doing strange things like using query strings (an indication of dynamic content, and hence not cachable.) I've updated the answer with more details.
Stu Thompson