参考:http://www.javaeye.com/topic/686293
还未实现连续播放。。。
效果:
代码部分:
- <?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"
- creationComplete="init();"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" width="100%" height="100%">
- <s:layout>
- <s:HorizontalLayout verticalAlign="middle" horizontalAlign="center"/>
- </s:layout>
- <fx:Declarations>
- <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- </fx:Declarations>
- <fx:Script>
- <![CDATA[
- import flash.utils.Timer;
- import mx.collections.ArrayCollection;
- import mx.controls.Alert;
- //
- private var acSound:ArrayCollection = new ArrayCollection(["butterfly.mp3", "life.mp3","rockstar.mp3"]);
- //歌曲索引
- private var index:int = 0;
- //使用 Sound 类创建 Sound 对象、将外部 MP3 文件加载到该对象并播放该文件、
- //关闭声音流,以及访问有关声音的数据,如有关流中字节数和 ID3 元数据的信息。
- private var sound:Sound;
- //SoundChannel 类控制应用程序中的声音。
- private var channel:SoundChannel
- //可以创建 Timer 对象,10ms运行一次,0 无限次执行。
- private var timer:Timer = new Timer(10, 0);
- //是否为静音
- private var isQuite:Boolean = false;
- //音量值
- private var volumnValue:Number;
- //暂停位置
- private static var pausePos:int;
- //播放进度
- private var tempValue:Number;
- //初始化播放器
- private function init():void {
- loadSound();
- }
- //下载mp3
- private function loadSound():void {
- if(channel != null) {
- channel.stop();
- }
- if(index == 0) {
- previouseBtn.enabled = false;
- }
- else if(index >= acSound.length-1) {
- nextBtn.enabled = false;
- }
- else if(index == acSound.length) {
- index = 0;
- }
- else {
- previouseBtn.enabled = true;
- nextBtn.enabled = true;
- }
- //停止计时器。
- timer.stop();
- sound = new Sound();
- //创建 URLRequest 对象,URLRequest 类可捕获单个 HTTP 请求中的所有信息
- var url:URLRequest = new URLRequest(acSound[index]);
- //load(stream:URLRequest, context:SoundLoaderContext = null):void
- //启动从指定 URL 加载外部 MP3 文件的过程。
- // stream:URLRequest — 指向外部 MP3 文件的 URL。
- //context:SoundLoaderContext (default = null) — 一个可选的 SoundLoader 上下文对象,
- //可以定义缓冲时间(MP3 数据保留到 Sound 对象的缓冲区中所需的最小毫秒数)
- //并且可以指定加载声音前应用程序是否应该检查跨域策略文件。
- sound.load(url);
- timer.addEventListener(TimerEvent.TIMER, timerHandle);
- //开始计时
- timer.start();
- pausePos = 0;
- channel = sound.play();
- playOrPauseBtn.label = "暂停";
- music.text = "--" + acSound[index] + "--";
- }
- //设置timer监听器,更新进度条值
- private function timerHandle(event:TimerEvent):void {
- tempValue = (channel.position/sound.length)*100;
- play_bar.setProgress(tempValue, 100);
- }
- //播放、暂停事件处理
- private function playPauseHandle():void {
- var str:String = playOrPauseBtn.label;
- if(str == "暂停") {
- channel.stop();
- pausePos = channel.position;
- playOrPauseBtn.label = "播放";
- }
- else {
- if(pausePos != 0) {
- channel = sound.play(pausePos);
- playOrPauseBtn.label = "暂停";
- }
- else {
- loadSound();
- }
- }
- }
- //停止事件处理
- private function stopHandle():void {
- if(channel != null) {
- timer.stop();
- play_bar.setProgress(100, 100);
- channel.stop();
- pausePos = 0;
- playOrPauseBtn.label = "播放";
- }
- }
- //播放上一首
- private function playNext():void {
- index--;
- loadSound();
- }
- //播放下一首
- private function playPrevious():void {
- index++;
- loadSound();
- }
- //设置音量
- private function volumnHandle():void {
- //public function SoundTransform(vol:Number = 1, panning:Number = 0)
- // vol:Number (default = 1) — 音量范围从 0(静音)至 1(最大音量)。
- //panning:Number (default = 0) — 声音从左到右的平移,范围从 -1(左侧最大平移)
- //至 1(右侧最大平移)。值 0 表示没有平移(居中)。
- var soundTf:SoundTransform = new SoundTransform(volumn_hs.value, 0);
- channel.soundTransform = soundTf;
- }
- //设置静音
- private function quiteBtnHandle():void {
- var soundTf:SoundTransform;
- if(!isQuite) {
- soundTf = new SoundTransform(0, 0);
- channel.soundTransform = soundTf;
- volumnValue = volumn_hs.value;
- volumn_hs.value = 0;
- isQuite = true;
- }
- else {
- soundTf = new SoundTransform(volumnValue, 0);
- channel.soundTransform = soundTf;
- isQuite = false;
- volumn_hs.value = volumnValue;
- }
- }
- //设置播放进度条
- private function play_barHandle(event:MouseEvent):void {
- var str:String = playOrPauseBtn.label;
- if(str == "暂停") {
- channel.stop();
- channel = sound.play((event.localX/play_bar.width) * sound.length);
- }
- }
- ]]>
- </fx:Script>
- <mx:VBox width="50%" height="50%" paddingLeft="5" paddingRight="5" paddingTop="5"
- paddingBottom="5" borderStyle="solid">
- <mx:Label id="music" width="122" fontSize="15"/>
- <!-- 用来摆的!! -->
- <mx:VideoDisplay width="100%" height="80%" />
- <!-- 播放的进度条 -->
- <mx:ProgressBar id="play_bar" width="100%" minimum="0" maximum="100"
- labelPlacement="center" indeterminate="false" visible="true"
- click="play_barHandle(event);" label="" mode="manual" direction="right" height="16"/>
- <mx:HBox width="100%" height="20%" verticalAlign="middle" fontSize="15" borderStyle="solid">
- <mx:LinkButton id="playOrPauseBtn" label="播放" click="playPauseHandle();"/>
- <mx:LinkButton id="stopBtn" label="停止" click="stopHandle();"/>
- <mx:LinkButton id="previouseBtn" label="上一首" click="playNext();"/>
- <mx:LinkButton id="nextBtn" label="下一首" click="playPrevious();"/>
- <mx:Spacer width="100%" />
- <mx:LinkButton id="quiteBtn" label="静音" click="quiteBtnHandle();"/>
- <!-- 控制音量 -->
- <mx:HSlider id="volumn_hs" change="volumnHandle();" value="4"/>
- </mx:HBox>
- </mx:VBox>
- </s:Application>