Coder Social home page Coder Social logo

averyzhong / subtitleforandroid Goto Github PK

View Code? Open in Web Editor NEW
112.0 6.0 32.0 2.48 MB

Subtitle For Android is a multi-subtitle support library for Android platform video playback. It supports almost all Android versions, and supports subtitle files in. SRT,. SCC,. ASS,. STL,. TTML formats.

Java 100.00%
subtitle subtitle-for-android android-multiple-subtitle

subtitleforandroid's Introduction

CSDN blog: https://blog.csdn.net/han_han_1/article/details/86747472

Download

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

概述

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

下载

implementation 'com.avery:subtitle:1.0.6' // 最新版本号请看上面"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中加入<uses-sdk tools:overrideLibrary="com.avery.subtitle"/>

怎样使用?

  1. 在播放器布局文件中添加SimpleSubtitleView
<com.avery.subtitle.widget.SimpleSubtitleView
        android:id="@+id/subtitle_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:text="字幕将在这里显示"
        android:textColor="#ffffff"
        android:textSize="26sp"
        android:textStyle="bold"/>

  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<Subtitle> 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

Copyright 2019 AveryZhong

Licensed 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.0

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations under the License.

subtitleforandroid's People

Contributors

averyzhong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

subtitleforandroid's Issues

解析ass未进行空判断,解析srt未进行格式解析

谢谢博主分享,使用项目很棒,但也发现了两个问题。
1.FormatASS中,85行开始,分割后直接取第二个字符串,会出错(如"Original Script:"后为空的情况),建议进行空判断
2.srt解析时,srt如果带有格式如:
00:00:00,000 --> 00:00:07,970(备注:这是时间轴){\3c&Hd22c01&}{\fn黑体\fs27} (备注:这是改变字体、颜色、大小的参数)字幕
将会把字体格式打印,可以加入正则过滤或者进行解析。
以上如有错误就忽略哟~~~

utf8 srt files

当SRT文件以UTF8编码存储的情况下,Format-SRT的解析过程会发生故障。
具体故障来自于UTF8编码的文件的BOM字符,导致字串到整数的解析失败。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.