我能够让它工作,因为它仍然是一个悬而未决的问题,我将在此处包含完整的解决方案。
虽然更改文件扩展名以防止压缩的想法很好,但我更喜欢将srt
文件从资源复制到设备上的应用程序本地目录,但无论如何为了完整起见,这里是一个扩展列表,不会t被压缩。
“.jpg”、“.jpeg”、“.png”、“.gif”、“.wav”、“.mp2”、“.mp3”、“.ogg”、“.aac”、“.mpg”、 “.mpeg”、“.mid”、“.midi”、“.smf”、“.jet”、“.rtttl”、“.imy”、“.xmf”、“.mp4”、“.m4a”、 “.m4v”、“.3gp”、“.3gpp”、“.3g2”、“.3gpp2”、“.amr”、“.awb”、“.wma”、“.wmv”
解决步骤很简单:
创建一个MediaPlayer
实例并通过调用MediaPlayer.create()
或player.setDataSource()
然后准备它player.prepare()
如果android设备上不存在字幕文件,将其从资源文件夹复制到设备
player.addTimedTextSource()
使用第一个参数 a调用,String
其中包含设备上字幕文件的完整路径,并MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP
作为第二个参数调用
通过调用选择TimedText
曲目player.selectTrack()
并通过the index of timedTextType
搜索TrackInfo[]
返回来传递player.getTrackInfo()
(我通常会找到它2
)
设置监听器,player.setOnTimedTextListener()
然后开始播放媒体文件player.start()
这是完整的课程:
要运行这个确切的类,您将需要文件res/raw
夹下的两个文件sub.srt
和video.mp4
(或任何扩展名)。然后TextView
用 id定义 a txtDisplay
。最后你的项目/设备/模拟器必须支持API 16
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);
}
}
这是subtitle
我用作示例的文件:
1
00:00:00,220 --> 00:00:01,215
First Text Example
2
00:00:03,148 --> 00:00:05,053
Second Text Example
3
00:00:08,004 --> 00:00:09,884
Third Text Example
4
00:00:11,300 --> 00:00:12,900
Fourth Text Example
5
00:00:15,500 --> 00:00:16,700
Fifth Text Example
6
00:00:18,434 --> 00:00:20,434
Sixth Text Example
7
00:00:22,600 --> 00:00:23,700
Last Text Example
以下是测试应用程序的一些屏幕截图,显示TextView
随着媒体文件的进展,它正在自动更改(即从字幕文件中读取)
编辑:
这是示例项目的代码