How to add Subtitles(.SRT files) to video in Exoplayer android?

前端 未结 6 723
眼角桃花
眼角桃花 2020-12-28 10:03

i am working on a project where i should play .srt files along with video in android. I was working through the samples of Exoplayer but cant able to play .srt files with vi

6条回答
  •  隐瞒了意图╮
    2020-12-28 10:39

    I have given a lots of efforts for this and finally concluded to the solution...
    Sharing here for the reference

    Srt file format ref link - http://www.storiesinflight.com/js_videosub/jellies.srt

    Solution which finds a way for me - https://github.com/google/ExoPlayer/issues/3869#issuecomment-367067013

    Adding Exoplayer code here

    // initialize exoplayer
    private void initializeExoPlayer() {
        if (player == null) {
            video_view = (PlayerView) findViewById(R.id.video_view);
    
            // 1. Create a default TrackSelector
            LoadControl loadControl = new DefaultLoadControl(
                    new DefaultAllocator(true, 16),
                    VideoPlayerConfig.MIN_BUFFER_DURATION,
                    VideoPlayerConfig.MAX_BUFFER_DURATION,
                    VideoPlayerConfig.MIN_PLAYBACK_START_BUFFER,
                    VideoPlayerConfig.MIN_PLAYBACK_RESUME_BUFFER, -1, true);
    
            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
            TrackSelection.Factory videoTrackSelectionFactory =
                    new AdaptiveTrackSelection.Factory(bandwidthMeter);
            TrackSelector trackSelector =
                    new DefaultTrackSelector(videoTrackSelectionFactory);
            // 2. Create the player
            player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(mContext), trackSelector, loadControl);
            video_view.setPlayer(player);
    
        }
    }
    


    public void buildMediaSource(String videoUrl, final String questionVideoThumbnail) {
    
    
        Uri mUri = Uri.parse(videoUrl);
        Uri srtUri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt");
    
        // Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
                Util.getUserAgent(mContext, getString(R.string.app_name)), bandwidthMeter);
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(mUri);
        // Prepare the player with the source.
    
        Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
                null, Format.NO_VALUE, Format.NO_VALUE, "en", null, Format.OFFSET_SAMPLE_RELATIVE);
        MediaSource textMediaSource = new SingleSampleMediaSource.Factory(dataSourceFactory)
                .createMediaSource(srtUri, textFormat, C.TIME_UNSET);
        MergingMediaSource mediaSource = new MergingMediaSource(videoSource, textMediaSource);
    
        player.prepare(mediaSource);
        player.setPlayWhenReady(true);
        player.addListener(this);
    
    
    }
    

提交回复
热议问题