本文为您介绍SDK flash版本手册(点播)使用说明和操作步骤。

概述

SDK以.swf文件方式提供,名称是pcdn_acc_live.swf。SDK采取远程加载的方式调用,对外提供的是一个继承自NetStream类的LiveStream类对象,该LiveStream类支持NetStream的标准接口,方便您的播放器进行集成;唯一的区别是,您需要设置注册时生成的tokenId,用于SDK完成合法性验证。

SDK集成步骤

  1. 播放器使用loader远程加载pcdn_acc_live.swf。
    var urlRequest:URLRequest = new URLRequest(pcdnlive_url);          
    var context:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain);
    loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwfLoaded);
    loader.load(urlRequest,context);
  2. 加载成功后,从pcdn_acc_live.swf获取NetStream对象。
    var pcdnlive:NetStream = event.currentTarget.content.stream;
  3. 使用NetStream对象设置clientId,就是注册生成的token。
    pcdnlive["setClientId"](clientId);
  4. 使用NetStream对象监听NetStream播放相关的事件。
    pcdnlive.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
  5. 使用NetStream对象调用play接口播放视频文件。
    pcdnlive.play(playUrl);

代码示例

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.system.ApplicationDomain;
	import flash.events.NetStatusEvent;
	import flash.system.Security;
	
	[SWF(width="960", height="540")]
	public class Main extends Sprite 
	{
		private var pcdnlive_url:String = "http://test-sdk.alicdn.com/p.swf";
		private var playUrl:String = "http://p-c.alicdn.com/p/t.flv";
		private var clientId:String = "yourtokenId";
		
		private var loader:Loader;
		private var video:Video;
		private var nc:NetConnection;
		private var pcdnlive:NetStream;
		
		public function Main():void 
		{
			Security.allowDomain("*");
			Security.allowInsecureDomain("*");
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			loadswf();
			
		}
		
		//加载pcdn_acc_live.swf
		private function loadswf():void {
            var urlRequest:URLRequest = new URLRequest(pcdnlive_url);
            var context:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain);
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwfLoaded);
            loader.load(urlRequest,context);
		}
		
       //获取Netstream对象
		private function onSwfLoaded(event:Event):void {
			pcdnlive = event.currentTarget.content.stream;
			pcdnlive.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
           pcdnlive.client = this;
            //设置clientId
	       pcdnlive["setClientId"](clientId);
	       //播放直播地址
           pcdnlive.play(playUrl);
           video = new Video();
           video.width = 960;
           video.height = 540;
           video.attachNetStream(pcdnlive);
			addChild(video);
		}
		
		//NetStream的事件
		private function netStatusHandler(e:NetStatusEvent):void {
			switch (e.info.code) {
                      case "NetStream.Play.StreamNotFound":
                            break;
                      case "NetStream.Play.Start":
                            break;
                      case "NetStream.Unpause.Notify"://暂停后恢复播放状态
                            break;
                      case "NetStream.Buffer.Full"://缓冲区已满,流开始播放
                            break;
                      case "NetStream.Pause.Notify"://暂停
                            break;
                      case "NetStream.Buffer.Empty"://发起缓冲
                            break;
                      case "NetStream.Token.Success":// sdk鉴权通过,无需任何操作
                            break;
                      case "NetStream.Token.Failed":// sdk鉴权失败,
                            break;
                 }
		}
				
		public function play():void {
			pcdnlive.play(playUrl);
		}
		
		public function close():void {
			pcdnlive.close();
		}
	}
	
}