• Flex在线录制视频并回放(源码)


    效果图:
    本文来源于 冰山上的播客 http://xinsync.xju.edu.cn , 原文地址:http://xinsync.xju.edu.cn/index.php/archives/1703
    第一步:安装Flash Midea Server 2,在安装路径的Flash Midea Server 2\applications下建立test文夹件(文件夹名称自己定义),重新启动FMS。
    第二步:编写代码,以下是完整代码:
     程序代码
    <?xml version=”1.0″ encoding=”utf-8″?>
    <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”playinit()” width=”366″ height=”350″>
    <mx:Script>
    <![CDATA[
    import mx.events.SliderEvent;
    import mx.events.VideoEvent;
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
    import mx.core.UIComponent;
    import flash.events.StatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.media.Camera;
    import flash.media.Microphone;
    import flash.net.NetConnection;
    //由于fms使用的是amf0而flex3中的as3默认使用的是amf3.所以要让flex使用AFM0
    NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
    //视频服务端地址
    private var _videoServerURL:String = “rtmp://192.168.0.107/test”;
    private var _camera:Camera; //定义一个摄像头
    private var _mic:Microphone; //定义一个麦克风
    private var _localVideo:Video; //定义一个本地视频
    private var _netConnection:NetConnection;
    private var _outStream:NetStream; //定义一个输出流
    private var _inStream:NetStream; //定义一个输入流
    private var isplaying:Boolean=false; //定义是否正在播放标记
    private var isrecing:Boolean = false; //定义是否正在录制标记
    private var ispauseing:Boolean = false; //定义是否正在暂停标记
    private var _duration:Number; //定义视频持续时间
    private var playPosition:Number; //定义播放进度位置
    private var soundPosition:Number; //定义声音大小控制条的位置
    private function playinit():void{
    t_hs_control.enabled=false;
    t_btn_play.enabled = false;
    t_btn_stop.enabled = false;
    t_btn_rec.enabled = false;
    t_btn_save.enabled = false;
    t_lbl_rec.visible = false;
    initCameraAndMic(); //初始化摄像头
    }
    //初始化摄像头
    //判断是否存在摄像头和访问权限
    private function initCameraAndMic():void
    {
    _camera = Camera.getCamera();
    if(_camera != null)
    {
    _camera.addEventListener(StatusEvent.STATUS,__onStatusHandler);
    _camera.setMode(320,420,30);
    //t_flv_video.attachCamera(_camera);
    _localVideo = new Video();
    _localVideo.width = 320;
    _localVideo.height = 240;
    _localVideo.attachCamera(_camera);
    t_flv_video.addChild(_localVideo);
    }
    _mic = Microphone.getMicrophone();
    if(_mic != null)
    {
    //未添加侦听麦克连接状态
    //设置本自本地的麦克风的音频传送到本地系统扬声器
    /*
    _mic.setUseEchoSuppression(true);
    _mic.setLoopBack(true);
    */
    _mic.setSilenceLevel(0,-1); //设置麦克风保持活动状态并持续接收集音频数据
    _mic.gain = 80; //设置麦克风声音大小
    }
    }
    //开始录制视频
    //检测网络连接状态
    private function beginOrShowRecVideo():void
    {
    _netConnection = new NetConnection();
    _netConnection.addEventListener(NetStatusEvent.NET_STATUS,__onNetStatusHandler);
    _netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,__onSecurityErrorHandler);
    _netConnection.connect(_videoServerURL);
    }
    //录制视频,向服务器传送视频及音频流
    private function beginRecConnectStream():void
    {
    if(_localVideo != null)
    {
    _localVideo.clear();
    t_flv_video.removeChild(_localVideo);
    _localVideo = new Video();
    _localVideo.width = 320;
    _localVideo.height = 240;
    _localVideo.attachCamera(_camera);
    t_flv_video.addChild(_localVideo);
    }
    _outStream = new NetStream(_netConnection);
    _outStream.attachCamera(_camera);
    _outStream.attachAudio(_mic);
    _outStream.publish(”testVideo”,”record”);
    }
    //播放视频
    private function showRecConnectStream():void
    {
    _inStream = new NetStream(_netConnection);
    _inStream.addEventListener(NetStatusEvent.NET_STATUS,__onNetStatusHandler);
    _inStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,__onStreamErrorHandler);
    //定义onMetaData,获取视频相关数据
    var customClient:Object = new Object();
    customClient.onMetaData = function(metadata:Object):void
    {
    _duration = metadata.duration; //获取视频持续时间
    t_hs_control.maximum = _duration; //设置播放进度条最大值
    }
    _inStream.client = customClient;
    //删除原_localVideo,便于在录制和播放视频之间切换
    _localVideo.clear();
    t_flv_video.removeChild(_localVideo);
    _localVideo = new Video();
    _localVideo.width = 320;
    _localVideo.height = 240;
    _localVideo.attachNetStream(_inStream);
    _inStream.play(”testVideo”);
    t_flv_video.addChild(_localVideo);
    }
    //播放按钮点击后事件:播放视频,同时分析是否播放来调整BUTTON上的标签,显示为播放或者暂停;
    //并监听播放器
    private function flvplay(event:Event):void{
    t_hs_control.enabled=true;
    t_btn_stop.enabled = true;
    t_btn_rec.enabled = false;
    if(!isplaying)
    {
    isplaying = true;
    beginOrShowRecVideo();
    }
    else
    {
    _inStream.togglePause(); //自动在停止和播放之间切换
    }
    if(isplaying)
    {
    if(ispauseing){
    t_btn_play.label=”播放”
    }else {
    t_btn_play.label=”暂停”
    }
    ispauseing = !ispauseing;
    }
    addEventListener(Event.ENTER_FRAME,__onEnterFrame);
    }
    //停止按钮和视频播放完毕
    //重设一些值变量、按钮enabled值等
    private function resetSomeParam():void
    {
    _inStream.close();
    t_btn_play.label = “播放”;
    t_lbl_playtime.text = “0:00 / “+ formatTimes(_duration);
    t_hs_control.value = 0;
    isplaying = false;
    ispauseing = false;
    t_hs_control.enabled=false;
    t_btn_rec.enabled = true;
    t_btn_stop.enabled = false;
    }
    //停止播放按钮点击事件:停止视频,同时调整相关BUTTON上的标签
    private function flvStop(event:Event):void
    {
    resetSomeParam();
    removeEventListener(Event.ENTER_FRAME,__onEnterFrame);
    }
    //拉动进度条
    private function thumbPress(event:SliderEvent):void{
    _inStream.togglePause();
    removeEventListener(Event.ENTER_FRAME,__onEnterFrame);
    }
    //进度条改变后,得到的值赋予PLAYPOSITION;
    private function thumbChanges(event:SliderEvent):void{
    playPosition = t_hs_control.value;
    }
    //放开进度条,再把PLAYPOSITION的值发给播放器;
    private function thumbRelease(event:SliderEvent):void{
    _inStream.seek(playPosition);
    _inStream.togglePause();
    addEventListener(Event.ENTER_FRAME,__onEnterFrame);
    }
    //声音音量控制
    private function sound_thumbChanges(event:SliderEvent):void{
    soundPosition = hs_sound.value;
    }
    private function sound_thumbRelease(event:SliderEvent):void{
    t_flv_video.volume = soundPosition;
    }
    //格式化时间
    private function formatTimes(value:int):String{
    var result:String = (value % 60).toString();
    if (result.length == 1){
    result = Math.floor(value / 60).toString() + “:0″ + result;
    } else {
    result = Math.floor(value / 60).toString() + “:” + result;
    }
    return result;
    }
    //录制按钮点击后事件:录制视频,同时分析是否播放来调整BUTTON上的标签,显示为开始录制或者停止录制;
    //并监听播放器
    private function recVideo(event:MouseEvent):void
    {
    if(!isrecing) //开始录制
    {
    isrecing = true;
    t_btn_rec.label = “停止录制”;
    t_btn_play.enabled = false;
    t_btn_save.enabled = false;
    t_lbl_rec.visible = true;
    beginOrShowRecVideo();
    }
    else //停止录制
    {
    isrecing = false;
    t_btn_rec.label = “开始录制”;
    t_btn_play.enabled = true;
    t_btn_save.enabled = true;
    t_lbl_rec.visible = false;
    _outStream.close();
    }
    }
    //检测摄像头权限事件
    private function __onStatusHandler(event:StatusEvent):void
    {
    if(!_camera.muted)
    {
    t_btn_rec.enabled = true;
    }
    else
    {
    trace(”错误:无法链接到活动摄像头!”)
    }
    _camera.removeEventListener(StatusEvent.STATUS,__onStatusHandler);
    }
    //网络链接事件
    //如果网络连接成功,开始录制或观看视频
    private function __onNetStatusHandler(event:NetStatusEvent):void
    {
    switch (event.info.code)
    {
    case “NetConnection.Connect.Success”:
    if(isrecing)
    {
    beginRecConnectStream();
    }
    else
    {
    showRecConnectStream();
    }
    break;
    case “NetConnection.Connect.Failed”:
    trace(”连接失败!”);
    break;
    case “NetStream.Play.StreamNotFound”:
    trace(”Stream not found: ” + event);
    break;
    }
    }
    private function __onSecurityErrorHandler(event:SecurityErrorEvent):void
    {
    trace(”securityErrorHandler:” + event);
    }
    private function __onStreamErrorHandler(event:AsyncErrorEvent):void
    {
    trace(event.error.message);
    }
    //播放视频实时事件
    //实时更改播放进度条值和播放时间值,当视频播放完成时删除实时侦听事件并重新设置一些初始值
    private function __onEnterFrame(event:Event):void
    {
    if(_duration > 0 && _inStream.time > 0)
    {
    t_hs_control.value =_inStream.time;
    t_lbl_playtime.text = formatTimes(_inStream.time) + ” / “+ formatTimes(_duration);
    }
    if(_inStream.time == _duration)
    {
    removeEventListener(Event.ENTER_FRAME,__onEnterFrame);
    resetSomeParam();
    }
    }
    ]]>
    </mx:Script>
    <!–通过HTTPSERVICE来分析XML,然后得出RESULT,结果的反馈在SCRIPT里 –>
    <!–此为读取XML扩展内容
    <mx:HTTPService id=”videoserver” url=”assets/videos.xml”  result=”readXml(event)”/>
    –>
    <!–主要的视频播放窗口 设置ID为FLVVIDEO,这个很重要,其他坐标可以随自己喜欢  –>
    <mx:Panel x=”12″ y=”10″ width=”342″ height=”282″ layout=”absolute”>
    <mx:VideoDisplay   id=”t_flv_video” x=”1″ y=”1″ width=”320″ height=”240″/>
    <mx:Label x=”243.5″ y=”6″ text=”正在录制中…” id=”t_lbl_rec” color=”#666666″ fontSize=”12″/>
    </mx:Panel>
    <!–播放器的播放进度条,用FLEX自带的HSLIDER来表现播放进度,同时可以拖动影片 –>
    <mx:HSlider id=”t_hs_control” x=”12″ y=”296″ minimum=”0″
    thumbPress=”thumbPress(event)”
    thumbRelease=”thumbRelease(event)”
    change=”thumbChanges(event)” />
    <!–播放器声音控制 –>
    <mx:HSlider id=”hs_sound” x=”260″ y=”295″ width=”80″
    minimum=”0″ maximum=”1″
    thumbRelease=”sound_thumbRelease(event)”
    change=”sound_thumbChanges(event)”
    value=”{t_flv_video.volume}” />
    <!– 播放按钮,根据是否在播放,按钮显示为:播放 或者 暂停 –>
    <mx:Button id=”t_btn_play” x=”22″ y=”320″ click=”flvplay(event)” label=”播放” fontSize=”12″/>
    <!– 播放按钮,停止播放影片 –>
    <mx:Button id=”t_btn_stop” label=”停止” x=”85″ y=”320″
    click=”flvStop(event)”  fontSize=”12″ enabled=”true”/>
    <!– 时间显示 –>
    <mx:Label x=”170″ y=”300″ id=”t_lbl_playtime”
    text=”0:00 / 0:00″
    color=”#ffffff”/>
    <!–录制按钮,根据是否在录制,按钮显示为:开始录制 或者 停止录制–>
    <mx:Button x=”210″ y=”320″ label=”开始录制” click=”recVideo(event)” fontSize=”12″ id=”t_btn_rec”/>
    <!–保存视频按钮–>
    <mx:Button x=”299″ y=”320″ label=”保存” fontSize=”12″ id=”t_btn_save” enabled=”true”/>
    </mx:Application>
    上面的代码中还有许多不足之处,目前播放的时候音量大小控制没有生效,大家请多多指正,谢谢。
    本文来源于 冰山上的播客 http://xinsync.xju.edu.cn , 原文地址:http://xinsync.xju.edu.cn/index.php/archives/1703
  • 相关阅读:
    JVM常用参数设置
    Jstat在分析java的内存GC时的应用
    jstack来分析linux服务器上Java应用服务性能异常
    linux 远程连接服务器ftp命令整理
    LR11中webservice协议的性能测试应用
    Windbg在.net性能问题排查hang情况的应用思路
    Windbg基本命令应用总结
    LR11直接对数据库访问操作方法在性能测试中的应用总结
    BenchmarkSQL v5.0测试达梦数据库
    SSH登录时间久,登录后报错:'abrt-cli status' timed out
  • 原文地址:https://www.cnblogs.com/zyosingan/p/1182217.html
Copyright © 2020-2023  润新知