MediaPlayer 在暂停或 seekTo 后不会继续显示字幕 (.SRT)
Posted
技术标签:
【中文标题】MediaPlayer 在暂停或 seekTo 后不会继续显示字幕 (.SRT)【英文标题】:MediaPlayer doesn't continue showing subtitle (.SRT) after pause or seekTo 【发布时间】:2016-08-03 21:36:49 【问题描述】:我正在尝试为 videoview 实现字幕。我已经使用了这个示例项目:
package com.example.media.timedtexttest;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnTimedTextListener;
import android.media.MediaPlayer.TrackInfo;
import android.media.TimedText;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity implements OnTimedTextListener
private static final String TAG = "TimedTextTest";
private TextView txtDisplay;
private static Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
MediaPlayer player = MediaPlayer.create(this, R.raw.video);
try
player.addTimedTextSource(getSubtitleFile(R.raw.sub),
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
int textTrackIndex = findTrackIndexFor(
TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT, player.getTrackInfo());
if (textTrackIndex >= 0)
player.selectTrack(textTrackIndex);
else
Log.w(TAG, "Cannot find text track!");
player.setOnTimedTextListener(this);
player.start();
catch (Exception e)
e.printStackTrace();
private int findTrackIndexFor(int mediaTrackType, TrackInfo[] trackInfo)
int index = -1;
for (int i = 0; i < trackInfo.length; i++)
if (trackInfo[i].getTrackType() == mediaTrackType)
return i;
return index;
private String getSubtitleFile(int resId)
String fileName = getResources().getResourceEntryName(resId);
File subtitleFile = getFileStreamPath(fileName);
if (subtitleFile.exists())
Log.d(TAG, "Subtitle already exists");
return subtitleFile.getAbsolutePath();
Log.d(TAG, "Subtitle does not exists, copy it from res/raw");
// Copy the file from the res/raw folder to your app folder on the
// device
InputStream inputStream = null;
OutputStream outputStream = null;
try
inputStream = getResources().openRawResource(resId);
outputStream = new FileOutputStream(subtitleFile, false);
copyFile(inputStream, outputStream);
return subtitleFile.getAbsolutePath();
catch (Exception e)
e.printStackTrace();
finally
closeStreams(inputStream, outputStream);
return "";
private void copyFile(InputStream inputStream, OutputStream outputStream)
throws IOException
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int length = -1;
while ((length = inputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, length);
// A handy method I use to close all the streams
private void closeStreams(Closeable... closeables)
if (closeables != null)
for (Closeable stream : closeables)
if (stream != null)
try
stream.close();
catch (IOException e)
e.printStackTrace();
@Override
public void onTimedText(final MediaPlayer mp, final TimedText text)
if (text != null)
handler.post(new Runnable()
@Override
public void run()
int seconds = mp.getCurrentPosition() / 1000;
txtDisplay.setText("[" + secondsToDuration(seconds) + "] "
+ text.getText());
);
// To display the seconds in the duration format 00:00:00
public String secondsToDuration(int seconds)
return String.format("%02d:%02d:%02d", seconds / 3600,
(seconds % 3600) / 60, (seconds % 60), Locale.US);
并创建了完全相同的实现并且它以某种方式工作,但是如果我停止视频并继续字幕不会继续,或者如果我寻找视频一段时间字幕也不会继续。
我尝试过的:
每次player.start()
之前我都会设置player.selectTrack(textTrackIndex);
我在做player.start();
player.setOnTimedTextListener(this);
时尝试重新注册监听器
请帮助我在字幕功能上花了 4 天...如果你有一些项目示例或 sn-p 会很好:)
【问题讨论】:
MilanNz - 您找到解决方案了吗?迪克 【参考方案1】:我们有同样的问题,我们必须实现OnSeekCompleteListener
,即使你什么都不做覆盖方法
这是我们的代码:
@Override
public void onSeekComplete(MediaPlayer mediaPlayer)
它的工作原理!!!
希望对你有帮助
【讨论】:
@JosefVancura 你有相同的版本吗?因为这个问题很老,所以最新版本的修复可能不再适用。我不记得使用的版本,但如果您遇到相同的问题,可能还有另一个修复程序。祝你好运。 使用 ExoPlayer 代替 MediaPlayer。是谷歌提供的免费库,非常不错。以上是关于MediaPlayer 在暂停或 seekTo 后不会继续显示字幕 (.SRT)的主要内容,如果未能解决你的问题,请参考以下文章
JavaFX 媒体 - 暂停();方法使 MediaPlayer 快进?