控制隱藏字幕可見度

您可以控制隱藏字幕的可見度。 當可見性開啟時,會顯示目前選取的軌道。 如果您變更了目前的軌道,可見度設定會維持不變。

秘訣

如果當播放器進入搜尋模式時顯示隱藏字幕文字,則搜尋完成後不會再顯示文字。 相反地,在數秒後,TVSDK會在結束搜尋位置後,在視訊中顯示下一個隱藏字幕文字。

注意

隱藏字幕的可見度值定義在ClosedCaptionsVisibility中。

public static const HIDDEN:String = hidden;
public static const VISIBLE:String = visible;
  1. 等待MediaPlayer至少具有PREPARED狀態(請參閱等待有效狀態)。

  2. 若要取得隱藏字幕的目前可見性設定,請使用MediaPlayer中的getter方法,此方法會傳回可見性值。

    public function get ccVisibility():String
    
  3. 若要變更隱藏字幕的可見度,請使用setter方法,從ClosedCaptionsVisibility傳遞可見度值。

    例如:

    public function set ccVisibility(value:String):void
    
  4. 定義下拉式清單。

    <s:DropDownList id="ccTracksList" width="85" 
                    dataProvider="{_ccTracks}" 
                    change="onCCTrackChange(event)" 
                    prompt="CC"/>
    
  5. 定義隱藏字幕軌道的可系結陣列。

    [Bindable] private var _ccTracks:ArrayCollection =  
      new ArrayCollection(); // active tracks 
    
  6. 設定監聽器。

    player.addEventListener(MediaPlayerItemEvent.ITEM_CREATED, onItemCreated); 
    player.addEventListener(MediaPlayerItemEvent.CAPTIONS_UPDATED, onCaptionUpdated);
    

    要從銷毀代碼中刪除監聽程式,請執行以下操作:

    player.removeEventListener(MediaPlayerItemEvent.ITEM_CREATED, onItemCreated); 
    player.removeEventListener(MediaPlayerItemEvent.CAPTIONS_UPDATED, onCaptionUpdated);
    
  7. 當使用者從清單中選擇時,建立並更新清單。

    private function onCCTrackChange(event:IndexChangeEvent):void { 
        var ccTrackIndex:int = event.newIndex; 
        var ccTracks:Vector.<ClosedCaptionsTrack> =  
          _player.currentItem.closedCaptionsTracks; 
        if (ccTrackIndex == 0) { 
            _player.ccVisibility = MediaPlayer.INVISIBLE; 
        } 
        else if (ccTrackIndex <= _ccTracks.length) { 
            var index:Number = findFromActiveIndex(ccTracks, ccTrackIndex - 1); 
            _player.currentItem.selectClosedCaptionsTrack(ccTracks[index]); 
            _player.ccVisibility = MediaPlayer.VISIBLE; 
        } 
    } 
    
    private function findFromActiveIndex(ccTracks:Vector.<ClosedCaptionsTrack>,  
      ccTrackIndex:int):Number { 
        var count:Number = 0; 
        for each (var ccTrack:ClosedCaptionsTrack in ccTracks) { 
            if (count < ccTrackIndex) 
                count = count + 1; 
            else 
                return count; 
        } 
        return -1; 
    } 
    
    private function onItemCreated(event:MediaPlayerItemEvent):void { 
        ... (you are likely to need more code here for other reasons) 
        updateCCTracks(_player.currentItem.closedCaptionsTracks); 
    } 
    
    private function onCaptionUpdated(event:MediaPlayerItemEvent):void { 
        ... (you are likely to need more code here for other reasons) 
        updateCCTracks(_player.currentItem.closedCaptionsTracks,  
                      (_player.ccVisibility == MediaPlayer.VISIBLE) ?  
                       _player.currentItem.selectedClosedCaptionsTrack : null); 
    } 
    
    private function updateCCTracks(tracks:Vector.<ClosedCaptionsTrack>,  
      selectedTrack:ClosedCaptionsTrack = null):void { 
        _ccTracks.removeAll(); 
    
        _ccTracks.addItem( 
            { 
                "label": "CC off", 
                "data": "cc-off" 
            } 
        ); 
    
        var selectedIndex:int = 0; 
        for each (var ccTrack:ClosedCaptionsTrack in tracks) { 
            _ccTracks.addItem( 
                { 
                    "label": ccTrack.name, 
                    "data": ccTrack.name 
                } 
            ); 
            if (selectedTrack && ccTrack.name == selectedTrack.name && 
            ccTrack.language == selectedTrack.language && 
            ccTrack.serviceType == selectedTrack.serviceType) { 
                selectedIndex = _ccTracks.length - 1; 
            } 
        } 
    
        var hasCC:Boolean = _ccTracks.length > 0; 
        ccTracksList.enabled = hasCC; 
        ccTracksList.mouseEnabled = hasCC; 
        ccTracksList.selectedIndex = selectedIndex; 
    } 
    

本頁內容