播放受保護內容的工作流程

  1. 建立 MediaPlayer 實體。
    player = new MediaPlayer();
  2. 向播放程式註冊 MediaPlayerCapabilityChangeEvent.HAS_DRM_CHANGE 事件。如果內容受 DRM 保護,便會傳送此事件。
    player.addEventListener(MediaPlayerCapabilityChangeEvent.HAS_DRM_CHANGE, onDRMCapabilityChange);
  3. 在事件處理常式中,取得 DRMTrait 實體。您可以透過 DRMTrait 介面來叫用 DRM 相關方法,例如 authenticate() 。載入 DRM 保護的內容時,OSMF 會執行 DRM 驗證動作並傳送狀態事件。將 DRMEvent.DRM_STATE_CHANGE 事件處理常式新增至 DRMTrait
    private function onDRMCapabilityChange 
        (event :MediaPlayerCapabilityChangeEvent) :void 
            { 
                if (event.type == MediaPlayerCapabilityChangeEvent.HAS_DRM_CHANGE 
                    && event.enabled) 
                { 
                    drmTrait = player.media.getTrait(MediaTraitType.DRM) as DRMTrait; 
                    drmTrait.addEventListener 
                        (DRMEvent.DRM_STATE_CHANGE,                     onDRMStateChange); 
                } 
            }
  4. onDRMStateChange() 方法處理 DRM 事件。
            private function onDRMStateChange(event :DRMEvent) :void 
            { 
                trace ( "DRMState: ",event.drmState); 
                switch(event.drmState) 
                { 
                    case DRMState.AUTHENTICATION_NEEDED: 
                        // Identity-based content 
                        var authPopup :AuthWindow = AuthWindow.create(_parentWin); 
                        authPopup.serverURL = event.serverURL; 
                        authPopup.addEventListener("dismiss", function () :void { 
                            trace ("Authentication dismissed"); 
                            if(_drmTrait != null) 
                            { 
                                //Ignore authentication. Just 
                                //try to acquire a license. 
                                _drmTrait.authenticate(null, null); 
                            } 
                        }); 
                        authPopup.addEventListener("authenticate", 
                                        function (event :AuthWindowEvent) :void { 
                            if(_drmTrait != null) 
                            { 
                                _drmTrait.authenticate(event.username, event.password); 
                            } 
                        }); 
                        authPopup.show(); 
                        break; 
                    case DRMState.AUTHENTICATING: 
                        //Display any authentication message. 
                        trace("Authenticating..."); 
                        break; 
                    case DRMState.AUTHENTICATION_COMPLETE: 
                        // Start to retrieve voucher and playback. 
                        // You can display the voucher information at this point. 
                        if(event.token) 
                        // You just received the authentication token. 
                        { 
                            trace("Authentication success. Token: \n", event.token); 
                        } 
                        else 
                        // You have got the voucher. 
                        { 
                            trace("DRM License:");                         
                            trace("Playback window period: ", 
                                !isNaN(event.period) ? event.period == 0 ? 
                                "<unlimited>" : event.period : "<none>"); 
                            trace("Playback window end date: ", 
                                event.endDate != null ? event.endDate : "<none>"); 
                            trace("Playback window start date: ", 
                                event.startDate != null ? event.startDate : "<none>"); 
                        } 
                        break; 
                    case DRMState.AUTHENTICATION_ERROR: 
                        trace ("DRM Error:", event.mediaError.errorID + 
                            "[" + DRMErrorEventRef.getDRMErrorMnemonic 
                            (event.mediaError.errorID) + "]"); 
                        //Stop everything. 
                        player.media = null; 
                        break; 
                    case DRMState.DRM_SYSTEM_UPDATING: 
                        Logger.log("Downloading DRM module..."); 
                        break; 
                    case DRMState.UNINITIALIZED: 
                        break; 
                } 
            }