Can the flash player play .wav files from a url?

给你一囗甜甜゛ 提交于 2019-12-28 12:44:27

问题


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?


回答1:


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




回答2:


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();
      }


 }



回答3:


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




回答4:


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/




回答5:


Flash itself does not support playing .wav files. Flash/Flex Builder compiles it directly into a Sound object ready to be played in your scripts, but for external .wav sound, the Flash SDK won't help you.

You will need to read the .wav data yourself and make Flash play, or: there are a couple of third-party libraries around that do this for you. The above post uses popforge, here's tutorial that demonstrates as3wavsound playing an external .wav file from a URL:

http://active.tutsplus.com/tutorials/media/quick-tip-play-external-wav-files-in-as3/



来源:https://stackoverflow.com/questions/668186/can-the-flash-player-play-wav-files-from-a-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!