假设我在网址上有一个wav文件:
http://hostname.com/mysound.wav
我正在尝试使用声音类加载wav文件:
var url:String = "http://hostname.com/test.wav"; var urlRequest:URLRequest = new URLRequest(url); var sound:Sound = new Sound(); sound.load(urlRequest); sound.play();
但是,这似乎不起作用.flash播放器可以播放wav文件,还是只是mp3?
Sound类的ActionScript文档指出仅支持MP3文件.
这里有一个简单的类,用于使用开源popforge库从flash中的url加载和播放wav文件:http://code.google.com/p/popforge/
干杯!
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(); } }
由于ByteArray,你可以直接进行解决方法; )
看一下这个 :
http://richapps.de/?p=97
编辑:
之前的链接有点老了我认为你也应该看看Andre和Joa神话般的PopForge 库.那里实际上还有一个wav解码器类.
http://code.google.com/p/popforge/source/browse/#svn/trunk/flash/PopforgeLibrary/src/de/popforge/format/wav