video buffering example for stackoverflow
♥0 |
Line 208 |
Modified 2011-01-23 03:45:40 |
MIT License
archived:2017-03-10 05:41:52
ActionScript3 source code
/**
* Copyright www0z0k ( http://wonderfl.net/user/www0z0k )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3dVV
*/
package
{
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.net.NetStream;
import flash.events.AsyncErrorEvent;
import flash.media.Video;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.system.Capabilities;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class NewClass extends Sprite
{
private var _connection:NetConnection;
//private var _videoURL:String = "http://www0z0k.webs.com/res/usavich(Prison%20the%20rabbit)1-5.mp4";
private var _videoURL:String = "http://www0z0k.narod.ru/silence.flv";
private var stream:NetStream;
private var playBtn:Sprite;
private var pauseBtn:Sprite;
private var scroller:Sprite;
private var bGScroller:Sprite;
private var client:Object;
private var metaObj:Object;
private var timer:Timer;
private var log:TextField;
private var usefulInfoFromInit: String;
private var currentScrollPos:int = 0;
public function NewClass()
{
playBtn = new Sprite();
pauseBtn = new Sprite();
with(playBtn.graphics){
beginFill(0xff0000);
lineTo(0, 24);
lineTo(12, 12);
lineTo(0, 0);
endFill();
}
with(pauseBtn.graphics){
beginFill(0x0000ff);
lineTo(0, 24);
lineTo(24, 24);
lineTo(24, 0);
lineTo(0, 0);
endFill();
}
log = new TextField();
scroller = new Sprite();
with(scroller.graphics){
beginFill(0x0000ff);
lineTo(0, 1);
lineTo(8, 1);
lineTo(8, 0);
lineTo(0, 0);
endFill();
}
bGScroller = new Sprite();
timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
usefulInfoFromInit = Capabilities.version + '\n';
_connection = new NetConnection();
_connection.addEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler);
_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandler);
_connection.connect(null);
// _connection.connect("rtmp://localhost/DYVideo");
}
private function connectionStatusHandler(e:NetStatusEvent):void
{
if (e.info.code == "NetConnection.Connect.Success")
connectStream();
}
private function streamStatusHandler(e:NetStatusEvent):void
{
//usefulInfoFromInit += "stream status: " + e.info.code + '\n';
switch (e.info.code) {
case "NetStream.Play.StreamNotFound":
usefulInfoFromInit += "Unable to locate the video " + _videoURL + '\n';
break;
case "NetStream.Play.Stop":
stream.seek(0);
stream.play(_videoURL);
break;
}
}
private function securityHandler(e:SecurityErrorEvent):void
{
usefulInfoFromInit += "security error " + e + '\n';
}
private function connectStream():void
{
if (stream != null)
stream.close();
stream = new NetStream(_connection);
client = new Object();
client.onMetaData = metaDataHandler;
stream.client = client;
stream.addEventListener(NetStatusEvent.NET_STATUS, streamStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
usefulInfoFromInit += 'stream inited \n';
var video:Video = new Video();
video.attachNetStream(stream);
stream.play(_videoURL);
timer.start();
addChild(video);
addChild(playBtn);
addChild(pauseBtn);
addChild(log);
addChild(bGScroller);
addChild(scroller);
log.multiline = true;
log.wordWrap = true;
log.addEventListener(MouseEvent.MOUSE_WHEEL, scroll);
pauseBtn.x = 30;
playBtn.y = 5 + video.height;
pauseBtn.y = 5 + video.height;
log.width = stage.stageWidth - 1;
log.y = 10 + video.height + 24;
log.height = stage.stageHeight - log.y - 1;
log.border = true;
log.borderColor = 0;
with(bGScroller.graphics){
beginFill(0xaaaaaa);
lineTo(0, log.height);
lineTo(8, log.height + 2);
lineTo(8, 0);
lineTo(0, 0);
endFill();
}
bGScroller.x = log.x + log.width - bGScroller.width;
bGScroller.y = log.y;
scroller.x = log.x + log.width - scroller.width;
scroller.y = log.y;
bGScroller.addEventListener(MouseEvent.MOUSE_MOVE, onMoveScroll);
bGScroller.addEventListener(MouseEvent.CLICK, onClickScroll);
bGScroller.buttonMode = true;
//bGScroller.mouseChildren = false;
playBtn.buttonMode = true;
pauseBtn.buttonMode = true;
playBtn.addEventListener(MouseEvent.CLICK, playClickHandler);
pauseBtn.addEventListener(MouseEvent.CLICK, pauseClickHandler);
log.text = usefulInfoFromInit;
}
private function onClickScroll(e:MouseEvent):void {
if(e.stageY <= log.y + log.height + 2 && e.stageY >= log.y) {
currentScrollPos = Math.ceil(((e.stageY - log.y) / log.height) * log.maxScrollV);
updateScroll();
}
}
private function onMoveScroll(e:MouseEvent):void {
if (e.buttonDown) {
onClickScroll(e);
}
}
private function asyncErrorHandler(e:AsyncErrorEvent):void
{
//nothing here
}
private function playClickHandler(e:MouseEvent):void
{
stream.resume();
//timer.start();
}
private function pauseClickHandler(e:MouseEvent):void
{
stream.pause();
//timer.stop()
}
private function metaDataHandler(info:Object):void
{
var tmpstr:String = '';
for(var s:String in info){
var tstr:String = s + ' = ' + info[s] + '\n';
tmpstr += tstr.indexOf('object') == -1 ? tstr : '';
for(var a:String in info[s]){
var ttstr:String = s + ':' + a + ' = ' + info[s][a] + '\n';
tmpstr += ttstr.indexOf('object') == -1 ? ttstr : '';
for(var c:String in info[s][a]){
var tttstr:String = s + ':' + a + ':' + c + ' = ' + info[s][a][c] + '\n';
tmpstr += tttstr.indexOf('object') == -1 ? tttstr : '';
}
}
}
usefulInfoFromInit += tmpstr;
log.text = usefulInfoFromInit;
stream.maxPauseBufferTime = info.duration;
stream.bufferTime = info.duration;
metaObj = info;
}
private function scroll(e:MouseEvent):void{
currentScrollPos -= e.delta;
updateScroll();
}
private function timerHandler(e:TimerEvent):void
{
try
{
log.text = usefulInfoFromInit + "\n[time in buffer: " + stream.bufferLength + ' + current time : ' + stream.time + '] = ' + (stream.bufferLength + stream.time) + " / video duration: " + metaObj.duration + "\ndata in buffer: " + stream.bytesLoaded + " / " + stream.bytesTotal + '\n log.maxScrollV = ' + log.maxScrollV + ' currentScrollPos = ' + currentScrollPos + ' log.height = ' + log.height;
}
catch (e:Error)
{}
updateScroll();
}
private function updateScroll():void {
currentScrollPos = currentScrollPos < 0 ? 0 : currentScrollPos > log.maxScrollV ? log.maxScrollV : currentScrollPos;
log.scrollV = currentScrollPos;
scroller.scaleY = (log.textHeight <= log.height ? log.height : log.height * (log.height / log.textHeight));
scroller.y = log.y + (log.height - scroller.height) * (currentScrollPos / log.maxScrollV);
}
}
}