全部产品
云市场

PCDN SDK flash版本手册(点播)

更新时间:2018-04-11 14:46:27

概述

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

SDK集成步骤

1.播放器使用loader远程加载pcdn_acc_live.swf

  1. var urlRequest:URLRequest = new URLRequest(pcdnlive_url);
  2. var context:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain);
  3. loader = new Loader();
  4. loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwfLoaded);
  5. loader.load(urlRequest,context);

2.加载成功后,从pcdn_acc_live.swf获取NetStream对象

  1. var pcdnlive:NetStream = event.currentTarget.content.stream;

3.使用NetStream对象设置clientId,就是注册生成的token

  1. pcdnlive["setClientId"](clientId);

4.使用NetStream对象监听NetStream播放相关的事件

  1. pcdnlive.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);

5.使用NetStream对象调用play接口播放视频文件

  1. pcdnlive.play(playUrl);

代码示例

  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.media.Video;
  6. import flash.net.NetConnection;
  7. import flash.net.NetStream;
  8. import flash.net.URLRequest;
  9. import flash.system.LoaderContext;
  10. import flash.system.SecurityDomain;
  11. import flash.display.Loader;
  12. import flash.display.LoaderInfo;
  13. import flash.system.ApplicationDomain;
  14. import flash.events.NetStatusEvent;
  15. import flash.system.Security;
  16. [SWF(width="960", height="540")]
  17. public class Main extends Sprite
  18. {
  19. private var pcdnlive_url:String = "http://test-sdk.alicdn.com/p.swf";
  20. private var playUrl:String = "http://p-c.alicdn.com/p/t.flv";
  21. private var clientId:String = "yourtokenId";
  22. private var loader:Loader;
  23. private var video:Video;
  24. private var nc:NetConnection;
  25. private var pcdnlive:NetStream;
  26. public function Main():void
  27. {
  28. Security.allowDomain("*");
  29. Security.allowInsecureDomain("*");
  30. if (stage) init();
  31. else addEventListener(Event.ADDED_TO_STAGE, init);
  32. }
  33. private function init(e:Event = null):void
  34. {
  35. removeEventListener(Event.ADDED_TO_STAGE, init);
  36. // entry point
  37. loadswf();
  38. }
  39. //加载pcdn_acc_live.swf
  40. private function loadswf():void {
  41. var urlRequest:URLRequest = new URLRequest(pcdnlive_url);
  42. var context:LoaderContext = new LoaderContext(true,ApplicationDomain.currentDomain);
  43. loader = new Loader();
  44. loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwfLoaded);
  45. loader.load(urlRequest,context);
  46. }
  47. //获取Netstream对象
  48. private function onSwfLoaded(event:Event):void {
  49. pcdnlive = event.currentTarget.content.stream;
  50. pcdnlive.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
  51. pcdnlive.client = this;
  52. //设置clientId
  53. pcdnlive["setClientId"](clientId);
  54. //播放直播地址
  55. pcdnlive.play(playUrl);
  56. video = new Video();
  57. video.width = 960;
  58. video.height = 540;
  59. video.attachNetStream(pcdnlive);
  60. addChild(video);
  61. }
  62. //NetStream的事件
  63. private function netStatusHandler(e:NetStatusEvent):void {
  64. switch (e.info.code) {
  65. case "NetStream.Play.StreamNotFound":
  66. break;
  67. case "NetStream.Play.Start":
  68. break;
  69. case "NetStream.Unpause.Notify"://暂停后恢复播放状态
  70. break;
  71. case "NetStream.Buffer.Full"://缓冲区已满,流开始播放
  72. break;
  73. case "NetStream.Pause.Notify"://暂停
  74. break;
  75. case "NetStream.Buffer.Empty"://发起缓冲
  76. break;
  77. case "NetStream.Token.Success":// sdk鉴权通过,无需任何操作
  78. break;
  79. case "NetStream.Token.Failed":// sdk鉴权失败,
  80. break;
  81. }
  82. }
  83. public function play():void {
  84. pcdnlive.play(playUrl);
  85. }
  86. public function close():void {
  87. pcdnlive.close();
  88. }
  89. }
  90. }