15.7.读取声音文件的ID3标签数据

问题
我想访问正在播放的mp3文件的信息,比如歌曲名,艺术家,专辑,分类等等。
解决办
读取Sound对象的id3属性
讨论
MP3 文件大多包含一些如songname, artist, album, genre, year等元数据,不过这些并不是都有,但大多数情况下都有songname和artist标签。

通过Sound对象的id3属性可获得这些数据。

这个属性其实是flash.media.ID3Info的类实例,它包含下列属性:
album
artist
comment
genre
songName
TRack
year
所以,想读取歌曲名,可这样写:
+展开
-ActionScript
_sound.id3.songName

如果歌曲还没有下载到swf中,id3数据是不能够被访问的,那到底怎么知道id3数据已经下载了呢,我们可以通过监听Sound对象的ID3事件,看下面的例子:
+展开
-ActionScript
package {
import flash.display.Sprite;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;
public class ID3Reader extends Sprite {
private var _sound:Sound;
public function ID3Reader ( ) {
_sound = new Sound(new URLRequest("song.mp3"));
_sound.addEventListener(Event.ID3, onID3);
_sound.play( );
}
public function onID3(event:Event):void {
// Create a text field and display it
var id3Display:TextField = new TextField( );
addChild(id3Display);
id3Display.x = 10;
id3Display.y = 20;
id3Display.width = 200;
id3Display.height = 200;
id3Display.background = true;
id3Display.multiline = true;
id3Display.wordWrap = true;
// Add some info about the song to the text field
id3Display.text += _sound.id3.songName + "\n";
id3Display.text += _sound.id3.artist + "\n";
id3Display.text += _sound.id3.album + "\n";
id3Display.text += _sound.id3.year + "\n"; }
}
}
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(174)喜欢(0)flash/flex/fcs/AIR