MediaPlayer 的字幕功能不起作用(SRT 文件)

Posted

技术标签:

【中文标题】MediaPlayer 的字幕功能不起作用(SRT 文件)【英文标题】:MediaPlayer's subtitle feature is not working (SRT file) 【发布时间】:2015-07-17 21:00:47 【问题描述】:

我正在尝试显示 .srt 文件中的字幕。 (适用于安卓 4.1+) mp4文件和srt文件都存在于设备中。

public class MainActivity extends Activity implements SurfaceHolder.Callback, OnPreparedListener

MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
SurfaceView playerSurfaceView;
TextView tv_subtitle;
String videoSrc = Environment.getExternalStorageDirectory().getPath() + "/video.mp4";
String subTitleSrc = Environment.getExternalStorageDirectory().getPath() + "/sub.srt";

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    playerSurfaceView = (SurfaceView)findViewById(R.id.playersurface);
    tv_subtitle = (TextView)findViewById(R.id.tv_subtitle);
    surfaceHolder = playerSurfaceView.getHolder();
    surfaceHolder.addCallback(this);


@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 



@Override
public void surfaceCreated(SurfaceHolder arg0) 

    try 
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder);
        mediaPlayer.setDataSource(videoSrc);
        mediaPlayer.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() 
            @Override
            public void onTimedText(final MediaPlayer mediaPlayer, final TimedText timedText) 
                if (timedText != null) 
                    Log.d("test", "subtitle: " + timedText.getText());
                
            
        );

        mediaPlayer.addTimedTextSource(subTitleSrc, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
        mediaPlayer.prepare();
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
     catch (Exception e) 
    


@Override
public void surfaceDestroyed(SurfaceHolder arg0) 


@Override
public void onPrepared(MediaPlayer mp) 
    mediaPlayer.start();

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >

<SurfaceView
    android:id="@+id/playersurface"
    android:layout_
    android:layout_/>

<TextView
    android:layout_gravity="center"
    android:id="@+id/tv_subtitle"
    android:textColor="@android:color/holo_red_dark"
    android:textSize="20sp"
    android:layout_
    android:layout_ />

没有调用 OnTimedTextListener。我的 Target SDK 是 22,我的设备是 Android 4.1 以上

我错过了什么吗?

【问题讨论】:

同样的问题 E/Vitamio[4.2.1][Player]: THIS SUBTITLE NO DECODER 0x53526970 E/Vitamio[4.2.1][Player]: NO SUBTITLE DATA FOUND 【参考方案1】:

我找到了答案。 信用:Looking for a working example of addTimedTextSource for adding subtitle to a video from an .srt file in Android 4.1

我的代码中缺少 mediaPlayer.selectTrack() 方法。

这是完整的代码:

public class MainActivity extends Activity implements SurfaceHolder.Callback, OnPreparedListener

MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
SurfaceView playerSurfaceView;
TextView tv_subtitle;
String videoSrc = Environment.getExternalStorageDirectory().getPath() + "/video.mp4";
String subTitleSrc = Environment.getExternalStorageDirectory().getPath() + "/sub.srt";

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    playerSurfaceView = (SurfaceView)findViewById(R.id.playersurface);
    tv_subtitle = (TextView)findViewById(R.id.tv_subtitle);
    surfaceHolder = playerSurfaceView.getHolder();
    surfaceHolder.addCallback(this);


@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 




@Override
public void surfaceCreated(SurfaceHolder arg0) 

    try 
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder);
        mediaPlayer.setDataSource(videoSrc);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.prepare();

        mediaPlayer.addTimedTextSource(subTitleSrc, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
        int textTrackIndex = findTrackIndexFor(
                MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT, mediaPlayer.getTrackInfo());
        if (textTrackIndex >= 0) 
            mediaPlayer.selectTrack(textTrackIndex);
         else 
            Log.w("test", "Cannot find text track!");
        

        mediaPlayer.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() 
            @Override
            public void onTimedText(final MediaPlayer mediaPlayer, final TimedText timedText) 
                if (timedText != null) 
                    Log.d("test", "subtitle: " + timedText.getText());
                
            
        );

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
     catch (Exception e) 
    


private int findTrackIndexFor(int mediaTrackType, MediaPlayer.TrackInfo[] trackInfo) 
    int index = -1;
    for (int i = 0; i < trackInfo.length; i++) 
        if (trackInfo[i].getTrackType() == mediaTrackType) 
            return i;
        
    
    return index;


@Override
public void surfaceDestroyed(SurfaceHolder arg0) 


@Override
public void onPrepared(MediaPlayer mp) 
    mediaPlayer.start();

【讨论】:

以上是关于MediaPlayer 的字幕功能不起作用(SRT 文件)的主要内容,如果未能解决你的问题,请参考以下文章

参考或链接在 Flowplayer 字幕插件中不起作用

方法不起作用

Win 10 应用开发加载外部的 srt 字幕

SRT字幕格式

vvt在线转换srt

SRT 字幕的最大尺寸