允許使用者變更註解追蹤

上次更新: 2023-09-21

此程式範例說明如何建立可讓使用者選取隱藏式字幕追蹤的按鈕。

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

此頁面上的