简体   繁体   中英

use addSubtitleSource with videoview

I'm trying to add subtitle to videoview and the method of getSubtitleSource is like this:

private InputStream getSubtitleSource(String filepath) {
        InputStream ins = null;
        String ccFileName = filepath.substring(0,filepath.lastIndexOf('.'));
        File file = new File(ccFileName);
        if (file.exists() == false)
        {
            Log.e(TAG,"no close caption file " + ccFileName);
            return null;
        }
        FileInputStream fins = null;
        try {
            fins = new FileInputStream(file);
        }catch (Exception e) {
            Log.e(TAG,"exception " + e);
        }
        ins = (InputStream)fins;
        return ins;
    }

and here how I call the method and use srt file with it:

videoView.seekTo(position);
String srt = "http://shasha/share/2018/09/07/Lucha_Mexico_.srt";
videoView.addSubtitleSource(getSubtitleSource(srt),MediaFormat.createSubtitleFormat("text/srt",Locale.ENGLISH.getLanguage())); 
videoView.start();

there is a problem with addSubtitleSource and createSubtitleFormat says:

Call requires API level 19 (current min is 16): android.widget.VideoView#addSubtitleSource

so I just surrounded it with sdk version like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
videoView.seekTo(position);
String srt = "http://shasha/share/2018/09/07/Lucha_Mexico_.srt";
videoView.addSubtitleSource(getSubtitleSource(srt),MediaFormat.createSubtitleFormat("text/srt",Locale.ENGLISH.getLanguage()));  
videoView.start();
 }

but when I play the video there is no subtitle appear so what's wrong with it

Checking with Build.VERSION just escaping your app from crashing.Therefore if you are using a device lower than api level 19, the app will not crash but the getSubTitleSource will not work.

Just edit your minSdkVersion in app.gradle with api level 19.

  minSdkVersion 19

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM