2017-02-20 219 views
1

我正在一个项目中,我应该在android中播放.srt文件以及视频。我正在研究Exoplayer的样本,但无法使用视频播放.srt文件。如何在Exoplayer android中添加字幕(.SRT文件)到视频中?

我使用的代码,

MediaSource mediaSource = new HlsMediaSource(Uri.parse("https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"), 
       mediaDataSourceFactory, mainHandler, null); 

     Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP, 
       null, Format.NO_VALUE, Format.NO_VALUE, "en", null); 

     Uri uri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt"); 


     MediaSource subtitleSource = new SingleSampleMediaSource(uri, mediaDataSourceFactory, textFormat, C.TIME_UNSET); 
// Plays the video with the sideloaded subtitle. 
     MergingMediaSource mergedSource = 
       new MergingMediaSource(mediaSource, subtitleSource); 

     player.prepare(mergedSource); 

任何人都可以请建议我要这个或相同的任何教程链接的解决方案。非常感激你的帮助 !

+0

您需要使用SubtitleView与TextRenderer设置它正确(希望您使用ExoPlayer 2) –

+0

@dari是dario,我使用的是Exoplayer 2.我的示例也有Subtitleview和TextRenderer。我仍然面临着问题。你可以请建议我一个这个教程? 代码为我的样品中的字幕视图, subtitleLayout =(SubtitleView)findViewById(com.google.android.exoplayer2.R.id.subtitles); subtitleLayout.setUserDefaultStyle(); subtitleLayout.setUserDefaultTextSize(); –

回答

1

我只是编辑在那里玩家活动exoplayer例如诸如此类......它的工作对我来说...

private void initializePlayer() { 
     Intent intent = getIntent(); 
     if (player == null) { 
      boolean preferExtensionDecoders = intent.getBooleanExtra(PREFER_EXTENSION_DECODERS, false); 
      UUID drmSchemeUuid = intent.hasExtra(DRM_SCHEME_UUID_EXTRA) 
        ? UUID.fromString(intent.getStringExtra(DRM_SCHEME_UUID_EXTRA)) : null; 
      DrmSessionManager<FrameworkMediaCrypto> drmSessionManager = null; 
      if (drmSchemeUuid != null) { 
       String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL); 
       String[] keyRequestPropertiesArray = intent.getStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES); 
       Map<String, String> keyRequestProperties; 
       if (keyRequestPropertiesArray == null || keyRequestPropertiesArray.length < 2) { 
        keyRequestProperties = null; 
       } else { 
        keyRequestProperties = new HashMap<>(); 
        for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) { 
         keyRequestProperties.put(keyRequestPropertiesArray[i], 
           keyRequestPropertiesArray[i + 1]); 
        } 
       } 
       try { 
        drmSessionManager = buildDrmSessionManager(drmSchemeUuid, drmLicenseUrl, 
          keyRequestProperties); 
       } catch (UnsupportedDrmException e) { 
        int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported 
          : (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME 
          ? R.string.error_drm_unsupported_scheme : R.string.error_drm_unknown); 
        showToast(errorStringId); 
        return; 
       } 
      } 

      @SimpleExoPlayer.ExtensionRendererMode int extensionRendererMode = 
        ((AppController) getApplication()).useExtensionRenderers() 
          ? (preferExtensionDecoders ? SimpleExoPlayer.EXTENSION_RENDERER_MODE_PREFER 
          : SimpleExoPlayer.EXTENSION_RENDERER_MODE_ON) 
          : SimpleExoPlayer.EXTENSION_RENDERER_MODE_OFF; 
      TrackSelection.Factory videoTrackSelectionFactory = 
        new AdaptiveVideoTrackSelection.Factory(BANDWIDTH_METER); 
      trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); 
      trackSelectionHelper = new TrackSelectionHelper(trackSelector, videoTrackSelectionFactory); 
      player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, new DefaultLoadControl(), 
        drmSessionManager, extensionRendererMode); 
      player.addListener(this); 

      eventLogger = new EventLogger(trackSelector); 
      player.addListener(eventLogger); 
      player.setAudioDebugListener(eventLogger); 
      player.setVideoDebugListener(eventLogger); 
      player.setMetadataOutput(eventLogger); 

      simpleExoPlayerView.setPlayer(player); 
      player.setPlayWhenReady(shouldAutoPlay); 
      debugViewHelper = new DebugTextViewHelper(player, debugTextView); 
      debugViewHelper.start(); 
      playerNeedsSource = true; 


     } 
     if (playerNeedsSource) { 
      String action = intent.getAction(); 
      Log.d("URL action: ", action); 
      Uri[] uris; 
      String[] extensions; 
      if (ACTION_VIEW.equals(action)) { 
       uris = new Uri[] {intent.getData()}; 
       extensions = new String[] {intent.getStringExtra(EXTENSION_EXTRA)}; 
      } else if (ACTION_VIEW_LIST.equals(action)) { 
       String[] uriStrings = intent.getStringArrayExtra(URI_LIST_EXTRA); 
       uris = new Uri[uriStrings.length]; 
       for (int i = 0; i < uriStrings.length; i++) { 
        uris[i] = Uri.parse(uriStrings[i]); 
        Log.d("URL action2: ", String.valueOf(uris[i])+" "); 
       } 
       extensions = intent.getStringArrayExtra(EXTENSION_LIST_EXTRA); 
       if (extensions == null) { 
        extensions = new String[uriStrings.length]; 
       } 
      } else { 
       showToast(getString(R.string.unexpected_intent_action, action)); 
       return; 
      } 
      if (Util.maybeRequestReadExternalStoragePermission(this, uris)) { 
       // The player will be reinitialized if the permission is granted. 
       return; 
      } 
      MediaSource[] mediaSources = new MediaSource[uris.length]; 
      for (int i = 0; i < uris.length; i++) { 
       mediaSources[i] = buildMediaSource(uris[i], extensions[i]); 
       Log.d("URL action extensions: ", String.valueOf(extensions[i])); 
      } 
      MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0] 
        : new ConcatenatingMediaSource(mediaSources); 
      boolean haveResumePosition = resumeWindow != C.INDEX_UNSET; 
      if (haveResumePosition) { 
       player.seekTo(resumeWindow, resumePosition); 
      } 
      // edit for subtitle 
      // player.prepare(mediaSource, !haveResumePosition, false); 
     // playerNeedsSource = false; 
      // player.seekTo(0); 
      //updateButtonVisibilities(); 

      /* MediaSource mediaSource = new HlsMediaSource(Uri.parse("https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"), 
        mediaDataSourceFactory, mainHandler, null);*/ 

      Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP, 
        null, Format.NO_VALUE, Format.NO_VALUE, "en", null); 

      Uri uri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt"); 


      MediaSource subtitleSource = new SingleSampleMediaSource(uri, mediaDataSourceFactory, textFormat, C.TIME_UNSET); 
// Plays the video with the sideloaded subtitle. 
      MergingMediaSource mergedSource = 
        new MergingMediaSource(mediaSource, subtitleSource); 

      player.prepare(mergedSource,!haveResumePosition, false); 
      playerNeedsSource = false; 
      //player.seekTo(0); 
     } 


    } 
0
从我创建呈现为一个界面中的文本监听器合并后的媒体源

除了作为演示

private final class ComponentListener implements TextRenderer.Output{ 

     @Override 
     public void onCues(List<Cue> cues) { 
      if (subtitleView != null) { 
       subtitleView.onCues(cues); 
      } 
     } 
    } 

player.setTextOutput(componentListener); 

我创建了一个对象,并设置TextOutput因为这

相关问题