摘要:How to load ByteArray to Sound Class (1)
[Embed(source="assets/Sounds/voices/hello.mp3")]
private var musicClass:Class;
private var music:Sound = new musicClass();
private var soundChannel:SoundChannel=null;
public function Test()
{
soundChannel = music.play();
}
FileReference.load
is a nice new feature in Flash Player 10, but the only thing you get back from it is the FileReference.data
property, which is a ByteArray
. This is useless (well, not altogether, as we’ll see) in the case of MP3 audio files, because the built-in Sound API does not support loading from ByteArray
.」 1: package {
2: import flash.display.Sprite;
3: import flash.events.Event;
4: import flash.events.MouseEvent;
5: import flash.net.FileFilter;
6: import flash.net.FileReference;
7:
8: import org.audiofx.mp3.MP3FileReferenceLoader;
9: import org.audiofx.mp3.MP3SoundEvent;
10:
11: public class MP3FileReferenceTest extends Sprite
12: {
13: private var loader:MP3FileReferenceLoader;
14: private var fileReference:FileReference;
15: public function MP3FileReferenceTest()
16: {
17: loader=new MP3FileReferenceLoader();
18: loader.addEventListener(MP3SoundEvent.COMPLETE,mp3LoaderCompleteHandler);
19: fileReference=new FileReference();
20: fileReference.addEventListener(Event.SELECT,fileReferenceSelectHandler);
21: stage.addEventListener(MouseEvent.CLICK,clickHandler);
22: }
23: private function clickHandler(ev:MouseEvent):void
24: {
25: fileReference.browse([new FileFilter("mp3 files","*.mp3")]);
26: }
27: private function fileReferenceSelectHandler(ev:Event):void
28: {
29: loader.getSound(fileReference);
30: }
31: private function mp3LoaderCompleteHandler(ev:MP3SoundEvent):void
32: {
33: ev.sound.play();
34: }
35: }
36: }
var swfBytesLoader:Loader=new Loader();
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.allowLoadBytesCodeExecution = true;
swfBytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfCreated);
swfBytesLoader.loadBytes(swfBytes,loaderContext);