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

时间:2015-07-17 21:00:47

标签: android media-player subtitle

我正在尝试显示.srt文件中的副标题。 (适用于Android 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_width="match_parent"
android:layout_height="match_parent" >

<SurfaceView
    android:id="@+id/playersurface"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

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

未调用OnTimedTextListener。我的目标SDK是22,我的设备高于Android 4.1

我错过了什么吗?

1 个答案:

答案 0 :(得分: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();
}

}