全部产品
云市场

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);

    代码示例

    ```actionscriptpackage{ 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 {

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

    }

}```