flash中聲音控制實(shí)現(xiàn)代碼
發(fā)布時間:2013-01-24 17:11:03 作者:佚名
我要評論

有時候我們需要在flash中對音樂進(jìn)程控制,這里簡單整理下flash中常用的聲音控制代碼,需要的朋友可以收藏下
簡單播放音樂
1. 首先打開新的Flash文件, 把聲音導(dǎo)入庫中 (還摸不清介面的朋友就按ctrl+r)
2. 導(dǎo)入之后到庫中定義聲音的ID, 如圖:
*** 這里的ID和場景上的實(shí)體名是不一樣的 ***
3. 接下來就在第一幀編寫代碼, 如下
mySound = new Sound(); //定義聲音類
mySound.attachSound("tomato"); //提取庫中我們所設(shè)定的ID
mySound.start(); //開始播放聲音
4. 測試結(jié)果..
音樂的開始, 停止和循環(huán)
mySound.start([Secondsoffset], loop);
start當(dāng)中的兩個參數(shù)分別為Secondsoffset, Seconds就是秒數(shù)而offset就是抵消或取消的意思...所以簡單的說就是取消開始播放,以秒數(shù)來計算... 沒有定義的話就是0, 另外一個loop就是循環(huán)了...
mySound.start(5, 99);
這個意思就是音樂從第5秒開始播放, 并循環(huán)99次, 這里提供了個例子為mySound.start(0,99);
點(diǎn)擊瀏覽該文件
mySound.stop();
mySound.stop("tomato"); //如果new Sound沒有定義的話就這樣使用, 不然多個聲音會全部停止
這個很簡單不用解釋了吧...就是停止音樂
我們看到某些網(wǎng)站所使用的一個按鈕控制播放和停止的效果就是使用這些就可以達(dá)成了, 如:
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start(0,99); //音樂開始播放并循環(huán)99次
var music = true; //定義一個變量記錄目前音樂是否是在播放, 因為音樂已經(jīng)播放所以設(shè)定為true
btn.onRelease = function() {
if(music) { //當(dāng)變量為true時就表示音樂是在播放
mySound.stop(); //使用stop設(shè)定音樂停止
music = false; //變量記錄false為音樂停止
} else { //以下的和以上相反
mySound.start(0,99);
music = play;
}
}
setPan 和 setVolume
mySound.setPan(pan);
pan的值是介于 -100 到 100, 用意在于設(shè)定喇叭的平衡... -100為只能左邊的喇叭聽到聲音, 100為右邊的, 而0就是平衡點(diǎn), 兩個喇叭都能聽到聲音
例如:
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start(0, 10);
var speaker = -100; //變量設(shè)定為-100, 即是從左邊喇叭開始
mySound.setPan(speaker); //設(shè)定喇叭平衡
function pan() { //設(shè)定函數(shù)并通過setInterval每秒調(diào)整平衡
speaker += 20; //每秒平衡偏移20
mySound.setPan(speaker); //設(shè)定喇叭的平衡
if(speaker > 100) { //當(dāng)音樂完全偏移到右邊喇叭播放的時候就停止
mySound.stop();
clearInterval(p);
}
}
var p = setInterval(pan, 1000); //開始每秒執(zhí)行喇叭平衡
mySound.setVolume(volume);
volume為0 - 100, 0為靜音, 100為最大
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start(0, 99);
var top = key.vol._y; //定義拖動按鈕的最高點(diǎn)
var left = right = key.vol._x; //定義拖動左右的范圍
var bottom = key.vol._y+100; //定義拖動按鈕的最低點(diǎn)
key.vol.onPress = function() {
this.startDrag(true,left,top,right,bottom); //按鈕按下拖動范圍
}
onEnterFrame = function() {
v = int(key.textInput.value.text); //取得輸入框內(nèi)的值
mySound.setVolume(v); //設(shè)定音量
}
Position, Duration 和 暫停
mySound.position();
唯讀指令, 主要是取得目前播放音樂的毫秒數(shù)(1秒 = 1000毫秒), 在音樂播放之后才能夠取得, 在一開始start()之后使用是無法取得的
mySound.duration();
唯讀指令, 主要是取得音樂的總毫秒數(shù) 要使音樂暫停, 播放的時候再繼續(xù)之前暫停的位置開始播放, 我們可以先取得按鈕按下暫停時的position以取得位置, 然后再次按下播放的時候就使用start()當(dāng)中的SecondsOffset使音樂從暫停的部分開始播放, 如:
mySound = new Sound();
mySound.attachSound("tomato");
var SecondsOffset = 0; //設(shè)定SecondsOffset為0
p1.onRelease = function() {
mySound.start(SecondsOffset, 0); //播放按鈕按下開始從0offset播放
}
p2.onRelease = function() {
SecondsOffset = mySound.position/1000; //暫停按鈕按下時記錄目前位置并換成秒數(shù)
mySound.stop(); //音樂暫停
}
onEnterFrame = function() { //這里是循環(huán)部分
if(mySound.position == mySound.duration) { //如果播放的毫秒數(shù)等于音樂總毫秒數(shù)
mySound.start(0, 99); //開始循環(huán)播放99次
}
}
只要會了以上的方法, 倒退播放和快速播放就非常簡單了, 如下:
1. 場景上建立兩個按鈕, 分別為(倒退 rev 和 快速播放 ff)
2. 在第一幀使用以下代碼 :
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start();
var SecondsOffset = 0;
var reverse = foward = false; //設(shè)定倒退和前進(jìn)變量為false
onEnterFrame = function () {
if (reverse && mySound.position > 0) { //當(dāng)?shù)雇税聪虑乙魳访霐?shù)大于0
mySound.stop(); //音樂停止
SecondsOffset -= .5; //offset倒退0.5秒
mySound.start(SecondsOffset, 0); //音樂從倒退的0.5秒開始播放
}
if (foward && mySound.position <= mySound.duration) { //當(dāng)快速播放按下且音樂不為結(jié)束
mySound.stop();
SecondsOffset += .5; //offset前進(jìn)0.5秒
mySound.start(SecondsOffset, 0);
}
rev.onPress = function() { //當(dāng)?shù)雇税聪虏⑷〉胮osition
SecondsOffset = mySound.position/1000;
reverse = true; //reverse變量為true
};
rev.onRelease = function() { //當(dāng)?shù)雇朔砰_就設(shè)定reverse變量為false
reverse = false;
}
ff.onPress = function() { //同上
SecondsOffset = mySound.position/1000;
foward = true;
};
ff.onRelease = function() {
foward = false;
}
};
至于loadSound部分就寫一下進(jìn)度條的寫法
1. 在場景上建立一個為100%長度的MC(loadBar)
2. 在第一幀使用以下代碼 :
onLoad = function () {
mySound = new Sound();
mySound.loadSound("tomato.mp3"); //載入同一目錄中的MP3
var percent = 0; //%一開始為0
loadBar._xscale = percent; //進(jìn)度條的寬度比例為percent
};
onEnterFrame = function () {
mySoundBytesTotal = mySound.getBytesTotal(); //取得文件的size
mySoundBytesLoaded = mySound.getBytesLoaded(); //取得目前文件所載入的size
percent = int(mySoundBytesLoaded/mySoundBytesTotal*100); //計算出文件所載入的比例
loadBar._xscale = percent; //設(shè)定進(jìn)度條寬度比例
if (percent>=100) { //當(dāng)完全載入之后
delete onEnterFrame; //刪除循環(huán)
mySound.start(); //音樂開始播放
}
};
Q1. 為何loadMovie當(dāng)中的swf音樂無法播放?
ans: 只要在swf當(dāng)中把 mySound = new Sound() 換成 mySound = new Sound(this) 就可以了
Q2. 為何不能同時設(shè)定兩首音樂不同的音量? ans: 一般你們會這樣使用AS
mySound1 = new Sound();
mySound1.attachSound("tomato1");
mySound1.start();
mySound2 = new Sound();
mySound2.attachSound("tomato2");
mySound2.setVolume(50); //另外一首音量為50
mySound2.start();
但這樣是錯誤的, 正確方法是分別把音樂分開在不同的層當(dāng)中 :
mySound1 = new Sound(this);
mySound1.attachSound("tomato1");
mySound1.start();
createEmptyMovieClip("mc", 0);
mySound2 = new Sound(mc);
mySound2.attachSound("tomato2");
mySound2.setVolume(50);
mySound2.start();
1. 首先打開新的Flash文件, 把聲音導(dǎo)入庫中 (還摸不清介面的朋友就按ctrl+r)
2. 導(dǎo)入之后到庫中定義聲音的ID, 如圖:
*** 這里的ID和場景上的實(shí)體名是不一樣的 ***
3. 接下來就在第一幀編寫代碼, 如下
mySound = new Sound(); //定義聲音類
mySound.attachSound("tomato"); //提取庫中我們所設(shè)定的ID
mySound.start(); //開始播放聲音
4. 測試結(jié)果..
音樂的開始, 停止和循環(huán)
mySound.start([Secondsoffset], loop);
start當(dāng)中的兩個參數(shù)分別為Secondsoffset, Seconds就是秒數(shù)而offset就是抵消或取消的意思...所以簡單的說就是取消開始播放,以秒數(shù)來計算... 沒有定義的話就是0, 另外一個loop就是循環(huán)了...
mySound.start(5, 99);
這個意思就是音樂從第5秒開始播放, 并循環(huán)99次, 這里提供了個例子為mySound.start(0,99);
點(diǎn)擊瀏覽該文件
mySound.stop();
mySound.stop("tomato"); //如果new Sound沒有定義的話就這樣使用, 不然多個聲音會全部停止
這個很簡單不用解釋了吧...就是停止音樂
我們看到某些網(wǎng)站所使用的一個按鈕控制播放和停止的效果就是使用這些就可以達(dá)成了, 如:
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start(0,99); //音樂開始播放并循環(huán)99次
var music = true; //定義一個變量記錄目前音樂是否是在播放, 因為音樂已經(jīng)播放所以設(shè)定為true
btn.onRelease = function() {
if(music) { //當(dāng)變量為true時就表示音樂是在播放
mySound.stop(); //使用stop設(shè)定音樂停止
music = false; //變量記錄false為音樂停止
} else { //以下的和以上相反
mySound.start(0,99);
music = play;
}
}
setPan 和 setVolume
mySound.setPan(pan);
pan的值是介于 -100 到 100, 用意在于設(shè)定喇叭的平衡... -100為只能左邊的喇叭聽到聲音, 100為右邊的, 而0就是平衡點(diǎn), 兩個喇叭都能聽到聲音
例如:
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start(0, 10);
var speaker = -100; //變量設(shè)定為-100, 即是從左邊喇叭開始
mySound.setPan(speaker); //設(shè)定喇叭平衡
function pan() { //設(shè)定函數(shù)并通過setInterval每秒調(diào)整平衡
speaker += 20; //每秒平衡偏移20
mySound.setPan(speaker); //設(shè)定喇叭的平衡
if(speaker > 100) { //當(dāng)音樂完全偏移到右邊喇叭播放的時候就停止
mySound.stop();
clearInterval(p);
}
}
var p = setInterval(pan, 1000); //開始每秒執(zhí)行喇叭平衡
mySound.setVolume(volume);
volume為0 - 100, 0為靜音, 100為最大
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start(0, 99);
var top = key.vol._y; //定義拖動按鈕的最高點(diǎn)
var left = right = key.vol._x; //定義拖動左右的范圍
var bottom = key.vol._y+100; //定義拖動按鈕的最低點(diǎn)
key.vol.onPress = function() {
this.startDrag(true,left,top,right,bottom); //按鈕按下拖動范圍
}
onEnterFrame = function() {
v = int(key.textInput.value.text); //取得輸入框內(nèi)的值
mySound.setVolume(v); //設(shè)定音量
}
Position, Duration 和 暫停
mySound.position();
唯讀指令, 主要是取得目前播放音樂的毫秒數(shù)(1秒 = 1000毫秒), 在音樂播放之后才能夠取得, 在一開始start()之后使用是無法取得的
mySound.duration();
唯讀指令, 主要是取得音樂的總毫秒數(shù) 要使音樂暫停, 播放的時候再繼續(xù)之前暫停的位置開始播放, 我們可以先取得按鈕按下暫停時的position以取得位置, 然后再次按下播放的時候就使用start()當(dāng)中的SecondsOffset使音樂從暫停的部分開始播放, 如:
mySound = new Sound();
mySound.attachSound("tomato");
var SecondsOffset = 0; //設(shè)定SecondsOffset為0
p1.onRelease = function() {
mySound.start(SecondsOffset, 0); //播放按鈕按下開始從0offset播放
}
p2.onRelease = function() {
SecondsOffset = mySound.position/1000; //暫停按鈕按下時記錄目前位置并換成秒數(shù)
mySound.stop(); //音樂暫停
}
onEnterFrame = function() { //這里是循環(huán)部分
if(mySound.position == mySound.duration) { //如果播放的毫秒數(shù)等于音樂總毫秒數(shù)
mySound.start(0, 99); //開始循環(huán)播放99次
}
}
只要會了以上的方法, 倒退播放和快速播放就非常簡單了, 如下:
1. 場景上建立兩個按鈕, 分別為(倒退 rev 和 快速播放 ff)
2. 在第一幀使用以下代碼 :
mySound = new Sound();
mySound.attachSound("tomato");
mySound.start();
var SecondsOffset = 0;
var reverse = foward = false; //設(shè)定倒退和前進(jìn)變量為false
onEnterFrame = function () {
if (reverse && mySound.position > 0) { //當(dāng)?shù)雇税聪虑乙魳访霐?shù)大于0
mySound.stop(); //音樂停止
SecondsOffset -= .5; //offset倒退0.5秒
mySound.start(SecondsOffset, 0); //音樂從倒退的0.5秒開始播放
}
if (foward && mySound.position <= mySound.duration) { //當(dāng)快速播放按下且音樂不為結(jié)束
mySound.stop();
SecondsOffset += .5; //offset前進(jìn)0.5秒
mySound.start(SecondsOffset, 0);
}
rev.onPress = function() { //當(dāng)?shù)雇税聪虏⑷〉胮osition
SecondsOffset = mySound.position/1000;
reverse = true; //reverse變量為true
};
rev.onRelease = function() { //當(dāng)?shù)雇朔砰_就設(shè)定reverse變量為false
reverse = false;
}
ff.onPress = function() { //同上
SecondsOffset = mySound.position/1000;
foward = true;
};
ff.onRelease = function() {
foward = false;
}
};
至于loadSound部分就寫一下進(jìn)度條的寫法
1. 在場景上建立一個為100%長度的MC(loadBar)
2. 在第一幀使用以下代碼 :
onLoad = function () {
mySound = new Sound();
mySound.loadSound("tomato.mp3"); //載入同一目錄中的MP3
var percent = 0; //%一開始為0
loadBar._xscale = percent; //進(jìn)度條的寬度比例為percent
};
onEnterFrame = function () {
mySoundBytesTotal = mySound.getBytesTotal(); //取得文件的size
mySoundBytesLoaded = mySound.getBytesLoaded(); //取得目前文件所載入的size
percent = int(mySoundBytesLoaded/mySoundBytesTotal*100); //計算出文件所載入的比例
loadBar._xscale = percent; //設(shè)定進(jìn)度條寬度比例
if (percent>=100) { //當(dāng)完全載入之后
delete onEnterFrame; //刪除循環(huán)
mySound.start(); //音樂開始播放
}
};
Q1. 為何loadMovie當(dāng)中的swf音樂無法播放?
ans: 只要在swf當(dāng)中把 mySound = new Sound() 換成 mySound = new Sound(this) 就可以了
Q2. 為何不能同時設(shè)定兩首音樂不同的音量? ans: 一般你們會這樣使用AS
mySound1 = new Sound();
mySound1.attachSound("tomato1");
mySound1.start();
mySound2 = new Sound();
mySound2.attachSound("tomato2");
mySound2.setVolume(50); //另外一首音量為50
mySound2.start();
但這樣是錯誤的, 正確方法是分別把音樂分開在不同的層當(dāng)中 :
mySound1 = new Sound(this);
mySound1.attachSound("tomato1");
mySound1.start();
createEmptyMovieClip("mc", 0);
mySound2 = new Sound(mc);
mySound2.attachSound("tomato2");
mySound2.setVolume(50);
mySound2.start();
相關(guān)文章
- flash怎么畫下水道口矢量圖?生活中我們經(jīng)常能遇到排水口,想要畫一個矢量的排水道扣,該怎么繪制呢?下面我們就來看看flash排水管道口的畫法2022-07-23
flash怎么畫雪景素材? flash繪制一個森林雪景矢量背景圖的技巧
flash怎么畫雪景素材?很喜歡雪景,想要繪制一款森林雪景圖,該怎么繪制這幅適量背景圖呢?下面我們就來看看flash繪制一個森林雪景矢量背景圖的技巧2022-06-09flashcs6怎么建模3d立體字? flash立體字制作教程
flashcs6怎么建模3d立體字?flash中想要制作一款立體自,該怎么制作三維立體的文字呢?下面,我們就來看看flash立體字制作教程,詳細(xì)請看下文介紹2022-06-06flash圣誕樹怎么畫? flash繪制一棵圣誕樹的矢量圖的技巧
flash圣誕樹怎么畫?flash中想要畫一顆圣誕樹素材,該怎么繪制圣誕樹呢?下面我們就來看看flash繪制一棵圣誕樹的矢量圖的技巧,詳細(xì)請看下文介紹2022-06-06flash cs6怎么畫油罐車圖案? flash簡筆畫油罐車的畫法
flash cs6怎么畫油罐車圖案?flash中想要繪制一個油罐車圖形,該怎么繪制呢?下面我們就來看看flash簡筆畫油罐車的畫法,需要的朋友可以參考下2021-04-06- flash怎么畫簡筆畫效果的公雞?flash中想要畫一個簡筆畫的公雞并填充顏色,該怎么繪制大公雞呢?下面我們就來看看flash公雞的畫法,需要的朋友可以參考下2021-04-06
- flash怎么畫簡筆畫長頸鹿?flash中想要手繪長頸鹿,該怎么繪制這個圖形呢?下面我們就來看看flash長頸鹿的手繪方法,需要的朋友可以參考下2021-04-04
flash文件怎么另存為PNG格式? flash導(dǎo)出png圖片的技巧
flash文件怎么另存為PNG格式?flash中想要導(dǎo)出png格式的文件,該怎么操作呢?下面我們就來看看那flash導(dǎo)出png圖片的技巧,需要的朋友可以參考下2021-02-01Animate怎么創(chuàng)建一個配置常數(shù)? Animate配置常數(shù)的添加方法
Animate怎么創(chuàng)建一個配置常數(shù)?Animate中想要添加一個配置常數(shù),該怎么添加呢?下面我們就來看看Animate配置常數(shù)的添加方法,需要的朋友可以參考下2020-11-03Flash濾鏡怎么調(diào)整元件的顏色? Flash濾鏡調(diào)色技巧
Flash濾鏡怎么調(diào)整元件的顏色?Flash中想要給元件變色,方法很多,今天我們就來看看首映Flash濾鏡調(diào)色的技巧,很簡單,需要的朋友可以參考下2020-09-03