博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flex4/Flash开发在线音乐播放器 , 含演示地址
阅读量:2222 次
发布时间:2019-05-08

本文共 29901 字,大约阅读时间需要 99 分钟。

要求

    • 必备知识

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

    • 开发环境

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

    • 演示地址

 
 

2014-07-09_1751281_thumb

 

传统网络程序的开发是基于页面的、服务器端数据传递的模式,把网络程序的表现层建立于HTML页面之上,而HTML是适合于文本的,传统的基于页面的系统已经渐渐不能满足网络浏览者的更高的、全方位的体验要求了。而富互联网应用(Rich Internet Applications,缩写为RIA)的出现就是为了解决这个问题。在HTML5发布以前,RIA领域的技术解决方案一直相都是各展所长,并无争议。Adobe体系中,Flash做不了的事情,Flex可以做到;.Net系决策者在选用RIA解决方案时,Silverlight是不二之选。随着HTML 5横空出世,Flex“易主”(这里说的是Adobe将Flex捐给Apache),Silverlight被“雪藏”(这里指微软停止对Silverlight的更新),RIA领域的技术解决方案开始变得扑朔迷离。 HTML 5无疑是“明日之星”,苹果公司前CEO乔布斯对它赞赏有加,绝大多数智能手机浏览器均支持HTML 5,基于HTML 5的网站也如雨后春笋般出现。这些似乎预示着HTML 5时代来临,人们试图让决策者相信,Flash/Flex时代已经过去了,HTML 5才是RIA领域的最佳解决方案。那到底真相会是什么呢? HTML 5其实也存在许多劣势,并不完美。HTML 5的浏览器兼容性问题(由于国内传统IE浏览器占了相当大的比重);要实现html5应用,还要写CSS与JavaScript,增加了人员构成和开发成本,其编写难度也要远大于Flex。在较长一段时间内,HTML5是无法”替代”Flex技术的,也许最终HTML 5与Flex将成为是两种截然不同的技术解决方案,所以,它们是互补的,而非替代。如对Flash/Flex/Html5还不了解,我到互联网上找了一篇相关的文章和大家分享一下:

 

一,Flex4&BlazeDS&JAVA整合:

 

二,用户界面设计:

  • 播放器界面的设计:

2014-07-10_1220305_thumb

 

  • 专辑制作界面的设计:

2014-07-10_122322410_thumb

 

三,数据库设计:

2014-07-10_1149235_thumb

  • album(专辑表)创建语句:
CREATE TABLE `album` (   `a_id` int(11) NOT NULL AUTO_INCREMENT,   `a_name` varchar(20) NOT NULL DEFAULT '',   `a_singer` varchar(20) NOT NULL DEFAULT '',   `a_image` varchar(120) NOT NULL DEFAULT '',   PRIMARY KEY (`a_id`) ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;
  • song(歌曲表)创建语句:
CREATE TABLE `song` (   `s_id` int(11) NOT NULL AUTO_INCREMENT,   `a_id` int(11) NOT NULL,   `s_name` varchar(120) NOT NULL DEFAULT '',   `s_source` varchar(150) NOT NULL DEFAULT '',   PRIMARY KEY (`s_id`) ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

 

四,前端代码物理实现(Flex4部分代码实现):

  • 播放器界面物理实现,通过在主程序中定义三个不同的自定义组件,”专辑””列表”“播放器控制面板”来构成整个播放器界面。

1,程序入口文件:myMusicPlayer.mxml

[Event(name="changeitem",type="events.MyEvent")] //通过 Event元数据 定义自定义监听事件

2,自定义“专辑”组件:Album.mxml

2014-07-10_1329245_thumb

[Event(name="changeitem3",type="events.MyEvent3")]

 

3,自定义“列表”组件:List.mxml

2014-07-10_1329455_thumb

[Event(name="changeitem",type="events.MyEvent")]

4,自定义“播放器控制面板”组件:Player.mxml

2014-07-10_1330085_thumb

//此状态为 暂停后点击播放按钮 状态 currentMusicChannel = currentMusicSound.play(currentMusicPosition); } isplaying=true; }else{ //暂停 //此状态为 播放过程中点击 暂停按钮 状态 currentMusicPosition = currentMusicChannel.position;//记录暂停位置 currentMusicChannel.stop();//暂停 isplaying=false; } currentMusicChannel.addEventListener(Event.SOUND_COMPLETE,autoPlayNext);//自动播放下一首 } /** * 自动播放下一首 * @param event * */ protected function autoPlayNext(event:Event):void{
//过滤参数问题 if(parentApplication.list.list.selectedIndex>=musicNum-1 ){ parentApplication.list.list.selectedIndex = 0; }else{ parentApplication.list.list.selectedIndex += 1; } if(currentMusicSound!=null&¤tMusicChannel!=null){ currentMusicChannel.stop();//暂停 } clearPar(); musicItem=parentApplication.list.list.selectedItem; currentMusicUrlString=serverIP+musicItem.s_source; 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);//自动播放下一首 } /** * 播放上一首 */ protected function playPrevious(event:MouseEvent):void { parentApplication.list.list.selectedIndex--; if(parentApplication.list.list.selectedIndex<0 ){ parentApplication.list.list.selectedIndex = musicNum-1; } if(currentMusicSound!=null&¤tMusicChannel!=null){ currentMusicChannel.stop();//暂停 } clearPar(); musicItem=parentApplication.list.list.selectedItem; currentMusicUrlString=serverIP+musicItem.s_source; 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);//自动播放下一首 } /** * 播放下一首 */ protected function playNext(event:MouseEvent):void { parentApplication.list.list.selectedIndex++; if(parentApplication.list.list.selectedIndex>musicNum-1 ){ parentApplication.list.list.selectedIndex = 0; } if(currentMusicSound!=null&¤tMusicChannel!=null){ currentMusicChannel.stop();//暂停 } clearPar(); musicItem=parentApplication.list.list.selectedItem; currentMusicUrlString=serverIP+musicItem.s_source; 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 function playerList_change_Handler(event:MyEvent):void{ if(currentMusicSound!=null&¤tMusicChannel!=null){ currentMusicChannel.stop();//暂停 } clearPar(); musicItem=event.musicItem; currentMusicUrlString=serverIP+musicItem.s_source; 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);//自动播放下一首 } /** * 清除参数 * currentMusicSound = null; * currentMusicChannel = null; * currentMusicPosition = 0; * */ private function clearPar():void{ currentMusicSound = null; currentMusicChannel = null; currentMusicPosition = 0; } /** * 正在播放的歌曲的总时长 */ private var len:int; /** * 文件加载完成 能读取到音乐的总时长 * @param event * */ protected function load_CompleteHandler(event:Event):void{ len = currentMusicSound.length; totalTime.text = Tool4DateTime.millionSecond2MinuteSecond(len); } /** * 同步更新已经播放的时间的计时器 * */ 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; playedTime.text = Tool4DateTime.millionSecond2MinuteSecond(currentMusicPosition); } /** * 播放进度条 可以拖动 * @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; } } /** * 音量调节 * @param event * */ protected function volumeSlider_changeHandler(event:Event):void{ if(currentMusicChannel != null){
//正在播放时调节音量 currentMusicTransform = currentMusicChannel.soundTransform; currentMusicTransform.volume = volumeSlider.value/10; currentMusicChannel.soundTransform = currentMusicTransform; currentVolum = currentMusicTransform.volume; } currentVolum = volumeSlider.value/10; } ]]>

关于flex操作音频文件,可以参考我之前做的一个简单音乐播放器实例:

o_2014-03-13_1310385_thumb

 

  • 专辑制作插件物理实现、

2014-07-10_122322411_thumb

MyUpload3.mxml

关于文件上传部分,是根据MultiFile Upload插件,通过自定义UI组件皮肤完成,可以参考我之前的一个修改实例:

0710172747118075_thumb

五,后端代码物理实现(JAVA部分代码物理实现):

AlbumDao.java

package com.dao;import java.awt.List;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import com.bean.Album;import com.resource.JDBCUtilSingle;public class AlbumDao {            /**     * 插入专辑信息 返回ID号     * @param a_name     * @param a_singer     * @param a_image     * @return     */    public int addAlbum(String a_name,String a_singer,String a_image){        Connection connection=null;        PreparedStatement statement=null;        ResultSet rs=null;        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();        int id=0;                 try {            //专辑信息插入            String sql="insert into album(a_name,a_singer,a_image) values(?,?,?)";            statement=connection.prepareStatement(sql);            statement.setString(1,a_name);            statement.setString(2,a_singer);            statement.setString(3,a_image);            //System.out.println(sql);            statement.executeUpdate();             //获取插入ID            rs = statement.getGeneratedKeys();            rs.next();                     id = rs.getInt(1);          } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);        return id;    }                        /**     * 获取所有专辑信息     * @return  返回专辑列表     */    public ArrayList selectAlbum(){        Connection connection=null;        PreparedStatement statement=null;        ResultSet rs=null;        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();        ArrayList albums=new ArrayList();        try {            //专辑信息插入            String sql="select * from album";            statement=connection.prepareStatement(sql);            rs=statement.executeQuery();             while(rs.next()){                    albums.add(new Album(rs.getInt("a_id"), rs.getString("a_name"), rs.getString("a_singer"), rs.getString("a_image")));             }                    } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally{            JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);        }        return albums;    }}

SongDao.java

package com.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import com.bean.Album;import com.bean.Song;import com.resource.JDBCUtilSingle;public class SongDao {            public int addSongs(int a_id,String s_name,String s_source){        Connection connection=null;        PreparedStatement statement=null;        ResultSet rs=null;        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();        int tag=0;        try {            String sql="insert into song(a_id,s_name,s_source) values(?,?,?)";            statement=connection.prepareStatement(sql);            statement.setInt(1,a_id);            statement.setString(2,s_name);            statement.setString(3,s_source);            tag=statement.executeUpdate();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);        return tag;    }            public ArrayList getSongs(int a_id){        ArrayList songs=new ArrayList();        Connection connection=null;        PreparedStatement statement=null;        ResultSet rs=null;        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();        try {            String sql="select * from song where a_id="+a_id;            statement=connection.prepareStatement(sql);            rs=statement.executeQuery();            while(rs.next()){                songs.add(new Song(rs.getInt("s_id"), rs.getInt("a_id"), rs.getString("s_name"), rs.getString("s_source")));         }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally{                        JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);        }        return songs;            }}

 

好吧,就写到这里了,因为代码是较早之前写的了,由于学校放假,闲来没事,就那之前的代码翻看了一遍,由于时间久远,也没写什么注释,在代码的语法和程序逻辑上,自己也硬是看了半天才回过神来。现在在代码中加了一些注释,添加了一些歌曲内容,已上传到测试空间中,欢迎大家来测试。由于本人水平有限,如文章在表述或代码方面有何不妥之处,欢迎批评指正。

 

你可能还对以下关于Flex的文章内容还感兴趣:

 

如以上文章或链接对你有帮助的话,别忘了在文章结尾处轻轻点击一下 “还不错”按钮或到页面右下角点击 “赞一个” 按钮哦。你也可以点击页面右边“分享”悬浮按钮哦,让更多的人阅读这篇文章。

作者:
出处:
由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正。留下你的脚印,欢迎评论哦。你也可以关注我,一起学习哦!

转载于:https://www.cnblogs.com/Li-Cheng/p/3837554.html

你可能感兴趣的文章
深入理解JVM虚拟机12:JVM性能管理神器VisualVM介绍与实战
查看>>
深入理解JVM虚拟机13:再谈四种引用及GC实践
查看>>
Spring源码剖析1:Spring概述
查看>>
Spring源码剖析2:初探Spring IOC核心流程
查看>>
Spring源码剖析5:JDK和cglib动态代理原理详解
查看>>
Spring源码剖析6:Spring AOP概述
查看>>
Spring源码剖析9:Spring事务源码剖析
查看>>
重新学习Mysql数据库1:无废话MySQL入门
查看>>
探索Redis设计与实现3:Redis内部数据结构详解——sds
查看>>
探索Redis设计与实现4:Redis内部数据结构详解——ziplist
查看>>
探索Redis设计与实现9:数据库redisDb与键过期删除策略
查看>>
探索Redis设计与实现10:Redis的事件驱动模型与命令执行过程
查看>>
分布式系统理论基础1: 一致性、2PC和3PC
查看>>
分布式系统理论基础2 :CAP
查看>>
分布式系统理论基础3: 时间、时钟和事件顺序
查看>>
分布式系统理论基础4:Paxos
查看>>
分布式系统理论基础5:选举、多数派和租约
查看>>
分布式系统理论基础6:Raft、Zab
查看>>
分布式系统理论进阶7:Paxos变种和优化
查看>>
分布式系统理论基础8:zookeeper分布式协调服务
查看>>