加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

actionscript-3 – 获取正在使用`Sound`对象下载的mp3文件的原始

发布时间:2020-12-15 07:23:42 所属栏目:百科 来源:网络整理
导读:我已经动态创建了Sound对象 var files:Object={};files["file1"]={};files["file1"]["snd"]=new Sound();...... //url etcfiles["file1"]["snd"].addEventListener(ProgressEvent.PROGRESS,onLoadProgress); function onLoadProgress(event:ProgressEvent):v
我已经动态创建了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();但不提供对正在加载的数据的公共访问

该课程将为您处理流媒体(更准确地说,是渐进式下载)

如果您需要访问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文件,您可以:

>使用URLLoader将mp3文件作为ByteArray加载,手动读取48个字节,并从内存加载Sound实例,从而失去任何渐进式下载功能. :

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);

>您还可以以经典方式加载使用Sound类(以允许渐进式下载),并使用URLStream独立加载前48个字节,并关闭ASAP流(仅限网络开销的数据包,另外您可以从中获取它缓存无论如何):

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读