编程教学最全的声音控制方法.docx

上传人:b****3 文档编号:1882213 上传时间:2022-10-24 格式:DOCX 页数:9 大小:303.58KB
下载 相关 举报
编程教学最全的声音控制方法.docx_第1页
第1页 / 共9页
编程教学最全的声音控制方法.docx_第2页
第2页 / 共9页
编程教学最全的声音控制方法.docx_第3页
第3页 / 共9页
编程教学最全的声音控制方法.docx_第4页
第4页 / 共9页
编程教学最全的声音控制方法.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

编程教学最全的声音控制方法.docx

《编程教学最全的声音控制方法.docx》由会员分享,可在线阅读,更多相关《编程教学最全的声音控制方法.docx(9页珍藏版)》请在冰豆网上搜索。

编程教学最全的声音控制方法.docx

[AS3.0编程教学]最全的声音控制方法

网上做flash音乐播放器的人不少,这个作品主要是对声音的外部读取,然后保存,然后控制播放,暂停,停止等操作,今天这个作品就是向大家展示这些操作的方法。

1.首先我们新建一个文件,在舞台上摆出下面这些按钮,我们今天对这个声音文件的操纵就如按钮所示:

动手之前我们按下Ctrl+Shift+F12,打开ActionScript设置,将“自动申明舞台对象”打钩取消,我们将每个对象自己用Public声明,这样做的好处是开发时每个元件的属性方便引用和提醒。

2.3

我们新建一个文档类,首先声明舞台上这些按钮,并定义声音变量:

testSound,控制变量testChannel,testTrans,testPosition。

publicvarbtnPlay:

SimpleButton;

publicvarbtnPause:

SimpleButton;

publicvarbtnStop:

SimpleButton;

publicvarbtnQuick:

SimpleButton;

publicvarbtnVocUp:

SimpleButton;

publicvarbtnVocDown:

SimpleButton;

publicvarbtnPanUp:

SimpleButton;

publicvarbtnPanDown:

SimpleButton;

privatevartestSound:

Sound;

privatevartestChannel:

SoundChannel;

privatevartestTrans:

SoundTransform;

privatevartestPosition:

Number=0;

3.4

首先用下面代码将一首叫做“test.mp3"的音乐加载到舞台。

publicfunctionTestSoundMain(){

testSound=newSound();

testChannel=newSoundChannel();

testTrans=newSoundTransform();

testSound.load(newURLRequest("test.mp3"));

testSound.addEventListener(Event.COMPLETE,soundLoadOver);}

privatefunctionsoundLoadOver(e:

Event):

void{

testSound.removeEventListener(Event.COMPLETE,soundLoadOver);

soudLoad=true;}

4.播放按钮功能。

控制音乐播放的按钮,单击后音乐开始播放,并记录音乐的SoundChannel属性。

为了防止连击,我们定义一个isSoundPlay布尔变量判断音乐是否在播放中。

//播放按钮功能

privatefunctionplayBtnEvent():

void{

btnPlay.addEventListener(MouseEvent.CLICK,soundPlay);}

privatefunctionsoundPlay(e:

MouseEvent):

void{

if(isSoundPlay)return;

isSoundPlay=true;

testChannel=testSound.play(testPosition);}

5.暂停按钮功能,该按钮让音乐暂停掉,为了能继续播放,我们需要记录下此时testChannel的位置,然后播放按钮单击时可以继续播放

//暂停按钮功能

privatefunctionpauseBtnEvent():

void{

btnPause.addEventListener(MouseEvent.CLICK,soudPause);}

privatefunctionsoudPause(e:

MouseEvent):

void{

if(!

isSoundPlay)return;

isSoundPlay=false;

testPosition=testChannel.position;

testChannel.stop();}

6.停止按钮功能,单击后音乐停止播放,记录位置归0.

//停止按钮功能

privatefunctionstopBtnEvent():

void{

btnStop.addEventListener(MouseEvent.CLICK,soundStop);}

privatefunctionsoundStop(e:

MouseEvent):

void{

isSoundPlay=false;

testPosition=0

testChannel.stop();}

7.快进声音。

单击该按钮时,我们让声音从当前位置向前播放500毫秒,也就是快进半秒。

//快进按钮功能

privatefunctionqucikBtnEvent():

void{

btnQuick.addEventListener(MouseEvent.CLICK,soudQuickPlay);}

privatefunctionsoudQuickPlay(e:

MouseEvent):

void{

if(!

isSoundPlay)return;

testPosition=testChannel.position;

testChannel.stop();

testChannel=testSound.play(testPosition+500);}

8.设定声音的音量增加。

控制音量就需要soundTransform对象了,它其实是testChanel的soundTransform属性而已,通过它来控制音量。

//音量增加

privatefunctionvolumeUpBtnEvent():

void{

btnVocUp.addEventListener(MouseEvent.CLICK,upSoudVoc);}

privatefunctionupSoudVoc(e:

MouseEvent):

void{

if(!

isSoundPlay)return;

testTrans=testChannel.soundTransform;

varaddedVoc:

Number=testTrans.volume>1?

1:

(testTrans.volume+0.05);

testTrans.volume=addedVoc;

testChannel.soundTransform=testTrans;}

9.设定声音的音量减小。

//音量减小

privatefunctionvolumeDownBtnEvent():

void{

btnVocDown.addEventListener(MouseEvent.CLICK,downSoundVoc);}

privatefunctiondownSoundVoc(e:

MouseEvent):

void{

if(!

isSoundPlay)return;

testTrans=testChannel.soundTransform;

vardownVoc:

Number=testTrans.volume<0?

0:

(testTrans.volume-0.05);

testTrans.volume=downVoc;

testChannel.soundTransform=testTrans;}

10.设定声音的平衡度向右,点击此按钮声音的平衡性会右移,直到变成右声道。

//平衡向右移动

privatefunctionpanRightBtnEvent():

void{

btnPanUp.addEventListener(MouseEvent.CLICK,upSoundPan);}

privatefunctionupSoundPan(e:

MouseEvent):

void{

if(!

isSoundPlay)return;

testTrans=testChannel.soundTransform;

varaddedPan:

Number=testTrans.pan>1?

1:

(testTrans.pan+0.05);

testTrans.pan=addedPan;

testChannel.soundTransform=testTrans;}

11.设定声音的平衡度向左,点击此按钮声音的平衡性会左移,直到变成左声道。

//平衡向左移动

privatefunctionpanLeftBtnEvent():

void{

btnPanDown.addEventListener(MouseEvent.CLICK,downSoundPan);}

privatefunctiondownSoundPan(e:

MouseEvent):

void{

if(!

isSoundPlay)return;

testTrans=testChannel.soundTransform;

vardownPan:

Number=testTrans.pan<0?

0:

(testTrans.pan-0.05);

testTrans.pan=downPan;

testChannel.soundTransform=testTrans;}

12.最后别忘了所有你定义的函数都要写到音乐加载完成的那个函数里执行,或者构造函数也可以。

就像下面这样子:

//加载音乐并控制播放

privatefunctionsoundLoadOver(e:

Event):

void{

testSound.removeEventListener(Event.COMPLETE,soundLoadOver);

soudLoad=true;

playBtnEvent();

pauseBtnEvent();

stopBtnEvent();

qucikBtnEvent();

volumeUpBtnEvent();

volumeDownBtnEvent();

panRightBtnEvent();

panLeftBtnEvent();}

13.所有这些一个个代码合并到一起,就是我们主的文档类。

制作完毕!

注意事项

我没有写声音加载过程和加载错误的监听,如果你的操作没有声音,那一定是你的声音文件有问题,换个文件试试;还有留意下方的报错提示。

其实SoundTransform类还有四个读写属性:

leftToLeft,leftToRight,rightToLeft,rightToRight,用来设置左右输入在左右扬声器的音量,有兴趣的同学可以细微研究:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1