博客中 Flex4/Flash mp3音乐播放器实例 含演示地址

时间:2023-03-08 18:31:54

要求

    • 必备知识

      本文要求基本了解 Adobe Flex编程知识和JAVA基础知识。

    • 开发环境

      MyEclipse10/Flash Builder4.6/Flash Player11及以上

    • 演示地址

      演示地址 资料下载

播放器演示已经在博客banner部分给出了,下面还是上一截图吧:

博客中 Flex4/Flash 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"
width="330" height="75" backgroundAlpha="1.0" backgroundColor="#9C4B4B"
preloaderChromeColor="#060606"
creationComplete="initApp()">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert; /**
* musicItem Object 音乐信息对象
*/
[Bindable]
public var musicItem:Object; /**
* 音乐是否正在播放
*/
private var isplaying:Boolean = false; /**
* 播放器音量
*/
private var currentVolum:Number =0.5;
/**
* 正在播放的音乐的URL字符串
* 从主程序中获得 player.currentMusicUrlString=musicSource;
*/
public var currentMusicUrlString:String;
/**
* 正在播放的音乐的URLRequest
*/
private var currentMusicUrlRequest:URLRequest;
/**
* 正在播放的音乐的Sound
*/
private var currentMusicSound:Sound;
/**
* 正在播放的音乐的SoungChannel
* SoundChannel 类控制应用程序中的声音。每个声音均分配给一个声道,而且应用程序可以具有混合在一起的多个声道。SoundChannel 类包含 stop() 方法、用于监控声道幅度(音量)的属性以及用于对声道指定 SoundTransform 对象的属性。
*/
private var currentMusicChannel:SoundChannel;
/**
* 正在播放的音乐的 SoundTransform
* SoundTransform 类包含音量和平移的属性。
*/
private var currentMusicTransform:SoundTransform; /**
* 正在播放的音乐的总时间
*/
private var currentMusicTotleTime:Number =0;
/**
* 正在播放的音乐的播放进度参数
*/
private var currentMusicPosition:int =0; private var songs:ArrayCollection=new ArrayCollection([
{name:"初恋",singer:"张力尹",pic:"data/images/zly.jpg",uri:"data/chulian.mp3"},
{name:"纯真的爱",singer:"张力尹",pic:"data/images/zly.jpg",uri:"data/chunzhendeai.mp3"},
{name:"相信爱",singer:"张力尹",pic:"data/images/zly.jpg",uri:"data/xiangxinai.mp3"},
{name:"星愿(I WILL)",singer:"张力尹",pic:"data/images/zly.jpg",uri:"data/I will.mp3"},
{name:"TIMELESS",singer:"张力尹",pic:"data/images/zly.jpg",uri:"data/TIMELESS.mp3"},
{name:"BLUE",singer:"BigBang",pic:"data/images/ALIVE-BIGBANG.jpg",uri:"data/BigBang - BLUE.mp3"},
{name:"BLUE(指弹)",singer:"Gabriella Quevedo",pic:"data/images/ALIVE-BIGBANG.jpg",uri:"data/Gabriella Quevedo - blue.mp3"},
{name:"You're Beautiful",singer:"James Blunt",pic:"data/images/s1461123.jpg",uri:"data/You're Beautiful.mp3"},
{name:"Only Friends",singer:"Rita Calypso",pic:"data/images/s3786226.jpg",uri:"data/Only Friends.mp3"},
{name:"총맞은것처럼(像中枪一样)",singer:"白智英",pic:"data/images/s3387692.jpg",uri:"data/Muz Cast - xiangzhongqiangyiyang.mp3"},
{name:"是否",singer:"G.E.M",pic:"data/images/150_albumpic_444525_0[1].jpg",uri:"data/G.E.M. - shifou.mp3"},
{name:"Young Girls",singer:"Bruno Mars",pic:"data/images/Bruno Mars.jpg",uri:"data/Bruno Mars - Young Girls.mp3"},
{name:"Nothing On You",singer:"B.O.B&Bruno Mars",pic:"data/images/Bruno Mars.jpg",uri:"data/B.o.B. - Nothing On You.mp3"},
{name:"Young Girls(指弹)",singer:"Gabriella Quevedo",pic:"data/images/Bruno Mars.jpg",uri:"data/Gabriella Quevedo-Young Girls.mp3"},
{name:"Im Yours(指弹)",singer:"Gabriella Quevedo",pic:"data/images/Bruno Mars.jpg",uri:"data/Im Yours -Gabriella Quevedo.mp3"},
{name:"金泰妍 - 가까이(靠近)",singer:"金泰妍 ",pic:"data/images/jintaiyan.jpg",uri:"data/closer.mp3"},
{name:"Grace",singer:"李秀英 ",pic:"data/images/s2868209.jpg",uri:"data/Grace.mp3"},
{name:"Twenty Nine",singer:"李秀英 ",pic:"data/images/s2722298.jpg",uri:"data/Twenty Nine.mp3"} ]); /**
* 随机获取一首歌曲
*/
private function getMusicItem():Object{
var index: Number = Math.round(Math.random()*(songs.length-1)); //产生一个随机数
musicItem=songs[index];
return musicItem;
} /**
* 程序初始化
*/
private function initApp():void{
//初始话一条数据
getMusicItem();
//UI控件事件监听
playAndPause.addEventListener(MouseEvent.CLICK,musicPlay); //播放按钮
next.addEventListener(MouseEvent.CLICK,autoPlayNext);
playingProcess.addEventListener(Event.CHANGE,playingProcess_changeHandler); //进度条滚动事件
} private function musicPlay(event:MouseEvent):void{
if(!isplaying){ //播放 false
//此状态为 启动播放器 然后点击播放按钮 状态(空状态)
if(currentMusicSound==null&&currentMusicChannel ==null){
currentMusicUrlString=musicItem.uri;
currentMusicUrlRequest =new URLRequest(currentMusicUrlString);
currentMusicSound = new Sound();
currentMusicSound.load(currentMusicUrlRequest);
currentMusicSound.addEventListener(Event.COMPLETE,load_CompleteHandler);
currentMusicChannel = currentMusicSound.play();//开始播放
timer_GetCurrentPositionHandler();//同步更新已经播放的时间的计时器
}else{//此状态为 暂停后点击播放按钮 状态
currentMusicChannel = currentMusicSound.play(currentMusicPosition);
}
isplaying=true;
}else{ //暂停
//此状态为 播放过程中点击 暂停按钮 状态
currentMusicPosition = currentMusicChannel.position;//记录暂停位置
currentMusicChannel.stop();//暂停
isplaying=false;
} //currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自动播放下一首 } /**
* 自动播放下一首 和 next按钮采用同样的方式吧
* @param event
*
*/
protected function autoPlayNext(event:Event):void{//过滤参数问题
getMusicItem();
if(currentMusicSound!=null&&currentMusicChannel!=null){
currentMusicChannel.stop();//暂停
}
clearPar();
currentMusicUrlString=musicItem.uri;
currentMusicUrlRequest =new URLRequest(currentMusicUrlString);
currentMusicSound = new Sound();
currentMusicSound.load(currentMusicUrlRequest);
currentMusicSound.addEventListener(Event.COMPLETE,load_CompleteHandler);
playAndPause.selected=true;
isplaying =true;
currentMusicChannel = currentMusicSound.play();//开始播放
timer_GetCurrentPositionHandler();//同步更新已经播放的时间的计时器
currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自动播放下一首 } /**
* 正在播放的歌曲的总时长
*/
private var len:int; /**
* 文件加载完成 能读取到音乐的总时长
* @param event
*
*/
protected function load_CompleteHandler(event:Event):void{ len = currentMusicSound.length; } /**
* 同步更新已经播放的时间的计时器
*
*/
protected function timer_GetCurrentPositionHandler():void{
var clock:Timer = new Timer(100,int(len/1000/60*10));//每0.1秒更新一次
clock.start();
clock.addEventListener(TimerEvent.TIMER,showTime);
} /**
* 显示已经播放的总时间
* @param event
*
*/
protected function showTime(event:Event):void{
playingProcess.maximum = int(len/1000)*10;//最大值
playingProcess.value = int(currentMusicPosition/1000*10); //当前值
currentMusicPosition = currentMusicChannel.position; } /**
* 播放进度条 可以拖动
* @param event
*
*/
protected function playingProcess_changeHandler(event:Event):void{
if(currentMusicChannel!=null){
currentMusicPosition = playingProcess.value*1000/10;//当前音乐播放进度
currentMusicChannel.stop();
currentMusicChannel = currentMusicSound.play(currentMusicPosition);
isplaying=true; playAndPause.selected=true;
currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自动播放下一首 }else{ playingProcess.value=0;
}
} /**
* 清除参数
* currentMusicSound = null;
* currentMusicChannel = null;
* currentMusicPosition = 0;
*
*/
private function clearPar():void{
currentMusicSound = null;
currentMusicChannel = null;
currentMusicPosition = 0;
} ]]>
</fx:Script>
<s:Group verticalCenter="0" >
<!--背景-->
<s:Rect width="330" height="75">
<s:fill>
<s:SolidColor color="#FFFFFF"/>
</s:fill>
</s:Rect>
<s:BitmapImage source="{musicItem.pic}" smooth="true" width="75" height="75" />
<s:VGroup left="85" top="10" right="10" bottom="0" gap="0">
<s:HGroup height="45" width="235" verticalAlign="middle" >
<s:VGroup height="45" gap="0" width="150">
<!--歌名-->
<s:Label maxDisplayedLines="1" width="100%" height="25" fontFamily="微软雅黑" fontSize="14"
fontWeight="bold" text="{musicItem.name}" verticalAlign="middle"/>
<!--歌手-->
<s:Label maxDisplayedLines="1" width="100%" height="20" fontFamily="微软雅黑" text="{musicItem.singer}"
verticalAlign="middle"/>
</s:VGroup>
<s:HGroup width="100%" horizontalAlign="right" gap="30" paddingRight="5">
<!--next-->
<s:Button id="next" skinClass="components._ButtonSkin1"/>
<!--play-->
<s:ToggleButton id="playAndPause" skinClass="components.pToggleButtonSkin1"/>
</s:HGroup>
</s:HGroup>
<s:Group width="100%" height="20">
<!--进度条-->
<s:ScrubBar id="playingProcess" horizontalCenter="0" verticalCenter="-2" skinClass="skinks.playercontrol.scrubbar.ScrubBar"/>
</s:Group>
</s:VGroup>
</s:Group> </s:Application>
作者:Li-Cheng
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。