tags:

views:

3301

answers:

4

Let's say I have a wav file at a url:

http://hostname.com/mysound.wav

I'm trying to load the wav file with the sound class like:

var url:String = "http://hostname.com/test.wav";
var urlRequest:URLRequest = new URLRequest(url);
var sound:Sound = new Sound();
sound.load(urlRequest);
sound.play();

However, this doesn't seem to work. Can flash player play wav files, or is it just mp3s?

+5  A: 

The ActionScript documentation for the Sound class states that only MP3 files are supported.

Chad Birch
And perhaps that's for the best.
aaaidan
+3  A: 

Directly you cannot but there are workarounds thanks to the ByteArray ; )

Check this out :

http://richapps.de/?p=97

EDIT:

The previous link being a bit old I reckon you should also have a look on Andre and Joa's fabulous PopForge library. There's actually a wav decoder class there as well.

http://code.google.com/p/popforge/source/browse/#svn/trunk/flash/PopforgeLibrary/src/de/popforge/format/wav

Theo.T
+1  A: 

Yes, you can. I have made Wav/Au Flash player, that can play stream wav, encoded in G.711 or PCM in any bitlength and samplerate. Licensed under GPLv2, here: http://blog.datacompboy.ru/2009/10/15/wav-au-flash-player/

datacompboy
A: 

here a simple class for loading and playing wav files from a url in flash using the open source popforge library: http://code.google.com/p/popforge/

cheers!

    public class WavURLPlayer
     {


      public static function PlayWavFromURL(wavurl:String):void
      {
       var urlLoader:URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        urlLoader.addEventListener(Event.COMPLETE, onLoaderComplete);
        urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoaderIOError);

       var urlRequest:URLRequest = new URLRequest(wavurl);

       urlLoader.load(urlRequest);
      }

      private static function onLoaderComplete(e:Event):void
      {
       var urlLoader:URLLoader = e.target as URLLoader;
        urlLoader.removeEventListener(Event.COMPLETE, onLoaderComplete);
        urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoaderIOError);

       var wavformat:WavFormat = WavFormat.decode(urlLoader.data);

       SoundFactory.fromArray(wavformat.samples, wavformat.channels, wavformat.bits, wavformat.rate, onSoundFactoryComplete);
      }

      private static function onLoaderIOError(e:IOErrorEvent):void
      {
       var urlLoader:URLLoader = e.target as URLLoader;
        urlLoader.removeEventListener(Event.COMPLETE, onLoaderComplete);
        urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoaderIOError);

       trace("error loading sound");

      }

      private static function onSoundFactoryComplete(sound:Sound):void
      {
       sound.play();
      }


 }
fluxa