Flex中利用SoundEffect类动态显示MP3文件的ID3信息
By Minidxer | March 8, 2008
在之前的利用Flex的Sound类动态显示导入MP3文件时的ID3信息中,我们了解了如何利用Sound类将一个MP3文件load到Flex中。接下来的例子展示了如何利用SoundEffect类,动态读入MP3文件并且播放,以及访问Sound对象和它的ID3信息。
注意:源代码的压缩包中并没有包括全部的MP3文件,你可以到http://ghosts.nin.com/main/order_options下载它们.万岁,免费的 Nine Inch Nails(乐队名称)歌曲以及创作共用许可(Creative Commons licensing)!
下面是全部的代码:
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- http://blog.flexexamples.com/2008/03/05/displaying-a-dynamically-loaded-mp3-files-id3-information-in-flex-using-the-soundeffect-class/ -->
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- import mx.utils.ObjectUtil;
- private const NIN_URL:String = "http://ghosts.nin.com/";
- private function playSoundEffect(src:String):void {
- textArea.text = "";
- if (soundEffect.isPlaying) {
- soundEffect.stop();
- }
- soundEffect.source = src;
- soundEffect.play([soundEffect]);
- }
- private function soundEffect_id3(evt:Event):void {
- var id3Info:ID3Info = Sound(soundEffect.sound).id3;
- textArea.text += ObjectUtil.toString(id3Info);
- textArea.text += "\n\n-------------------\n\n";
- }
- private function comboBox_change(evt:Event):void {
- var cb:ComboBox = evt.currentTarget as ComboBox;
- playSoundEffect(cb.selectedItem.@source);
- }
- ]]>
- </mx:Script>
- <mx:XML id="playlist" source="data/playlist.xml" />
- <mx:SoundEffect id="soundEffect"
- duration="10000"
- useDuration="true"
- id3="soundEffect_id3(event);" />
- <mx:ApplicationControlBar dock="true">
- <mx:ComboBox id="comboBox"
- prompt="Please select a song..."
- dataProvider="{playlist.song}"
- labelField="@title"
- change="comboBox_change(event);" />
- </mx:ApplicationControlBar>
- <mx:TextArea id="textArea"
- editable="false"
- width="100%"
- height="100%" />
- <mx:LinkButton id="linkButton"
- label="All songs copyright Nine Inch Nails (2008)"
- click="navigateToURL(new URLRequest(NIN_URL), '_blank');" />
- </mx:Application>
下面是歌曲List的XML文件:
Download: playlist.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!-- http://blog.flexexamples.com/2008/03/05/displaying-a-dynamically-loaded-mp3-files-id3-information-in-flex-using-the-soundeffect-class/ -->
- <playlist>
- <song title="Track 1" source="assets/01 Ghosts I.mp3" />
- <song title="Track 2" source="assets/02 Ghosts I.mp3" />
- <song title="Track 3" source="assets/03 Ghosts I.mp3" />
- <song title="Track 4" source="assets/04 Ghosts I.mp3" />
- <song title="Track 5" source="assets/05 Ghosts I.mp3" />
- <song title="Track 6" source="assets/06 Ghosts I.mp3" />
- <song title="Track 7" source="assets/07 Ghosts I.mp3" />
- <song title="Track 8" source="assets/08 Ghosts I.mp3" />
- <song title="Track 9" source="assets/09 Ghosts I.mp3" />
- </playlist>
到这里直接察看源代码,下面是执行Demo。
注意:这里的SoundEffect类每首歌只播放前面的10秒,如果你想播放全部的话,只要简单的在<mx:SoundEffect />tag中省略掉duration属性,并且将useDuration设置为false就可以了。
原文作者:Peter deHaan 翻译:minidxer
Topics:
Flex |
Tags: Flex, ID3, ID3Info, isPlaying, SoundEffect, uration, useDuration.