发表时间:33 分钟前
相关文章:
推荐群组: flex
更多相关推荐
直接上代码,在Flash Builder 4中新建 FLEX项目,复制代码可以可以直接运行
需要将 file = "D:\\voice\\3791.MP3"; 改为自己的路径
以后还准备增加流式播放!

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="initMusic()">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
import flash.utils.ByteArray;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.UIComponent;
import mx.events.IndexChangedEvent;
import mx.managers.PopUpManager;
import mx.printing.FlexPrintJob;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
private var serverIP:String = "http://127.0.0.1";
private var ba:ByteArray = new ByteArray();
private var soundRequest:URLRequest;
private var sound:Sound = new Sound(); //使用Sound对象来获取音乐文件播放声音
private var chanel:SoundChannel;
//引入SoundChannel对象来精确控制声音
private var vol:SoundTransform;
//通过设置SoundTransform对象的volume属性控制音量
private var stopPosition:int=0;
//使用stopPosition记录暂停音乐时的位置
private var soundState:int=1;
//使用soundState记录是否静音
private var soundValue:int=2;
//记录静音前的音量
private var playState:int=0;
//使用playState记录声音是否被停止(包括暂停)
private var totalTime:String = "00:00";//语音文件总时间
private var params:Object;
//URL参数列表
private var _sperite:Sprite;
//用于绘制声音波形
//初始化
private function initMusic():void{
var args:Object = getParams();
//获取URL中的参数列表
var file:String = serverIP + args.audio+".mp3";//得到语音文件名
// file = "http://127.0.0.1/voice/3416.mp3";
//对播放控制按钮和滑动条添加鼠标动作监听
soundBtn.addEventListener(MouseEvent.CLICK,onSoundClick);//有声音
soundBtnJ.addEventListener(MouseEvent.CLICK,onSoundClick);//静音
playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClick);//播放
pauseBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClick);//暂停
proccessBar.addEventListener(MouseEvent.MOUSE_DOWN,onProccessBarDown);//播放进度条
proccessBar.addEventListener(MouseEvent.MOUSE_UP,onProccessBarUp);//播放进度条
//组件初始状态设置
soundSlide.value=2;//默认音量等于2
if(file != null && file != ""){
var buffer:SoundLoaderContext = new SoundLoaderContext(5000);
soundRequest = new URLRequest(file);
sound.load(soundRequest,buffer);
sound.addEventListener(Event.COMPLETE, loaded);//监听加载事件
sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);//缓冲事件
}
//控制音量
vol = new SoundTransform();
vol.volume = soundSlide.value;
}
//缓冲事件方法
private function progressHandler(event:ProgressEvent):void{
//计算缓冲方框的宽度(滑块本身也有一定的宽度,减去约10个像素宽度)
if (sound.bytesLoaded>0){
bufferRect.width = sound.bytesLoaded / sound.bytesTotal*(proccessBar.width);
}
}
private function onProccessBarDown(e:MouseEvent):void{
this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onProccessBarUp(e:MouseEvent):void{
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
//读取语音数据,一边取一边画
private function loaded(event:Event):void {
var width:Number = box.width;
var height:Number =box.height;
//获取语音长度,毫秒
var lengthTime:Number = sound.length;
totalTime = formatTime(lengthTime);
playTime.text = totalTime;
_sperite = new Sprite();//建立影片精灵
var uicomponent:UIComponent = new UIComponent();
uicomponent.addChild(_sperite);
box.addElement(uicomponent);
doDraw(0.1,0xffffff,0,height/2,width-2,height/2); //绘制中心轴线
//绘制横轴
for(var i:int=20;i<height;i+=20){
doDraw(0.1,0xCCCCCC,0,i,width-2,i); //绘制中心轴线
}
//绘制纵轴
for(var j:int=40;j<width;j+=40){
doDraw(0.1,0xCCCCCC,j,0,j,height); //绘制中心轴线
}
var dataCount:Number = lengthTime*44.1;
sound.extract(ba, dataCount); //将所有的语音数据读取到字节数组里面
ba.position = 0;//文件指针的当前位置
var startX:Numbe
(责任编辑:JavaVideo) |