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

前端 未结 6 861
眼角桃花
眼角桃花 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

     PopupMenu popup = new PopupMenu(getContext(), v);
                    popup.getMenuInflater().inflate(R.menu.subtile_menu, popup.getMenu());
                    popup.setOnMenuItemClickListener(item -> {
                        if (item.getItemId()==R.id.remove_subtitle){
                            //player.prepare(videoSource);
                            player.prepare(videoSource, false, false);
                        }else if (item.getItemId()==R.id.english){
                            if (caption){
                                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(Uri.parse(sub_url), textFormat, C.TIME_UNSET);
                                MergingMediaSource mediaSource = new MergingMediaSource(videoSource, textMediaSource);
                                player.prepare(mediaSource, false, false);
                            }else {
                                Toast.makeText(getContext(), "Subtitle not available", Toast.LENGTH_SHORT).show();
                            }
                        }
                        return true;
                    });
                    popup.show();
    

提交回复
热议问题