vkwave

Seeking not working in netStream byteArray

Jan 21st, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" width="450" height="400"
  3.                        xmlns:s="library://ns.adobe.com/flex/spark"
  4.                        xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()">
  5.     <fx:Declarations>
  6.         <!-- Place non-visual elements (e.g., services, value objects) here -->
  7.     </fx:Declarations>
  8.    
  9.     <fx:Script>
  10.         <![CDATA[
  11.            
  12.             private var ns:NetStream;
  13.             private var byteArray:ByteArray;
  14.             public function init():void
  15.             {
  16.                 var myVideo:Video = new Video(400,300);  
  17.                 var nc:NetConnection = new NetConnection();
  18.                 nc.connect(null);
  19.                
  20.                 var customClient:Object = new Object();
  21.                
  22.                 ns = new NetStream(nc);
  23.                 ns.client = customClient;
  24.                
  25.                 myVideo.attachNetStream(ns);
  26.                
  27.                 ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
  28.  
  29.                 // important: play(null), otherwise exception
  30.                 ns.play(null);
  31.                
  32.                 var fileName:String = "dummy-video.flv";
  33.                 byteArray = readFile(fileName);
  34.                 ns.appendBytes(byteArray);
  35.                
  36.                 myUIComponent.addChild(myVideo);
  37.             }
  38.            
  39.             private function netStatusHandler(event: NetStatusEvent): void
  40.             {  
  41.                 var code:String = event.info.code;
  42.                 trace("Code: "+code);
  43.                
  44.                 switch (event.info.code)
  45.                 {
  46.                     case "NetStream.Seek.Notify":
  47.                         //seekPoint = event.info.seekPoint;
  48.                         trace("seeking to: ", seekPoint);
  49.                         seekPoint == 0 ? seekToBeginning() : seekToOffset(seekPoint);
  50.                         break;
  51.                    
  52.                     case "NetStream.Seek.Failed":
  53.                         trace("Seeking is Failed.");
  54.                         break;
  55.                    
  56.                     case "NetStream.Buffer.Full":
  57.                         break;
  58.                    
  59.                     case "NetStream.Buffer.Empty":
  60.                         ns.seek(0);
  61.                         //seekToBeginning();
  62.                         break;
  63.                    
  64.                     case "NetStream.Pause.Notify":
  65.                         break;
  66.                    
  67.                     case "NetStream.Unpause.Notify":
  68.                         break;
  69.                 }
  70.             }
  71.            
  72.             private function readFile(fileName:String):ByteArray
  73.             {
  74.                 var data:ByteArray = new ByteArray();
  75.                 var inFile:File = File.applicationDirectory; // source folder is application
  76.                 inFile = inFile.resolvePath(fileName);  // name of file to read
  77.                 var inStream:FileStream = new FileStream();
  78.                 inStream.open(inFile, FileMode.READ);
  79.                 inStream.readBytes(data, 0, data.length);
  80.                 inStream.close();
  81.                
  82.                 return data;
  83.             }
  84.            
  85.             private function seekToOffset(seekPoint:Number):void
  86.             {
  87.                 trace("Seeking Notify to Offset");
  88.                 //ns.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
  89.                 //ns.seek(seekPoint);
  90.             }
  91.            
  92.             private function seekToBeginning():void
  93.             {
  94.                 ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
  95.                 ns.appendBytes(byteArray);
  96.                 ns.appendBytesAction(NetStreamAppendBytesAction.END_SEQUENCE);
  97.             }
  98.            
  99.             private function playVideo():void
  100.             {
  101.                 ns.resume();
  102.             }
  103.            
  104.             private function pauseVideo():void
  105.             {
  106.                 ns.pause();
  107.             }
  108.            
  109.             private function stopVideo():void
  110.             {
  111.                 ns.pause();
  112.                 ns.seek(0);
  113.             }
  114.            
  115.             private var seekPoint:Number;
  116.             private function videoSeek():void
  117.             {
  118.                 seekPoint = 13472; // seek to a specific location
  119.                 ns.seek(seekPoint);
  120.             }
  121.            
  122.         ]]>
  123.     </fx:Script>
  124.    
  125.     <mx:VBox width="100%" height="100%">
  126.         <mx:VBox height="85%" width="100%">
  127.             <mx:UIComponent id="myUIComponent" width="100%" height="100%" />   
  128.         </mx:VBox>
  129.         <mx:HBox>
  130.             <s:Button label="Play" click="playVideo()"/>
  131.             <s:Button label="Pause" click="pauseVideo()"/>
  132.             <s:Button label="Stop" click="stopVideo()"/>
  133.             <s:Button label="Seek" click="videoSeek()"/>
  134.         </mx:HBox>
  135.     </mx:VBox>
  136.    
  137. </s:WindowedApplication>
Add Comment
Please, Sign In to add comment