寻找addTimedTextSource的工作示例用于从.srt文件,在安卓4.1添加字幕到视频 [英] Looking for a working example of addTimedTextSource for adding subtitle to a video from an .srt file in Android 4.1

查看:1428
本文介绍了寻找addTimedTextSource的工作示例用于从.srt文件,在安卓4.1添加字幕到视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用.srt文件,用于定时文本源(仅适用于Android的可4.1+ <一href="http://developer.android.com/about/versions/android-4.1.html#Multimedia">http://developer.android.com/about/versions/android-4.1.html#Multimedia) 。第一个问题,是因为有获得一个文件描述符的.srt文件,(在资产的文件夹,怎么回事,你会在你的应用程序捆绑在一起呢?)。该文件中获取COM $ P $自动pssed所以你不会甚至能够看到文件,而不改变编译设置或做一个自定义生成,最简单的解决办法是重新命名的.srt文件,以JPG格式,这样它不会COM pressed和openFD方法仍然有效。现在的我加入TimedTextSource有:

I have been trying to use an .srt file for a timed text source (Only available in android 4.1+ http://developer.android.com/about/versions/android-4.1.html#Multimedia) . The first problem has to do with getting a file descriptor for the .srt file (in the assets folder, how else would you bundle it in your app?). The file gets compressed automatically so you won't be able to even see the file without changing compile settings or doing a custom build. The easiest solution was to rename the .srt file to .jpg so that it does not get compressed and the openFD method still works. I am now adding the TimedTextSource with:

_myMP.addTimedTextSource(getAssets().openFd("captions.jpg").getFileDescriptor(),   MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);

现在该文件加载正确使用myMP.getTrackInfo()来获取曲目列表,可以看到,添加定时文本源后,第6赛道的类型是3,这是定时文本轨道类型。我已经使用selectTrack选择这条赛道的谷歌文档中表示,但这样做后无字幕不断出现,在我的TimedTextListener:

Now the file loads correctly and using myMP.getTrackInfo() to get a list of tracks, can see that after adding the timed text source, the 6th track has type "3" which is timed text track type. I have used selectTrack to choose this track as said in the google documentation but after doing so no captions ever appear and on my TimedTextListener:

 _myMP.setOnTimedTextListener(new OnTimedTextListener(){
        @Override
        public void onTimedText(MediaPlayer mp, TimedText text) {
                if (text!=null)
                   Log.d("TimedText", text.getText());  
            }       
        });

火灾只有一次(我有这样的文件在20定时文本事件),但是文本参数总是空。我已经做了搜索,也找不到使用timeText的一个工作code为例,它不会出现在任何示例项目,从字面上有没有文件不是从谷歌的API文档等,但据我所知,没有人发布了它的工作的例子呢。我在谷歌的Nexus更新到Android 4.2测试这种

Fires only once (I have like 20 timed text events in the file) but the text parameter is always null. I have done searches and cannot find a single working code example of using timeText and it does not appear in any sample projects, there is literally no documentation other than the api docs from google but as far as I can tell, NO one has posted a working example of it yet. I am testing this on a google Nexus updated to Android 4.2

推荐答案

我能得到这个工作,因为它仍然是一个悬而未决的问题,我会在这里包含了完整的解决方案。

I was able to get this to work and since it is still an open question I will include the complete solution here.

虽然改变文件扩展名,以prevent的COM pression是不错的主意,但我preFER到 SRT 文件的复制的资源的设备,但反正为了这里的完整性上的应用程序的本地目录的扩展,也不会玉米pressed

Although the idea of changing the file extension to prevent the compression is nice, but I prefer to copy the srt file from the resources to the app local directory on the device, but anyways for the sake of completeness here is a list of extensions that won't be compressed.

。JPG,.JPEG,巴纽,名为.gif,.WAV,.mp2,MP3,.OGG,.AAC   .MPG,文件.mpeg,。中旬,.midi,.smf,.jet,.rtttl,.imy,.xmf,的MP4, .M4A,的.m4v,名为.3gp,.3gpp,.3g2,.3gpp2,AMR,.awb,WMA,.WMV

".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"

解决方法步骤很简单:

  1. 通过创建一个的MediaPlayer 实例和prepare,要么调用 MediaPlayer.create() player.setDataSource()然后播放器。prepare()

  1. Create a MediaPlayer instance and prepare it by either calling MediaPlayer.create() or player.setDataSource() then player.prepare()

如果字幕文件尚未在Android设备上存在,从资源文件夹复制到设备

If the subtitle files does not already exists on the android device, copy it from the resource folder to the device

呼叫 player.addTimedTextSource()的第一个参数字符串包含的完整路径中的的字幕文件的设备上和 MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP 作为第二个参数

Call player.addTimedTextSource() with the first argument a String that contains the full path of the subtitle file on the device and MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP as the second argument

通过调用选择时间文字跟踪 player.selectTrack()并通过 timedTextType 的通过搜索从 TrackInfo [] 返回> player.getTrackInfo()(我觉得它通常 2

Select the TimedText track by calling player.selectTrack() and pass the index of timedTextType by searching the TrackInfo[] returned from player.getTrackInfo() (I find it usually 2)

设置一个侦听器 player.setOnTimedTextListener(),然后开始播放媒体文件 player.start()

Set up a listener with player.setOnTimedTextListener() and then start playing the media file player.start()

下面是完整的类:

要运行这个确切类,你需要在你的水库两个文件/原文件夹 sub.srt video.mp4 (或任何扩展)。然后定义一个的TextView id为 txtDisplay 。最后,你的项目/设备/仿真器必须支持 API 16

To run this exact class you will need two files under your res/raw folder sub.srt and video.mp4 (or whatever extensions). Then define a TextView with the id txtDisplay. Finally your project/device/emulator must support 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);
}
}

这里是字幕文件我使用为例:

And here is the subtitle file I am using as example:

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 自动改变(即从字幕文件中读取)的媒体文件的进展

Here are few screenshots from the test app showing that the TextView is changing automatically (i.e. reading from the subtitle file) as the media file progresses

编辑:

下面是$ C $下实例项目

这篇关于寻找addTimedTextSource的工作示例用于从.srt文件,在安卓4.1添加字幕到视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆