as3.0 和XML 做的一个音乐播放器
package
{
import flash.display.Sprite;
import fl.controls.*;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.media.Sound;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import fl.events.SliderEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.display.MovieClip;
import fl.containers.UILoader;
import flash.display.Loader;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.display.Graphics;
public class mp3 extends Sprite
{
private var startBtn:Button;
private var stopBtn:Button;
private var preBtn:Button;
private var afterBtn:Button;
private var sound:Sound;
private var urlLoader:URLLoader;
private var musicUrl:URLRequest;
private var beiJingUrl:URLRequest;
private var musicArr:Array = [];
private var musicXml:XML;
private var i:uint = 0;
private var p:Number = 0;
private var m:uint;
private var str:String;
private var channel:SoundChannel = new SoundChannel();//SoundChannel 类控制应用程序中的声音
private var soundtransform:SoundTransform = new SoundTransform();//SoundTransform 类包含控制音量和平移的属性
private var slider:Slider;
private var tiaoJie:Slider;
private var listTextField:TextField = new TextField();
private var songFormat:TextFormat;
public function mp3()
{
beiJing();
btn();
music();
startBtn.addEventListener(MouseEvent.CLICK,onStartClick);
stopBtn.addEventListener(MouseEvent.CLICK,onStopClick);
preBtn.addEventListener(MouseEvent.CLICK,onPreClick);
afterBtn.addEventListener(MouseEvent.CLICK,onAfterClick);
slider.addEventListener(SliderEvent.CHANGE,onSliderClick);
tiaoJie.addEventListener(SliderEvent.CHANGE,onTiaoJieClick);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
var bytes:ByteArray = new ByteArray();//ByteArray 类提供用于优化读取、写入以及处理二进制数据的方法和属性
const PLOT_HEIGHT:int = 200;
const CHANNEL_LENGTH:int = 256;
SoundMixer.computeSpectrum(bytes, true, 0);//SoundMixer 类包含 SWF 文件中全局声音控件的静态属性和方法.
/*computeSpectrum(outputArray:ByteArray, FFTMode:Boolean = false, stretchFactor:int = 0):void
[静态] 获取当前声音波形的快照,并将其放在指定的 ByteArray 对象中*/
var g:Graphics = this.graphics; //绘制波形普
g.clear();
g.lineStyle(0, 0x00ff00);
g.beginFill(0x00ff00);
g.moveTo(0, PLOT_HEIGHT);
var n:Number = 0;
for (var i:int = 0; i < CHANNEL_LENGTH; i++)
{
n = (bytes.readFloat() * PLOT_HEIGHT);
g.lineTo(i * 2, PLOT_HEIGHT - n);
}
g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
g.endFill();
g.lineStyle(0, 0x00ff00);
g.beginFill(0x00ff00, 0.5);
g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
for (i = CHANNEL_LENGTH; i > 0; i--)
{
n = (bytes.readFloat() * PLOT_HEIGHT);
g.lineTo(i * 2, PLOT_HEIGHT - n);
}
g.lineTo(0, PLOT_HEIGHT);
g.endFill();
}
private function btn():void
{
startBtn = new Button();
startBtn.x = 180;
startBtn.y = 300;
startBtn.width = 40;
startBtn.height = 40;
startBtn.label = "播放";
addChild(startBtn);
stopBtn = new Button();
stopBtn.x = 280;
stopBtn.y = 300;
stopBtn.width = 40;
stopBtn.height = 40;
stopBtn.label = "停止";
addChild(stopBtn);
preBtn = new Button();
preBtn.x = 230;
preBtn.y = 250;
preBtn.width = 40;
preBtn.height = 40;
preBtn.label = ">>";
addChild(preBtn);
afterBtn = new Button();
afterBtn.x = 230;
afterBtn.y = 350;
afterBtn.width = 40;
afterBtn.height = 40;
afterBtn.label = "<<";
addChild(afterBtn);
slider = new Slider();
slider.x = 350;
slider.y = 320;
addChild(slider);
tiaoJie = new Slider();
tiaoJie.x = 350;
tiaoJie.y = 300;
addChild(tiaoJie);
//addChild(beiJing);
}
private function music():void
{
urlLoader = new URLLoader();
musicUrl = new URLRequest("music.xml");
urlLoader.load(musicUrl);
urlLoader.addEventListener(Event.COMPLETE,onGetMusicXmlHandler);
}
private function onGetMusicXmlHandler(e:Event):void
{
musicXml = new XML(urlLoader.data);
for (var j:uint = 0; j < musicXml.children().length(); j++)
{
musicArr[j] = (musicXml.music[j]);
//trace(musicArr[j]);
m = musicArr.length;
//trace(m)
}
trace(musicArr);
//trace(musicArr.length);
sound = new Sound();
channel = new SoundChannel();
sound.load(new URLRequest(musicArr[i]));
slider.value = 5;//音量调节
soundtransform.volume = slider.value * 0.1; /*volume : Number
一个数字,介于 0 到 1 的范围内,指示音量控制设置*/
channel.soundTransform = soundtransform;
}
private function onStartClick(MouseEvent):void
{
if (startBtn.label == "播放")
{
channel = sound.play(p);
startBtn.label = "暂停";
channel.addEventListener(Event.SOUND_COMPLETE,onSoundCompleteHandler);
}
else
{
p = channel.position;
channel.stop();
startBtn.label = "播放";
}
txt();
}
private function onStopClick(e:MouseEvent):void
{
if (stopBtn.label == "停止")
{
p = 0;
channel.stop();
startBtn.label = "播放";
}
txt();
}
private function onPreClick(e:MouseEvent):void ////设置上一首按钮
{
if (i >= m-1)
{
i = m - 1;
//trace(i);
p = 0;
channel.stop();
sound = new Sound();
sound.load(new URLRequest(musicArr[i]));
channel = sound.play(p);
}
else
{
channel.stop();
i++;
//trace(i);
sound = new Sound();
sound.load(new URLRequest(musicArr[i]));
channel = sound.play(p);
startBtn.label = "暂停";
channel.addEventListener(Event.SOUND_COMPLETE,onSoundCompleteHandler); //调用顺序播放方法
}
txt();
}
private function onAfterClick(e:MouseEvent):void //设置下一首按钮
{
if (i <=0)
{
i = 0;
p = 0;
//trace(i);
channel.stop();
sound = new Sound();
sound.load(new URLRequest(musicArr[i]));
channel = sound.play(p);
}
else
{
channel.stop();
i--;
//trace(i);
sound = new Sound();
sound.load(new URLRequest(musicArr[i]));
channel = sound.play(p);
startBtn.label = "暂停";
channel.addEventListener(Event.SOUND_COMPLETE,onSoundCompleteHandler); //调用顺序播放方法
}
txt();
}
private function onSliderClick(e:SliderEvent):void //此方法通过控制slider组件调节音量
{
soundtransform.volume = (e.target.value)*0.1;
channel.soundTransform = soundtransform;
}
private function onTiaoJieClick(e:SliderEvent):void //此方法通过控制slider组件调节播放进度
{
channel.stop();
tiaoJie.maximum = sound.length;
//tiaoJie.minimum = channel.position;
sound = new Sound();
sound.load(new URLRequest(musicArr[i]));
p = tiaoJie.value;
channel = sound.play(p);
p = 0;
channel.addEventListener(Event.SOUND_COMPLETE,onSoundCompleteHandler); //调用顺序播放方法
txt();
}
private function onSoundCompleteHandler(e:Event):void //此方法设置点击播放或下一首后如果没有操作就按顺序自动播放
{
if (i <= m -1)
{
p = 0;
i++;
sound = new Sound();
sound.load(new URLRequest(musicArr[i]));
channel = sound.play(p);
}
channel.addEventListener(Event.SOUND_COMPLETE,onSoundCompleteHandler); //调用顺序播放方法
txt();
}
private function txt():void //设置显示歌曲文本框的样式
{
listTextField.autoSize = TextFieldAutoSize.LEFT;
listTextField.border = true;
listTextField.borderColor = 0x0000ff;
listTextField.background = true;
listTextField.backgroundColor = 0x003300;
listTextField.x = 200;
listTextField.y = 200;
listTextField.textColor = 0x00ff00;
listTextField.text = (musicXml.music[i]);//显示文本内容为xml内的歌曲名
addChild(listTextField);
//listTextField.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function beiJing():void //加载背景图片
{
var urlLoader:Loader = new Loader();
beiJingUrl = new URLRequest("beiJing.jpg");
urlLoader.load(beiJingUrl);
urlLoader.alpha = 0.5;
addChild(urlLoader);
}
}
}
//xml内容
<?xml version="1.0" encoding="UTF-8"?>
<root>
<music>后弦-夏天的味道(铃声).mp3</music>
<music>擦肩而过.mp3</music>
<music>任贤齐 - 沧海一声笑.mp3</music>
<music>刘德华-冰雨.mp3</music>
<music>后街男孩 - 经典老歌.mp3</music>
<music>大海 - 张雨生.mp3</music>
<music>方大同 - Red Bean.mp3</music>
<music>高安-在心里从此永远有个你.mp3</music>
<music>欢子 - 伤心的时候可以听情歌.mp3</music>
<music>姜玉阳-痛彻心扉.mp3</music>
<music>徐誉滕-天使的翅膀.mp3</music>
<music>张国荣 - 千千阙歌.mp3</music>
</root>