actionscript-3 - 获取使用 `Sound` 对象下载的 mp3 文件的原始数据

标签 actionscript-3 object flash-cs5

我已经动态创建了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 文件,您可以:
  • 使用 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 个字节,并尽快关闭流(仅网络开销的一个数据包,另外您可能会从缓存中获取它反正) :
    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/

    相关文章:

    actionscript-3 - AS3 中的评估公式?

    python - 如何在 Python 中中止对象实例的创建?

    actionscript-3 - 通过 ActionScript 3在时间轴上引用现有声音对象

    xml - 我什至没有任何AS3代码?

    actionscript-3 - 错误 2006 提供的索引超出范围

    flash - JSFL - 更改静态文本值

    apache-flex - Adobe Flex : Why do I get intermittent SecurityErrorEvents on some browsers?

    flash - 我如何知道 MovieClip 何时放置在舞台上完成播放?

    javascript - 如何判断对象的所有值是否存在于另一个对象中

    javascript - 通过比较属性将第二个数组与第一个数组合并,如果对象不属于第二个数组,则将属性添加到第一个数组中的对象