视频加载播放暂停切换

Posted zqiang0803

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视频加载播放暂停切换相关的知识,希望对你有一定的参考价值。

package com{
    import flash.display.MovieClip;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.AsyncErrorEvent;
    import flash.events.MouseEvent;
    import flash.media.Video;
    import flash.display.SimpleButton;
    import flash.events.Event;
    import flash.display.Loader;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class MyLoader extends MovieClip {

        public static var nc:NetConnection;
        public static var ns:NetStream;
        public static var vid:Video;
        public static var empty:MovieClip;
        public static var vidTotalTime:Number;
        public static var vidNowTime:Number;
        public static var obj:Object=new Object  ;


        public static var loader:Loader;
        public static var urlR:URLRequest;
        public static var backBtn:SimpleButton;
        public static var removeBoo:Boolean=false;
        public static var timer:Timer;
        public static var btnMc:MovieClip;
        public static var picMc:MovieClip;
        
        
        private var vidY:Number
        private var initX:Number
        private var vidW:Number
        private var vidH:Number
        private var interval:Number

        //加载视频============================================
        public function MyLoader(_empty:MovieClip,_btnMc:MovieClip,_picMc:MovieClip) {
            empty=_empty;
            btnMc=_btnMc;
            picMc=_picMc
            
            vidY=picMc.y
            initX=picMc.x
            vidW=picMc.width
            vidH=picMc.height
            interval=212.45
            
            btnMc.addEventListener(MouseEvent.MOUSE_MOVE,btnFun);
            nc=new NetConnection  ;
            nc.connect(null);
            ns=new NetStream(nc);
            ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
            ns.close();
            vid=new Video(vidW,vidH);
            function asyncErrorHandler(event:AsyncErrorEvent):void {
                // 忽略错误
            }

            obj.onMetaData=onMetaData;
            ns.client=obj;
            function onMetaData(data:Object):void {
                vidTotalTime=data.duration;
            }

            //ns.play("vid/"+2+".mp4");
            //empty.addChild(vid);
            vid.attachNetStream(ns);
            ns.pause();
            vid.smoothing=true;

            timer=new Timer(25);
            timer.addEventListener(TimerEvent.TIMER,timerFun);


        }
        //播放暂停
        public static function togglePauseFun() {
            ns.togglePause();
        }
        public static function removeMP4() {
            if (ns.time!=0) {
                ns.close();
                empty.removeChild(vid);
            }
        }
        public static function addMP4(str:String) {
            while (empty.numChildren<0) {
                empty.removeChildAt(0);
            }
            ns.play(str);
            empty.addChild(vid);
            ns.pause();
        }

        public static function timerFun(e:TimerEvent) {
            vidNowTime=ns.time;
            if ((vidTotalTime-vidNowTime)<0.1) {
                ns.close();
                empty.removeChild(vid);
                timer.stop();
                for (var i:uint=1; i<=5; i++) {
                    btnMc["btn_"+i].visible=true;
            }
            }
        }


        private function btnFun(e:MouseEvent) {
            if (timer.running==false) {
                timer.start();
                empty.addChild(vid);
            }
            var str:Number=Number(e.target.name.slice(-1));
            ns.play(("vid/"+str)+".mp4");
            vid.y=vidY
            vid.x=initX+(vid.width+interval)*(str-1)
            for (var i:uint=1; i<=5; i++) {
                if (str==i) {
                    btnMc["btn_"+i].visible=false;
                } else {
                    btnMc["btn_"+i].visible=true;
                }
            }
        }



    }
}

 

以上是关于视频加载播放暂停切换的主要内容,如果未能解决你的问题,请参考以下文章

实现视频的暂停播放,上下切换等功能

视频没有在片段 ViewPager 中暂停

单击视频时,如何使用 Javascript 切换多个视频的播放/暂停?

视频播放/暂停图标的切换

MPMoviePlayerController 播放/暂停切换问题

FFmpegffplay 播放视频命令 ( 播放 | 暂停 | 停止 | 音量控制 | 进度控制 | 音频流 / 视频流 / 字幕流 / 节目切换 )