允許使用者變更標題軌道

此程式是如何建立按鈕的範例,讓使用者選取隱藏字幕軌道。

  1. 建立按鈕以變更隱藏字幕軌道。

    <Button 
      android:id="@+id/selectCC" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="10dp" 
      android:onClick="selectClosedCaptioningClick" 
      android:text="CC" /> 
    
  2. 將可用隱藏字幕音軌清單轉換為字串陣列。

    相應地標籤具有活動(即TVSDK已發現資料的頻道)的隱藏字幕軌道。

    /** 
    * Converts the closed captions tracks to a string array. 
    * 
    * @return array of CC tracks 
    */ 
    private String[] getCCsAsArray() { 
        List<String> closedCaptionsTracksAsStrings = new ArrayList<String>(); 
        MediaPlayerItem currentItem = mediaPlayer.getCurrentItem(); 
        if (currentItem != null) { 
            List<ClosedCaptionsTrack> closedCaptionsTracks = 
            currentItem.getClosedCaptionsTracks(); 
            Iterator<ClosedCaptionsTrack> iterator = closedCaptionsTracks.iterator(); 
            while (iterator.hasNext()) { 
                ClosedCaptionsTrack closedCaptionsTrack = iterator.next(); 
                String isActive = closedCaptionsTrack.isActive() ? " (" +  
                  getString(R.string.active)+ ")" : ""; 
                closedCaptionsTracksAsStrings.add(closedCaptionsTrack.getName() +  
                  isActive); 
            } 
        } 
        return closedCaptionsTracksAsStrings. 
          toArray(new String[closedCaptionsTracksAsStrings.size()]); 
    } 
    
  3. 當使用者按一下按鈕時,會顯示對話方塊,列出所有預設的隱藏字幕軌道。

    public void selectClosedCaptioningClick(View view) { 
        Log.i(LOG_TAG + "#selectClosedCaptions", "Displaying closed captions chooser dialog."); 
        final String items[] = getCCsAsArray(); 
        final AlertDialog.Builder ab = new AlertDialog.Builder(this); 
        ab.setTitle(R.string.PlayerControlCCDialogTitle); 
        ab.setSingleChoiceItems(items, selectedClosedCaptionsIndex, new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
                // Select the new closed captioning track. 
                MediaPlayerItem currentItem = mediaPlayer.getCurrentItem(); 
                ClosedCaptionsTrack desiredClosedCaptionsTrack =  
                  currentItem.getClosedCaptionsTracks().get(whichButton); 
                boolean result = currentItem.selectClosedCaptionsTrack(desiredClosedCaptionsTrack); 
                if (result) { 
                    selectedClosedCaptionsIndex = whichButton; 
                } 
                // Dismiss dialog. 
                dialog.cancel(); 
         } 
        }).setNegativeButton(R.string.PlayerControlCCDialogCancel, new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 
                // Just cancel the dialog. 
            } 
        }); 
        ab.show(); 
    } 
    

本頁內容