[AS3]基于rtmp协议的Mp3流媒体播放器fms音乐播放器
[AS3]基于rtmp协议的Mp3流媒体播放器fms音乐播放器,fms音乐播放器,rtmp播放器,mp3播放器
[AS3]基于rtmp协议的Mp3流媒体播放器fms音乐播放器,fms音乐播放器,rtmp播放器,mp3播放器
- /*******************************
- Import Classes
- *******************************/
- //Allow AS3 to talk to FMS2 AS1
- NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;
- /*******************************
- Var
- *******************************/
- var rtmp:String = "rtmp://yourserver.com";
- var songList:Array = new Array("You and I", "05 Phantom pt. I");
- var nc:NetConnection = new NetConnection();
- // nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
- var ns:NetStream;
- var id3_ns:NetStream;
- var nsClient:Object = new Object();
- var id3Client:Object = new Object();
- var isConnected:Boolean = false;
- var currentSong:Number = 0;
- var soundXForm:SoundTransform = new SoundTransform();
- soundXForm.volume = 0.5;
- /*******************************
- Event Listeners
- *******************************/
- prev_btn.addEventListener(MouseEvent.CLICK, prevHandler);
- next_btn.addEventListener(MouseEvent.CLICK, nextHandler);
- play_mc.addEventListener(MouseEvent.CLICK, toggleButton);
- play_mc.addEventListener(MouseEvent.CLICK, pauseHandler);
- seekBar_mc.buttonMode = true;
- seekBar_mc.addEventListener(MouseEvent.MOUSE_DOWN, seekStartDrag);
- seekScrub_mc.buttonMode = true;
- seekScrub_mc.addEventListener(MouseEvent.MOUSE_DOWN, seekStartDrag);
- volScrub_mc.buttonMode = true;
- volScrub_mc.scaleX = (volBar_mc.scaleX / 2);
- volScrub_mc.addEventListener(MouseEvent.MOUSE_DOWN, volStartDrag);
- volBar_mc.buttonMode = true;
- volBar_mc.addEventListener(MouseEvent.MOUSE_DOWN, volStartDrag);
- /*******************************
- Event Handlers
- *******************************/
- function prevHandler(e:MouseEvent):void {
- prevSong();
- }
- function pauseHandler(e:MouseEvent):void {
- pauseSong();
- }
- function playHandler(e:MouseEvent):void {
- //Play the song based on the position of the seekKnob
- playSong(ns.time);
- }
- function nextHandler(e:MouseEvent):void {
- nextSong();
- }
- function toggleButton(e:MouseEvent) {
- // trace(e.target.currentFrame);
- //on - 1 :: onOver - 5 :: mute - 10 :: muteOver - 15
- if (e.target.currentFrame == 1)
- {
- e.target.gotoAndStop(10);
- } else if (e.target.currentFrame == 5)
- {
- e.target.gotoAndStop(15);
- } else if (e.target.currentFrame == 10)
- {
- e.target.gotoAndStop(1);
- } else if (e.target.currentFrame == 15)
- {
- e.target.gotoAndStop(5);
- }
- }
- /*******************************
- Connect to FMS and play song
- *******************************/
- //cuplayer.comThis Handles a Function call made by the Flash Media Server default main.asc
- NetConnection.prototype.onBWDone = function(info) {}
- NetConnection.prototype.onBWCheck = function() {}
- function createConnection(appURL:String):void {
- if( !nc.connected){
- nc.connect(appURL);
- nc.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
- nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
- }else{
- nc.close();
- isConnected = false;
- }
- }
- function securityErrorHandler( e ):void{
- trace("SecurityError: " + e);
- }
- //Detect when a connection has been made as well as the status of a playing song
- function netStatusHandler( e:NetStatusEvent ):void {
- switch( e.info.code ) {
- case "NetConnection.Connect.Success":
- connectionSuccess( new Event( "success" ) );
- break;
- case "NetConnection.Connect.Failed":
- connectionFailed( new Event( "failed" ) );
- break;
- case "NetStream.Play.Stop":
- // trace("netStatusHandler:code: " + e.info.code);
- break;
- default:
- //trace( "netStatusHandler:code: " + e.info.code );
- break;
- }
- }
- function connectionSuccess( e:Event ):void {
- //A. Assign that a NetConnection has been made
- isConnected = true;
- //B. Assign a new NetStream connection
- if( ns == null ){
- ns = new NetStream(nc);
- ns.addEventListener( AsyncErrorEvent.ASYNC_ERROR, catchAll );
- ns.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
- ns.addEventListener( IOErrorEvent.IO_ERROR, catchAll );
- }
- //C. Assign a new ID3 Netstream Connection
- if( id3_ns == null ) {
- id3_ns = new NetStream(nc);
- id3_ns.addEventListener( AsyncErrorEvent.ASYNC_ERROR, catchAll );
- id3_ns.addEventListener( IOErrorEvent.IO_ERROR, catchAll );
- id3_ns.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
- }
- playSong(0);
- }
- function connectionFailed( e:Event ):void {
- isConnected = false;
- }
- function catchAll( e:Event ){
- trace("\n\n\n\nCatch All: " + e + "\n\n\n\n");
- }
- /*******************************
- Single callback functions that process the information sent by FMS2
- *******************************/
- nsClient.onPlayStatus = function( obj:Object ):void{
- switch ( obj.code ){
- case "NetStream.Play.Switch":
- break;
- case "NetStream.Play.Complete":
- soundCompleteHandler();
- break;
- default:
- for( var a:String in obj ){ trace(a + " " + obj[ a ]); }
- break;
- }
- }
- id3Client.onId3 = function( obj:Object ):void{
- if(obj["artist"] != undefined){
- text_txt.text = "Artist: " + obj["artist"].toString();
- }
- if(obj["songtitle"] != undefined){
- text_txt.text += "\nTitle: " + obj["songtitle"].toString()
- }
- for( var b:String in obj ) {
- //trace(b + ": " + obj[ b ]);
- }
- }
- function setNetClientLength(length:Number):void{
- nsClient.length = length;
- }
- /*******************************
- Song
- *******************************/
- function prevSong():void {
- //cuplayer.comA. Close NetStream connection to allow for a new one
- ns.close();
- //B. If there is no previous song to play, restart the currentSong
- if (currentSong > 0) {
- currentSong--;
- } else {
- currentSong = songList.length - 1;
- }
- //C. Create a new Net Stream. The new Event is simply Fluff
- connectionSuccess( new Event("Empty String"));
- }
- function nextSong():void {
- //A. Close the NetStream connection to allow for a new one
- ns.close();
- //B. If there is another song in the playlist, play on!
- if (currentSong < songList.length - 1) {
- currentSong++;
- }else {
- //C. Otherwise return to the first song on the playlist
- currentSong = 0;
- }
- //cuplayer.comD. Create a new NetStream connection
- connectionSuccess(new Event("Empty String"));
- }
- function playSong(position:Number):void {
- text_txt.text = "";
- //A. Ensure that the connection to FMS is open
- if (!isConnected) { createConnection(rtmp); }
- //B. Find the ID3 Information of the Mp3
- id3_ns.play("id3:" + songList[currentSong]);
- //C. Reassign the id3Client:Object to re-catch any ID3 info
- id3_ns.client = id3Client;
- //C. Play the new NetStream connection
- //play(songName, postion in Seconds:Number, duration in seconds:Number, flush:Boolean -> false means play the next song)
- ns.play("mp3:" + songList[currentSong], position);
- //D. Reassign the nsClient:Object to ensure the catch of the onPlayStatus
- ns.client = nsClient;
- //cuplayer.comE. Adjust the Volume
- ns.soundTransform = soundXForm;
- //E. Retreive the Length of the Song from FMS and set it to nsClient.length
- nc.call("getStreamLength", new Responder(setNetClientLength), "mp3:" + songList[currentSong]);
- //F. Begin tracking the position of the song for the status bar
- seekBar_mc.addEventListener(Event.ENTER_FRAME, songPosition);
- }
- function pauseSong():void {
- ns.togglePause();
- }
- function soundCompleteHandler():void {
- trace("soundCompleteHandler");
- //Garbage Collection
- seekBar_mc.removeEventListener(Event.ENTER_FRAME, songPosition);
- //This resets the Progress Bar back to 0
- songPosition(new Event("Empty String"));
- //Flip the Play/Pause button back to play
- play_mc.gotoAndStop("play");
- //Repeat the song
- nextSong();
- }
- /*******************************
- Volume
- *******************************/
- function volStartDrag(e:MouseEvent):void {
- volumeUpdate(e);
- volBar_mc.addEventListener(MouseEvent.MOUSE_MOVE, volumeUpdate);
- volScrub_mc.addEventListener(MouseEvent.MOUSE_MOVE, volumeUpdate);
- stage.addEventListener(MouseEvent.MOUSE_UP, volumeStopDrag);
- }
- function volumeStopDrag(e:MouseEvent):void {
- volBar_mc.removeEventListener(MouseEvent.MOUSE_MOVE, volumeUpdate);
- volScrub_mc.removeEventListener(MouseEvent.MOUSE_MOVE, volumeUpdate);
- stage.removeEventListener(MouseEvent.MOUSE_UP, volumeStopDrag);
- }
- function volumeUpdate(e:MouseEvent):void{
- //Calculate the Distance Between the Mouse (e.stageX) and the volBar_mc.x
- var dist:Number = ((e.stageX - volBar_mc.x) / volBar_mc.width);
- if (dist >= 0 && dist <= 1){
- //Adjust the Volume Bar GUI
- volumeBarUpdate(dist);
- //Adjust the Sound Volume
- soundXForm.volume = dist;
- ns.soundTransform = soundXForm;
- }
- }
- function volumeBarUpdate(dist:Number):void{
- volScrub_mc.scaleX = (dist);
- }
- /*******************************
- Seek
- *******************************/
- function seekStartDrag(e:MouseEvent):void {
- //cuplayer.comA. Jump to the spot in the song where the user clicked
- seekUpdate(e);
- //A. Remain seeking while the Mouse is Still Down and Moving
- seekBar_mc.addEventListener(MouseEvent.MOUSE_MOVE, seekUpdate);
- seekScrub_mc.addEventListener(MouseEvent.MOUSE_MOVE, seekUpdate);
- //cuplayer.comC. Self Destruct
- stage.addEventListener(MouseEvent.MOUSE_UP, seekStopDrag);
- }
- function seekStopDrag(e:Event):void {
- seekScrub_mc.removeEventListener(MouseEvent.MOUSE_MOVE, seekUpdate);
- seekBar_mc.removeEventListener(MouseEvent.MOUSE_MOVE, seekUpdate);
- stage.removeEventListener(MouseEvent.MOUSE_UP, seekStopDrag);
- }
- function seekUpdate(e:MouseEvent):void {
- //cuplayer.comA. If a user clicks a new part of the song, you must first seek() then update the Progress Bar
- var dist:Number = ((e.stageX - seekBar_mc.x) / seekBar_mc.width);
- //cuplayer.comB. Seek the spot
- ns.seek(Math.floor(nsClient.length * dist));
- //C. Upate the Progress Bar
- seekBarUpdate(dist)
- }
- function songPosition(e:Event):void{
- var dist:Number = ns.time / nsClient.length;
- if (!isNaN(dist)) {
- seekBarUpdate(dist);
- } else {
- seekBarUpdate(0);
- }
- }
- function seekBarUpdate(dist:Number){
- //A. Adjust the GUI
- seekScrub_mc.scaleX = dist;
- }
- /*******************************
- Launch the Application on Frame(0) cuplayer.com
- *******************************/
- addFrameScript(0, createConnection(rtmp));
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...