Subtitle For Android 是一个Android平台视频播放多字幕支持库,几乎支持所有的Android版本,可以在需要外挂字幕中的项目集成。支持的字幕格式有:.SRT、.SCC、.ASS、.STL、.TTML格式的字幕文件。集成方式简单,可几行代码就可以使你的播放器支持外挂做字幕的支持。

项目地址:https://github.com/averyzhong/SubtitleForAndroid

Android外挂字幕组件库(Subtitle For Android)

概述

Subtitle For Android 是一个Android平台视频播放多字幕支持库,几乎支持所有的Android版本,可以在需要外挂字幕中的项目集成。支持的字幕格式有:.SRT、.SCC、.ASS、.STL、.TTML格式的字幕文件。集成方式简单,可几行代码就可以使你的播放器支持外挂做字幕的支持

下载

implementation 'com.avery:subtitle:x.x.x' // 最新版本号请看上面"Download"气泡后面的数字

如果Gradle同步出现如下错误: Manifest merger failed : uses-sdk:minSdkVersion xx cannot be smaller than version xx declared in library [com.avery:subtitle:x.x.x]

请在AndroidManifest.xml中加入

怎样使用?

  1. 在播放器布局文件中添加SimpleSubtitleView
  1. 绑定MediaPlayerSimpleSubtitleView
  private SimpleSubtitleView mSubtitleView;   ....省略无关代码.....  mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {             @Override             public void onPrepared(final MediaPlayer mp) {                 // 绑定MediaPlayer                 mSubtitleView.bindToMediaPlayer(mp);                 // 设置字幕                 mSubtitleView.setSubtitlePath(SUBTITLE_URL);             }         });   mVideoView.setVideoURI(Uri.parse(VIDEO_URL));    ....省略无关代码.....   @Override   protected void onDestroy() {         mSubtitleView.destroy(); // 记得销毁         super.onDestroy();   }

SimpleSubtitleView还有其他与Activity生命周期相似的方法:start()pause()resume()stop()reset()可以根据具体集成情况在适当的地方进行调用。

字幕样式设置

SimpleSubtitleView集成自TextView,所以TextView的所有样式设置都适用于SimpleSubtitleView,如设置字幕颜色、字幕大小、字幕对其方式等。

注意!!!

  1. 最好在MediaPlayer初始化完成后才能调用SimpleSubtitleView.setSubtitlePath()方法,最好的时机是在MediaPlayer的onPrepared回调方法里调用SimpleSubtitleView.setSubtitlePath()
  2. 最好在MediaPlayer销毁之前先销毁SimpleSubtitleView,即调用SimpleSubtitleView.destroy(),最好的时机是在调用MediaPlayer.release()方法前先调用调用SimpleSubtitleView.destroy()

自定义字幕显示控件

如果不想使用提供的SimpleSubtitleView控件,你还可以轻松自定义你自己的显示控件,只需通过 DefaultSubtitleEngine来辅助就能办到

.... private SubtitleEngine mSubtitleEngine = new DefaultSubtitleEngine(); mSubtitleEngine.setOnSubtitlePreparedListener(new OnSubtitlePreparedListener() {        @Override        public void onSubtitlePrepared(@Nullable final List subtitles) {                // 启动字幕刷新任务                mSubtitleEngine.start();           }        }); mSubtitleEngine.setOnSubtitleChangeListener(new OnSubtitleChangeListener() {        @Override         public void onSubtitleChanged(@Nullable final Subtitle subtitle) {                // 拿到Subtitle对象来刷新你自定义过的字幕显示控件,注意subtitle可能为空                // 当subtitle为空时,你应该清除自定义控件已显示的字幕显示                .......           }        });....

自定义的最后一步就是通过DefaultSubtitleEngine的生命周期相应方法:start()pause()resume()stop()reset()处理好控件的生命周期,以免导致bug。

快照

License

ll source code is licensed under Apache License 2.0. If you create your own app (free or commercial) that uses some or all of the code from this codebase, you MUST attribute it to the original author, no exceptions. You can do so by displaying an "About" or "Licenses" screen in your app that links back to this GitHub page AND includes the following notice:

Copyright 2019 - Avery ZhongLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at   http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

 

更多相关文章

  1. Android控件抖动效果
  2. Android瀑布流控件——AndroidStaggeredGrid
  3. 在Android当中常用的控件的详解和分析
  4. iOS和android解决键盘弹出遮挡控件的处理比较
  5. Android中ViewGroup等容器控件的使用
  6. 使用shape来定义控件的一些显示属性
  7. android listView控件用法
  8. android基础控件(4)GridView实现网格视图
  9. Android 高仿微信头像截取 打造不一样的自定义控件

随机推荐

  1. android WebView结合javascript相互调用
  2. [Android]获取网络连接状态
  3. 像素鸟(flapybird)的简单实现
  4. Android(安卓)可選文件格式瀏覽器
  5. Android(Java):长时间未登录提醒
  6. android截屏简单引用
  7. Android(安卓)之 SeekBar用法介绍
  8. Android(安卓)获取设备宽高分辨率
  9. android(6)(读数据的一些权限)
  10. android监控程序状态(安装 卸载)