GiG
November 6, 2017, 10:50am
#1
Hi,
I am trying to load a remote audio file, and I’d like to use it as a Sound object:
var urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete);
urlLoader.load(new URLRequest(url));
and onURLLoaderComplete does:
var loadedSfx = cast(event.target.data, Sound);
// event.target is urlLoader
I thought I could simply cast the binary data to Sound, but the data results as an OggS, and it can’t be cast to Sound.
How do I use a downloaded sound file as a Sound? Do I have to extract/convert it to play its content?
Thanks
Perhaps you could use sound.load
unstead of URLLoader
, or Sound.fromBytes
or Sound.loadFromBytes
?
var sound = new Sound ();
sound.load (new URLRequest (url));
or
private function onURLLoaderComplete (event:Event):Void {
Sound.loadFromBytes (urlLoader.data).onComplete (function (sound) {
sound.play ();
}).onError (function (e) trace (e));
});
GiG
November 7, 2017, 2:22pm
#3
The first option work, thanks!
In the second option I think you mean
sound = new Sound();
sound.loadSomething(urlLoader.data)
...
as the static method loadFromBytes
doesn’t exist for Sound
.
sound can do loadPCMFromByteArray
and loadCompressedDataFromByteArray
, are you referring to one of these functions?
Oops, yeah, I suppose I meant Sound.loadFromFile (path)
, or yeah, those methods should work for bytes