通过ByteArray来播放MP3

在Actionscript3中,由于没有Sound.loadBytes()方法,所以无法通过ByteArray来直接播放MP3。SWF和图片文件(JPEG,GIF和PNG)可以通过Loader.loadBytes加载,并不需要太多的操作就可以使用,而原始的声音数据可以通过Sound对象的SampleDataEvent来播放。现在事情有点棘手,如果你的MP3数据在ByteArray中,如何播放它呢。as3swf可以帮助你解决这个问题。
我们要做以下几步:
  1.创建一个嵌入了MP3的SWF。
  2.通过LoaderoadBytes导入SWF。
  3.实例化绑定的类。
显然,最麻烦的差事是创建嵌入了MP3的SWF。刚开始,我们要创建一个嵌入了虚拟MP3的虚拟SWF,来看看里面有什么,然后再动态创建这个结构。(We’ll just start by creating a dummy SWF using a dummy MP3 to see how it has to look like, in order to recreate the structure dynamically later.)

  1. package
  2. {
  3.    import flash.display.Sprite;

  4.    public class MP3Wrapper extends Sprite
  5.    {
  6.       [Embed(source="test.mp3")]
  7.       public var soundClass:Class;
  8.    }
  9. }
复制代码

这段代码我们只是嵌入进来了MP3,并且把它绑定到一个叫Sound类上。当我们把这个SWF导入到主SWF上的时候,我们可以声明这个类的实例,然后我们得到了一个包含该MP3的声音对象,这时就可以播放它了。
上面的代码就像这样创建SWF的:

  1. [69:FileAttributes] AS3: true, HasMetadata: false, UseDirectBlit: false, UseGPU: false, UseNetwork: false
  2. [09:SetBackgroundColor] Color: ffffffff
  3. [86:DefineSceneAndFrameLabelData]
  4.    Scenes:
  5.       [0] Offset: 0, Name: Scene 1
  6. [14:DefineSound] SoundID: 1, Format: 2, Rate: 44kHz, Size: 16bit, Type: stereo, Samples: 6867072
  7. [82:DoABC] Lazy: true, Length: 767
  8. [76:SymbolClass]
  9.    Symbols:
  10.       [0] TagID: 1, Name: MP3Wrapper_soundClass
  11.       [1] TagID: 0, Name: MP3Wrapper
  12. [01:ShowFrame]
  13. [00:End]
复制代码

这里面有三个有用的标签:
1.DefineSound:包含了嵌入的MP3.我们所用的MP3是44kHz,16bit,立体声,包含了6867072声音采样。它属于SWF的内部资源,ID为1.
2.DoABC:包含所有的Actionscript字节,这里是文档类和定义的声音类。
3.SymbolClass:把Actionscript类文件绑定到库资源上,在我们的例子里,文档类是MP3Wrapper,这里

最重要的,是被绑定到库资源里的ID为1的MP3Wrapper_soundClass类。

我们刚刚重用了包括DoABC和SymboClass的所有标签,留下了DefineSound标签,需要我们去动态创建。DefineSound包括了原始的声音数据(这里是MP3),这里面包含了一些标签,它们告诉Flash Player数据的规格数等,比如声音采样的数量、采样率、采样大小、是单声道还是立体声播放,还有库ID等(通常是1,因为我们只有一个库资源)

TagDefineSound的有一个非常方便的工厂方法,用来从MP3创建自己,这个方法叫做createWithMP3。这个方法自动去扫描MP3,来确定MP3里包含的采样数量,还有采样率等:

  1. var defineSound:TagDefineSound = TagDefineSound.createWithMP3(1, mp3);
复制代码

这里代码展示了在运行时创建包含MP3文件的SWF,并开始播放它:

  1. package
  2. {
  3. import flash.display.Loader;
  4. import flash.display.LoaderInfo;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.media.Sound;
  8. import flash.net.URLLoader;
  9. import flash.net.URLLoaderDataFormat;
  10. import flash.net.URLRequest;
  11. import flash.utils.ByteArray;

  12. import com.codeazur.as3swf.SWF;
  13. import com.codeazur.as3swf.SWFData;
  14. import com.codeazur.as3swf.data.SWFScene;
  15. import com.codeazur.as3swf.data.SWFSymbol;
  16. import com.codeazur.as3swf.data.consts.SoundCompression;
  17. import com.codeazur.as3swf.data.consts.SoundRate;
  18. import com.codeazur.as3swf.data.consts.SoundSize;
  19. import com.codeazur.as3swf.data.consts.SoundType;
  20. import com.codeazur.as3swf.tags.TagDefineSceneAndFrameLabelData;
  21. import com.codeazur.as3swf.tags.TagDefineSound;
  22. import com.codeazur.as3swf.tags.TagDoABC;
  23. import com.codeazur.as3swf.tags.TagEnd;
  24. import com.codeazur.as3swf.tags.TagFileAttributes;
  25. import com.codeazur.as3swf.tags.TagSetBackgroundColor;
  26. import com.codeazur.as3swf.tags.TagShowFrame;
  27. import com.codeazur.as3swf.tags.TagSymbolClass;

  28. public class MP3Player extends Sprite
  29. {
  30. public function MP3Player()
  31. {
  32. // Load the mp3 into a ByteArray
  33. var request:URLRequest = new URLRequest("test.mp3");
  34. var loader:URLLoader = new URLLoader();
  35. loader.dataFormat = URLLoaderDataFormat.BINARY;
  36. loader.addEventListener(Event.COMPLETE, completeHandler);
  37. loader.load(request);
  38. }

  39. protected function completeHandler(e:Event):void {
  40. var mp3:ByteArray = URLLoader(e.target).data as ByteArray;
  41. // Wrap the MP3 with a SWF
  42. var swf:ByteArray = createSWFFromMP3(mp3);
  43. // Load the SWF with Loader::loadBytes()
  44. var loader:Loader = new Loader();
  45. loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
  46. loader.loadBytes(swf);
  47. }

  48. protected function initHandler(e:Event):void {
  49. // Get the sound class definition
  50. var SoundClass:Class = LoaderInfo(e.currentTarget).applicationDomain.getDefinition("MP3Wrapper_soundClass") as Class;
  51. // Instantiate the sound class
  52. var sound:Sound = new SoundClass() as Sound;
  53. // Play the sound
  54. sound.play();
  55. }

  56. protected function createSWFFromMP3(mp3:ByteArray):ByteArray
  57. {
  58. // Create an empty SWF
  59. // Defaults to v10, 550x400px, 50fps, one frame (works fine for us)
  60. var swf:SWF = new SWF();

  61. // Add FileAttributes tag
  62. // Defaults: as3 true, all other flags false (works fine for us)
  63. swf.tags.push(new TagFileAttributes());

  64. // Add SetBackgroundColor tag
  65. // Default: white background (works fine for us)
  66. swf.tags.push(new TagSetBackgroundColor());

  67. // Add DefineSceneAndFrameLabelData tag
  68. // (with the only entry being "Scene 1" at offset 0)
  69. var defineSceneAndFrameLabelData:TagDefineSceneAndFrameLabelData = new TagDefineSceneAndFrameLabelData();
  70. defineSceneAndFrameLabelData.scenes.push(new SWFScene(0, "Scene 1"));
  71. swf.tags.push(defineSceneAndFrameLabelData);

  72. // Add DefineSound tag
  73. // The ID is 1, all other parameters are automatically
  74. // determined from the mp3 itself.
  75. swf.tags.push(TagDefineSound.createWithMP3(1, mp3));

  76. // Add DoABC tag
  77. // Contains the AS3 byte code for the document class and the
  78. // class definition for the embedded sound
  79. swf.tags.push(TagDoABC.create(abc));

  80. // Add SymbolClass tag
  81. // Specifies the document class and binds the sound class
  82. // definition to the embedded sound
  83. var symbolClass:TagSymbolClass = new TagSymbolClass();
  84. symbolClass.symbols.push(SWFSymbol.create(1, "MP3Wrapper_soundClass"));
  85. symbolClass.symbols.push(SWFSymbol.create(0, "MP3Wrapper"));
  86. swf.tags.push(symbolClass);

  87. // Add ShowFrame tag
  88. swf.tags.push(new TagShowFrame());

  89. // Add End tag
  90. swf.tags.push(new TagEnd());

  91. // Publish the SWF
  92. var swfData:SWFData = new SWFData();
  93. swf.publish(swfData);

  94. return swfData;
  95. }

  96. private static var abcData:Array = [
  97. 0x10, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x07, 0x6d, 0x78, 0x2e, 0x63, 0x6f, 0x72, 0x65,
  98. 0x0a, 0x49, 0x46, 0x6c, 0x65, 0x78, 0x41, 0x73, 0x73, 0x65, 0x74, 0x0a, 0x53, 0x6f, 0x75, 0x6e,
  99. 0x64, 0x41, 0x73, 0x73, 0x65, 0x74, 0x0b, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x6d, 0x65, 0x64,
  100. 0x69, 0x61, 0x05, 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x12, 0x6d, 0x78, 0x2e, 0x63, 0x6f, 0x72, 0x65,
  101. 0x3a, 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x41, 0x73, 0x73, 0x65, 0x74, 0x00, 0x15, 0x4d, 0x50, 0x33,
  102. 0x57, 0x72, 0x61, 0x70, 0x70, 0x65, 0x72, 0x5f, 0x73, 0x6f, 0x75, 0x6e, 0x64, 0x43, 0x6c, 0x61,
  103. 0x73, 0x73, 0x0a, 0x4d, 0x50, 0x33, 0x57, 0x72, 0x61, 0x70, 0x70, 0x65, 0x72, 0x0d, 0x66, 0x6c,
  104. 0x61, 0x73, 0x68, 0x2e, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x06, 0x53, 0x70, 0x72, 0x69,
  105. 0x74, 0x65, 0x0a, 0x73, 0x6f, 0x75, 0x6e, 0x64, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x05, 0x43, 0x6c,
  106. 0x61, 0x73, 0x73, 0x2a, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x61,
  107. 0x64, 0x6f, 0x62, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x32, 0x30, 0x30, 0x36, 0x2f, 0x66, 0x6c,
  108. 0x65, 0x78, 0x2f, 0x6d, 0x78, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x07, 0x56,
  109. 0x45, 0x52, 0x53, 0x49, 0x4f, 0x4e, 0x06, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x07, 0x33, 0x2e,
  110. 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x0b, 0x6d, 0x78, 0x5f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61,
  111. 0x6c, 0x06, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x0c, 0x66, 0x6c, 0x61, 0x73, 0x68, 0x2e, 0x65,
  112. 0x76, 0x65, 0x6e, 0x74, 0x73, 0x0f, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x44, 0x69, 0x73, 0x70, 0x61,
  113. 0x74, 0x63, 0x68, 0x65, 0x72, 0x0d, 0x44, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x4f, 0x62, 0x6a,
  114. 0x65, 0x63, 0x74, 0x11, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x61, 0x63, 0x74, 0x69, 0x76, 0x65, 0x4f,
  115. 0x62, 0x6a, 0x65, 0x63, 0x74, 0x16, 0x44, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x4f, 0x62, 0x6a,
  116. 0x65, 0x63, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x0a, 0x16, 0x01, 0x16,
  117. 0x04, 0x18, 0x06, 0x16, 0x07, 0x18, 0x08, 0x16, 0x0a, 0x18, 0x09, 0x08, 0x0e, 0x16, 0x14, 0x03,
  118. 0x01, 0x01, 0x01, 0x04, 0x14, 0x07, 0x01, 0x02, 0x07, 0x01, 0x03, 0x07, 0x02, 0x05, 0x09, 0x02,
  119. 0x01, 0x07, 0x04, 0x08, 0x07, 0x04, 0x09, 0x07, 0x06, 0x0b, 0x07, 0x04, 0x0c, 0x07, 0x04, 0x0d,
  120. 0x07, 0x08, 0x0f, 0x07, 0x04, 0x10, 0x07, 0x01, 0x12, 0x09, 0x03, 0x01, 0x07, 0x04, 0x13, 0x07,
  121. 0x09, 0x15, 0x09, 0x08, 0x02, 0x07, 0x06, 0x16, 0x07, 0x06, 0x17, 0x07, 0x06, 0x18, 0x0d, 0x00,
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x05, 0x00, 0x02, 0x00, 0x02, 0x03, 0x09, 0x03, 0x01,
  126. 0x04, 0x05, 0x00, 0x05, 0x02, 0x09, 0x05, 0x00, 0x08, 0x00, 0x06, 0x07, 0x09, 0x07, 0x00, 0x0b,
  127. 0x01, 0x08, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x04, 0x01, 0x0a, 0x06, 0x01, 0x0b, 0x11, 0x01,
  128. 0x07, 0x00, 0x0a, 0x00, 0x05, 0x00, 0x01, 0x0c, 0x06, 0x00, 0x00, 0x08, 0x08, 0x03, 0x01, 0x01,
  129. 0x04, 0x00, 0x00, 0x06, 0x01, 0x02, 0x04, 0x00, 0x01, 0x09, 0x01, 0x05, 0x04, 0x00, 0x02, 0x0c,
  130. 0x01, 0x06, 0x04, 0x01, 0x03, 0x0c, 0x00, 0x01, 0x01, 0x01, 0x02, 0x03, 0xd0, 0x30, 0x47, 0x00,
  131. 0x00, 0x01, 0x00, 0x01, 0x03, 0x03, 0x01, 0x47, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x02, 0x0a,
  132. 0xd0, 0x30, 0x5d, 0x04, 0x20, 0x58, 0x00, 0x68, 0x01, 0x47, 0x00, 0x00, 0x04, 0x02, 0x01, 0x05,
  133. 0x06, 0x09, 0xd0, 0x30, 0x5e, 0x0a, 0x2c, 0x11, 0x68, 0x0a, 0x47, 0x00, 0x00, 0x05, 0x01, 0x01,
  134. 0x06, 0x07, 0x06, 0xd0, 0x30, 0xd0, 0x49, 0x00, 0x47, 0x00, 0x00, 0x06, 0x02, 0x01, 0x01, 0x05,
  135. 0x17, 0xd0, 0x30, 0x5d, 0x0d, 0x60, 0x0e, 0x30, 0x60, 0x0f, 0x30, 0x60, 0x03, 0x30, 0x60, 0x03,
  136. 0x58, 0x01, 0x1d, 0x1d, 0x1d, 0x68, 0x02, 0x47, 0x00, 0x00, 0x07, 0x01, 0x01, 0x06, 0x07, 0x03,
  137. 0xd0, 0x30, 0x47, 0x00, 0x00, 0x08, 0x01, 0x01, 0x07, 0x08, 0x06, 0xd0, 0x30, 0xd0, 0x49, 0x00,
  138. 0x47, 0x00, 0x00, 0x09, 0x02, 0x01, 0x01, 0x06, 0x1b, 0xd0, 0x30, 0x5d, 0x10, 0x60, 0x0e, 0x30,
  139. 0x60, 0x0f, 0x30, 0x60, 0x03, 0x30, 0x60, 0x02, 0x30, 0x60, 0x02, 0x58, 0x02, 0x1d, 0x1d, 0x1d,
  140. 0x1d, 0x68, 0x05, 0x47, 0x00, 0x00, 0x0a, 0x01, 0x01, 0x08, 0x09, 0x03, 0xd0, 0x30, 0x47, 0x00,
  141. 0x00, 0x0b, 0x02, 0x01, 0x09, 0x0a, 0x0b, 0xd0, 0x30, 0xd0, 0x60, 0x05, 0x68, 0x08, 0xd0, 0x49,
  142. 0x00, 0x47, 0x00, 0x00, 0x0c, 0x02, 0x01, 0x01, 0x08, 0x23, 0xd0, 0x30, 0x65, 0x00, 0x60, 0x0e,
  143. 0x30, 0x60, 0x0f, 0x30, 0x60, 0x11, 0x30, 0x60, 0x12, 0x30, 0x60, 0x13, 0x30, 0x60, 0x07, 0x30,
  144. 0x60, 0x07, 0x58, 0x03, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x1d, 0x68, 0x06, 0x47, 0x00, 0x00
  145. ];

  146. private static function abcDataToByteArray():ByteArray {
  147. var ba:ByteArray = new ByteArray();
  148. for (var i:uint = 0; i < abcData.length; i++) {
  149. ba.writeByte(abcData[i]);
  150. }
  151. return ba;
  152. }

  153. private static var abc:ByteArray = abcDataToByteArray();
  154. }
  155. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值