2

我使用Companion library將視頻從我的應用投射到Chromecast。 有什麼方法可以添加字幕/隱藏字幕切換按鈕,以便用戶可以打開和關閉它們嗎?Android Chromecast伴侶庫 - 字幕切換按鈕

我讀他們的documentation在那裏我可以看到,如何可以設置字幕URL

MediaTrack englishSubtitle = new MediaTrack.Builder(1 /* ID */, 
MediaTrack.TYPE_TEXT) 
    .setName("English Subtitle") 
    .setSubtype(MediaTrack.SUBTYPE_SUBTITLE) 
    .setContentId("https://some-url/caption_en.vtt") 
    /* language is required for subtitle type but optional otherwise */ 
    .setLanguage("en-US") 
    .build(); 

但是,沒有什麼地方我應該處理顯示/隱藏行動的話。

你有什麼建議,我怎麼可以添加切換按鈕和手柄顯示/隱藏行動?

我使用的是使用VideoCastControllerActivity從鑄造庫VideoCastManager

這是我CastConfiguration

// Build a CastConfiguration object and initialize VideoCastManager 
    CastConfiguration options = new CastConfiguration.Builder(MyAppConfig.CHROMECAST_APP_ID) 
      .enableAutoReconnect() 
      .enableCaptionManagement() 
      .enableDebug() 
      .enableLockScreen() 
      .enableNotification() 
      .enableWifiReconnection() 
      .setCastControllerImmersive(true) 
      .setLaunchOptions(false, Locale.getDefault()) 
      .setNextPrevVisibilityPolicy(CastConfiguration.NEXT_PREV_VISIBILITY_POLICY_DISABLED) 
      .addNotificationAction(CastConfiguration.NOTIFICATION_ACTION_REWIND, false) 
      .addNotificationAction(CastConfiguration.NOTIFICATION_ACTION_PLAY_PAUSE, true) 
      .addNotificationAction(CastConfiguration.NOTIFICATION_ACTION_DISCONNECT, true) 
      .setForwardStep(10) 
      .build(); 

    // Google Chrome Cast initialization of the VideoCastManager that is a helper class from the CasCompanionLibrary 
    // that helps us deal with the flow of communicating with chromecast 
    VideoCastManager. 
      initialize(this, options) 
      .setVolumeStep(MyAppConfig.VOLUME_INCREMENT); 

還有我創建MediaInfo

MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE); 
       mediaMetadata.addImage(new WebImage(Uri.parse(MyAppConfigBase.IMAGE_API_ENDPOINT + movieVideoItem.getImages().getKeyart()))); 
       mediaMetadata.addImage(new WebImage(Uri.parse(MyAppConfigBase.IMAGE_API_ENDPOINT + movieVideoItem.getImages().getKeyart()))); 
       mediaMetadata.putString(MediaMetadata.KEY_TITLE, movieVideoItem.getTitle()); 
       mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, movieVideoItem.getDescription()); 
       mediaMetadata.putString("movie-urls", url); 
       mediaMetadata.putString("content-type", movieVideoItem.getContent().getHighRes().getType()); 

       MediaTrack englishSubtitle = new MediaTrack.Builder(1 /* ID */, MediaTrack.TYPE_TEXT) 
         .setName("English Subtitle") 
         .setSubtype(MediaTrack.SUBTYPE_CAPTIONS) 
         .setContentId(closedCaptionsUrl) 
         /* language is required for subtitle type but optional otherwise */ 
         .setLanguage("en-US") 
         .build(); 

       List tracks = new ArrayList(); 
       tracks.add(englishSubtitle); 

       MediaInfo mediaInfo = new MediaInfo.Builder(url) 
         .setStreamDuration(movieVideoItem.getDuration()) 
         .setStreamType(MediaInfo.STREAM_TYPE_NONE) 
         .setContentType(type) 
         .setMetadata(mediaMetadata) 
         .setMediaTracks(tracks) 
         .setCustomData(customData) 
         .build(); 
+0

你不使用VideoCastControllerActivity? –

+0

不,我有自定義活動,我正在播放視頻,也可以將視頻流式傳輸到Chromecast。我使用示例項目中的ChromeCastUtility。 – Sandak

+0

@AliNaddaf我明白了,你是對的!我正在使用VideoCastControllerActivity!對不起,誤導我的錯誤。 – Sandak

回答

0

你需要做到以下幾點:

  1. 確保您的MediaInfo項目具有跟蹤信息。

  2. 確保軌道在設置中啓用,而在配置CastVideoManager啓用軌道支持。

  3. 在你的,比方說,活動註冊一個OnTracksSelectedListener偵聽器,以便當軌道改變,你的活動可以得到通知。

4.爲你的活動添加一個按鈕,點擊按鈕後,調用如下的方法。

private void showTracksChooserDialog() 
     throws TransientNetworkDisconnectionException, NoConnectionException { 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    Fragment prev = getSupportFragmentManager().findFragmentByTag(DIALOG_TAG); 
    if (prev != null) { 
     transaction.remove(prev); 
    } 
    transaction.addToBackStack(null); 

    // Create and show the dialog. 
    TracksChooserDialog dialogFragment = TracksChooserDialog 
      .newInstance(mCastManager.getRemoteMediaInformation()); 
    dialogFragment.show(transaction, DIALOG_TAG); 
} 

這將打開一個(片段)對話框,顯示當前的文本和音軌,並允許用戶選擇一個。當在該對話框中選擇一個並按下確定時,將調用您在上一步中註冊的偵聽器,然後您可以在偵聽器中啓用該音軌。

  • 確保當你離開你的活動,你刪除偵聽。
  • +0

    謝謝你的回答。但我有Android的自定義播放器,我正在使用使用VideoCastControllerActivity的VideoCastManager。我不知道如何添加CC按鈕並在其上設置OnClickListener,即使我可以看到,cast_activity.xml佈局中有CC按鈕,但它不可見,我不知道如何啓用它。感謝您的時間 – Sandak

    +0

    我編輯了我的問題,提供更多信息。您是對的,我正在使用'VideoCastControllerActivity' – Sandak

    +0

    如果您正在使用CastControllerActivity,並且在初始化VideoCastManager時啓用了隱藏式字幕,並且您的媒體中配置了音頻或文本軌道,則該按鈕會顯示出來,檢查CastVideos-android應用程序 –