Show Menu
TOPICS×

Example: Allow users to change the caption track

This is an example of how to create a button that allows a user to select a closed-caption track.
  1. Create a simple button to change the closed-caption track.
       <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. Convert the list of available closed caption tracks to a string array. The closed-caption tracks that have activity—that is, channels for which TVSDK has discovered data—are marked accordingly:
    /** 
    * 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. When the user clicks the button, display a dialog that lists all the default CC tracks.
       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(); 
    }