MediaPlayer 的字幕功能不工作(SRT 文件)
MediaPlayer's subtitle feature is not working (SRT file)
我正在尝试显示 .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。我的 Target SDK 是 22,我的设备是 Android 4.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();
}
}
我正在尝试显示 .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。我的 Target SDK 是 22,我的设备是 Android 4.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();
}
}