gpt4 book ai didi

android - MediaPlayer 的字幕功能不工作(SRT 文件)

转载 作者:行者123 更新时间:2023-11-30 02:01:00 28 4
gpt4 key购买 nike

我正在尝试显示 .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_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();
}

关于android - MediaPlayer 的字幕功能不工作(SRT 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com