在用BitMapData.draw()方法去取一个视频图片时会报安全沙箱错误,以下是解决方法:
在创建NetStream时video先不要显示视频流(_video.attachNetStream(null)),等到播放视频时再显示视频(_video.attachNetStream(_netStream))
protected function CreateNetStream():void { _netStream = new NetStream(_connection); _netStream.addEventListener(NetStatusEvent.NET_STATUS,NetStatusHandler, false, 0, true); _netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,AsyncErrorHandler, false, 0, true); _netStream.addEventListener(IOErrorEvent.IO_ERROR,IOErrorHandler,false, 0, true); _netStream.client={onMetaData:onMetaData,onPlayStatus:onPlayStatus}; //_video.attachNetStream(_netStream); _video.attachNetStream(null); /******* 设置声音 *******/ _netStream.soundTransform = _videoSound; _oldSound = _netStream.soundTransform; if(_controlBar != null) _controlBar.VolumePane.slider.value = _netStream.soundTransform.volume *100; }
/** * 播放方法 * @param url_ 播放地址 */ public function PlayVideo(url_:String = ''):void { _controlBar.btnPlayorPause.status = ButtonStatus.ORIGINALSTATUS; if(url_== ""){ url_ = _mediaUrl; } else if(_mediaUrl != url_){ _mediaUrl = url_; } if(_mediaUrl == "" || _mediaUrl == null){ throw new Error("The url is null!"); return; } if(_controlBar != null){ _controlBar.visible = true; _controlBar.alpha = 0; } _netStream.play(_mediaUrl); _video.attachNetStream(_netStream); HideItem(_btnPlay); _playStatus = PLAYING; if(_TimeArea != null){ _TimeArea.visible = true; _TimeArea.setTotalTime(_totalTime); } if(_thumb != null && this.contains(_thumb)){ this.removeChild(_thumb); } }
这样播放视频前和正在播放时都可以BitMapData.draw()方法去取一个视频图片而不会出现安全沙箱错误。。。不过如果想这样就算是解决了安全沙箱错误问题就大错特错了,不信,停止播放再draw()一下试试,又出现安全沙箱错误了吧。
/** * 停止播放方法 */ public function Stopvideo():void { //停止播放并清除视频当前画面 _netStream.close(); _video.clear(); //////////////////////////////////////////////////////////////// /**当用bitmapdata.draw画video时会出现安全沙箱错误,当video.attachNetStream已设置过netStream后再设置为null也会出错 * 下面是克隆一个新的video来解决出现安全沙箱错误的 * */ _video.attachNetStream(null); var tv:Video=cloneVideo(_video); if(_video.hasEventListener(MouseEvent.CLICK)){ _video.removeEventListener(MouseEvent.CLICK, onVideoClick); tv.addEventListener(MouseEvent.CLICK, onVideoClick); } if(this.contains(_video)){ this.addChild(tv); this.setChildIndex(tv,this.getChildIndex(_video)); this.removeChild(_video); } _video=tv; _video.attachNetStream(null); ////////////////////////////////////////////////////////////// if(_thumb != null && !this.contains(_thumb)){ this.addChild(_thumb); this.setChildIndex(_thumb, this.getChildIndex(_video) + 1); } ShowItem(_btnPlay); _playStatus = STOP; _controlBar.btnPlayorPause.status = ButtonStatus.EXCHANGESTATUS; _playTimer.stop(); if(_TimeArea != null){ _TimeArea.visible = false; _TimeArea.setTotalTime(0); _TimeArea.UpdateTime(0); } if(_controlBar != null){ _controlBar.visible = false; // _controlBar.alpha = 0; } }
private function cloneVideo(v:Video):Video{
var tv:Video= new Video(_videoWidth, _videoHeight);
tv.smoothing = v.smoothing;
tv.x=v.x;
tv.y=v.y;
tv.width=v.width;
tv.height=v.height;
return tv;
}