分享一个flash播放音乐的类
//定义数组
var myXML = new XML();
var mp3total = new Array();
var mp3name = new Array();
var mp3 = new Array();
//定义s为声音类
var s = new Sound();
//路径
var url = "music/";
//第几首
var id = 0;
//已播放时间
var played = 0;
//剩余时间
var playedTime;
//当前曲目
var currmusic;
//停止
var key = true;
//暂停
var paused = false;
//预读函数
function preloading() {
preload.onEnterFrame = function() {
loaded = s.getBytesLoaded();
total = s.getBytesTotal();
this._width = loaded/total*160;
if (loaded == total) {
delete this.onEnterFrame;
}
};
}
//播放声音函数
function loadsound(x) {
played = 0;
paused = false;
key = true;
s = new Sound();
s.loadSound(url+x, true);
trackname = (id+1)+". - "+mp3name[id];
s.start();
s.onSoundComplete = function() {
id = (id == mp3total.length-1) ? 0 : (id+1);
loadsound(mp3[id]);
};
mix.gotoAndStop(1);
preloading();
}
//XML解码
myXML.ignoreWhite = true;
System.useCodepage = true;
//预读XML
myXML.onLoad = function(success) {
if (success) {
parseXML();
} else {
trace("载入失败");
}
};
//分析XML
function parseXML() {
mp3total = myXML.firstChild.childNodes;
for (var i = 0; i<mp3total.length; i++) {
mp3name[i] = mp3total[i].childNodes[0].firstChild.nodeValue;
mp3[i] = mp3total[i].childNodes[1].firstChild.nodeValue;
}
play();
}
//读取XML
myXML.load(url+"list.xml");
stop();
InitSound = function () {
loadsound(mp3[id]);
var time1 = time2=time3=time4=0;
_root.onEnterFrame = function() {
time1 = int(s.position/60000)<10 ? "0"+int(s.position/60000) : int(s.position/60000);
time2 = int(s.position/1000%60)<10 ? "0"+int(s.position/1000%60) : int(s.position/1000%60);
time3 = int(s.duration/60000)<10 ? "0"+int(s.duration/60000) : int(s.duration/60000);
time4 = int(s.duration/1000%60)<10 ? "0"+int(s.duration/1000%60) : int(s.duration/1000%60);
timeline = time1+":"+time2+" - "+time3+":"+time4;
playedTime = int(time3-time1)+":"+int(time4-time2);
//trace(playedTime);
};
};
xml文件var myXML = new XML();
var mp3total = new Array();
var mp3name = new Array();
var mp3 = new Array();
//定义s为声音类
var s = new Sound();
//路径
var url = "music/";
//第几首
var id = 0;
//已播放时间
var played = 0;
//剩余时间
var playedTime;
//当前曲目
var currmusic;
//停止
var key = true;
//暂停
var paused = false;
//预读函数
function preloading() {
preload.onEnterFrame = function() {
loaded = s.getBytesLoaded();
total = s.getBytesTotal();
this._width = loaded/total*160;
if (loaded == total) {
delete this.onEnterFrame;
}
};
}
//播放声音函数
function loadsound(x) {
played = 0;
paused = false;
key = true;
s = new Sound();
s.loadSound(url+x, true);
trackname = (id+1)+". - "+mp3name[id];
s.start();
s.onSoundComplete = function() {
id = (id == mp3total.length-1) ? 0 : (id+1);
loadsound(mp3[id]);
};
mix.gotoAndStop(1);
preloading();
}
//XML解码
myXML.ignoreWhite = true;
System.useCodepage = true;
//预读XML
myXML.onLoad = function(success) {
if (success) {
parseXML();
} else {
trace("载入失败");
}
};
//分析XML
function parseXML() {
mp3total = myXML.firstChild.childNodes;
for (var i = 0; i<mp3total.length; i++) {
mp3name[i] = mp3total[i].childNodes[0].firstChild.nodeValue;
mp3[i] = mp3total[i].childNodes[1].firstChild.nodeValue;
}
play();
}
//读取XML
myXML.load(url+"list.xml");
stop();
InitSound = function () {
loadsound(mp3[id]);
var time1 = time2=time3=time4=0;
_root.onEnterFrame = function() {
time1 = int(s.position/60000)<10 ? "0"+int(s.position/60000) : int(s.position/60000);
time2 = int(s.position/1000%60)<10 ? "0"+int(s.position/1000%60) : int(s.position/1000%60);
time3 = int(s.duration/60000)<10 ? "0"+int(s.duration/60000) : int(s.duration/60000);
time4 = int(s.duration/1000%60)<10 ? "0"+int(s.duration/1000%60) : int(s.duration/1000%60);
timeline = time1+":"+time2+" - "+time3+":"+time4;
playedTime = int(time3-time1)+":"+int(time4-time2);
//trace(playedTime);
};
};
<?xml version="1.0" encoding="GB2312"?>
<data>
<song>
<title>001_music_001.mp3</title>
<name>001.mp3</name>
</song>
<song>
<title>002_music_002.mp3</title>
<name>002.mp3</name>
</song>
<song>
<title>003_music_003.mp3</title>
<name>003.mp3</name>
</song>
</data>
控制方法<data>
<song>
<title>001_music_001.mp3</title>
<name>001.mp3</name>
</song>
<song>
<title>002_music_002.mp3</title>
<name>002.mp3</name>
</song>
<song>
<title>003_music_003.mp3</title>
<name>003.mp3</name>
</song>
</data>
//上一首
btn1.onPress = function() {
id = id == 0 ? mp3total.length - 1 : id - 1;
loadsound(mp3[id]);
};
//停止
btn2.onPress = function() {
if (key) {
s.stop();
trackname = "STOP";
mix.gotoAndPlay(1);
played = 0;
key = false;
s = new Sound();
}
};
//暂停/播放
btn3.onPress = function() {
if (key) {
if (paused) {
s.start(played / 1000, 1);
trackname = (id + 1) + ". - " + mp3name[id];
mix.gotoAndStop(1);
paused = false;
} else {
played = s.position;
s.stop();
mix.gotoAndPlay(1);
trackname = "PAUSED";
paused = true;
}
} else {
loadsound(mp3[id]);
}
};
//下一首
btn4.onPress = function() {
id = id == mp3total.length - 1 ? 0 : id + 1;
loadsound(mp3[id]);
};
//开始播放声音
loadsound(mp3[id]);
//显示时间
var time1 = time2 = time3 = time4 = 0;
onEnterFrame = function () {
time1 = int(s.position / 60000) < 10 ? "0" + int(s.position / 60000) : int(s.position / 60000);
time2 = int(s.position / 1000 % 60) < 10 ? "0" + int(s.position / 1000 % 60) : int(s.position / 1000 % 60);
time3 = int(s.duration / 60000) < 10 ? "0" + int(s.duration / 60000) : int(s.duration / 60000);
time4 = int(s.duration / 1000 % 60) < 10 ? "0" + int(s.duration / 1000 % 60) : int(s.duration / 1000 % 60);
timeline = time1 + ":" + time2 + " - " + time3 + ":" + time4;
};
stop();
btn1.onPress = function() {
id = id == 0 ? mp3total.length - 1 : id - 1;
loadsound(mp3[id]);
};
//停止
btn2.onPress = function() {
if (key) {
s.stop();
trackname = "STOP";
mix.gotoAndPlay(1);
played = 0;
key = false;
s = new Sound();
}
};
//暂停/播放
btn3.onPress = function() {
if (key) {
if (paused) {
s.start(played / 1000, 1);
trackname = (id + 1) + ". - " + mp3name[id];
mix.gotoAndStop(1);
paused = false;
} else {
played = s.position;
s.stop();
mix.gotoAndPlay(1);
trackname = "PAUSED";
paused = true;
}
} else {
loadsound(mp3[id]);
}
};
//下一首
btn4.onPress = function() {
id = id == mp3total.length - 1 ? 0 : id + 1;
loadsound(mp3[id]);
};
//开始播放声音
loadsound(mp3[id]);
//显示时间
var time1 = time2 = time3 = time4 = 0;
onEnterFrame = function () {
time1 = int(s.position / 60000) < 10 ? "0" + int(s.position / 60000) : int(s.position / 60000);
time2 = int(s.position / 1000 % 60) < 10 ? "0" + int(s.position / 1000 % 60) : int(s.position / 1000 % 60);
time3 = int(s.duration / 60000) < 10 ? "0" + int(s.duration / 60000) : int(s.duration / 60000);
time4 = int(s.duration / 1000 % 60) < 10 ? "0" + int(s.duration / 1000 % 60) : int(s.duration / 1000 % 60);
timeline = time1 + ":" + time2 + " - " + time3 + ":" + time4;
};
stop();