我已经动态创建了Sound
目的
var files:Object={};
files["file1"]={};
files["file1"]["snd"]=new Sound();
...... //url etc
files["file1"]["snd"].addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
function onLoadProgress(event:ProgressEvent):void
//// somehow I need to get the raw data (first 48 bytes to be exact) of the mp3 file which is downloading now
}
我试过
URLRequest
在那个函数中var myByteArray:ByteArray = URLLoader(event.target).data as ByteArray;
但没有成功
文件数据这么简单的东西竟然这么难得到,真是好笑
最佳答案
flash.media.Sound
是一个高级类,允许您在一行中播放声音文件:new Sound(new URLRequest('your url')).play();
但不提供对正在加载的数据的公共(public)访问
该类(class)将为您处理流式传输(更准确地说,渐进式下载)
如果需要访问 id3 数据,只需监听 Event.ID3 事件:
var sound:Sound = new Sound("http://archive.org/download/testmp3testfile/mpthreetest.mp3");
sound.addEventListener(Event.ID3, onId3);
sound.play();
function onId3(e:Event):void {
var id3:ID3Info = (e.target as Sound).id3;
trace(id3.album, id3.artist, id3.comment, id3.genre,
id3.songName, id3.track, id3.year);
}
如果您确实需要获取原始的前 48 个字节并自己处理它们,但请记住,您将不得不处理各种 mp3 格式 id3/no id3,并直接处理二进制数据,而不是让 actionscript 为你。
假设您不想两次下载 mp3 文件,您可以:
var l:URLLoader = new URLLoader;
l.dataFormat = URLLoaderDataFormat.BINARY;
l.addEventListener(Event.COMPLETE, onComplete);
l.load(new URLRequest("http://archive.org/download/testmp3testfile/mpthreetest.mp3"));
function onComplete(e:Event):void {
//do whatever you need to do with the binary data (l.data)
// ...
// load sound from memory
new Sound().loadCompressedDataFromByteArray(l.data, l.data.length);
var s:URLStream = new URLStream;
s.addEventListener(ProgressEvent.PROGRESS, onStreamProgress);
s.load(new URLRequest("http://archive.org/download/testmp3testfile/mpthreetest.mp3"));
function onStreamProgress(e:ProgressEvent):void {
if (s.bytesAvailable >= 48) {
// whatever you need to do with the binary data: s.readByte()...
s.close();
}
}
我仍然很想知道你为什么需要那些 48 个字节?
编辑:由于应该将 48 个字节提供给 MP3InfoUtil,因此您不需要做任何特别的事情,只需让 lib 完成工作:
MP3InfoUtil.addEventListener(MP3InfoEvent.COMPLETE, yourHandler);
MP3InfoUtil.getInfo(yourMp3Url);
关于actionscript-3 - 获取使用 `Sound` 对象下载的 mp3 文件的原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086357/