Android mediaplayer subtitle Synchronization

Source: Internet
Author: User

Sorry for the logic of writing a blog for the first time ~

Android updates subtitle tracking support from api16. The following methods are used.

Added in API level 16

Public void addtimedtextsource (context, Uri, string mimetype) Public void addtimedtextsource (string path, string mimetype) Public void addtimedtextsource (filedescriptor FD, long offset, long length, string mimetype) public void addtimedtextsource (filedescriptor FD, string mimetype)

Take the second method as an example. Let's take a look at the API comments:

 

Added in API level 16
Adds an external timed text source file. currently supported format is SubRip with the file extension. SRT, case insensitive. note that a single external timed text source may contain multiple tracks in it. one can find the total number of available tracks usinggetTrackInfo()To see what additional tracks become available after this method call.

Mediaplayer supports subtitles for external files, but only supports. srt format. This file may have multiple trackers tracking it. You can call the gettrackinfo () method to obtain all the trackers.

Trackinfo has four types: Unknown, video, audio, and subtitle.

Added in API level 16
Public static final int media_track_type_unknown = 0;
Public static final int media_track_type_video = 1;
Public static final int media_track_type_audio = 2;
Public static final int media_track_type_timedtext = 3;

Use

This error is often encountered during the call:

03-29 14:42:38. 675: W/system. Err (21382): Java. Lang. runtimeexception: Failure Code:-38
03-29 14:42:38. 675: W/system. Err (21382): at Android. Media. mediaplayer. Invoke (mediaplayer. Java: 644)
03-29 14:42:38. 675: W/system. Err (21382): at Android. Media. mediaplayer. addtimedtextsource (mediaplayer. Java: 1814)
03-29 14:42:38. 675: W/system. Err (21382): at Android. Media. mediaplayer. addtimedtextsource (mediaplayer. Java: 1778)
03-29 14:42:38. 675: W/system. Err (21382): at Android. Media. mediaplayer. addtimedtextsource (mediaplayer. Java: 1710)
03-29 14:42:38. 675: W/system. Err (21382): At com. Demo. Devin. Fragment. qosdemofragment $ videoholder. onprepared (qosdemofragment. Java: 224)

The API tells us that these exceptions will be thrown:

Added in API level 16

Throws:

Ioexception-if the file cannot be accessed or is already upted.

Illegalargumentexception-if the mimetype is not supported.

Illegalstateexception-
If called in an invalid state.

 

After testing, there is no problem with calling onprepared!

However, you cannot call this method, and you need to register the response.ListenerAnd callSelecttrackMethod.

The Code is as follows: (in the onprepared callback)

 

try            {                mMediaPlayer.addTimedTextSource(srtPath,                        MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);                TrackInfo[] trackInfos = mMediaPlayer.getTrackInfo();                if (trackInfos != null && trackInfos.length > 0)                {                    for (int i = 0; i < trackInfos.length; i++)                    {                        final TrackInfo info = trackInfos[i];                        Log.w(TAG, "TrackInfo: " + info.getTrackType() + " "                                + info.getLanguage());                        if (info.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_AUDIO)                        {                            // mMediaPlayer.selectTrack(i);                        }                        else if (info.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT)                        {                            mMediaPlayer.selectTrack(i);                        }                    }                }            }            catch (Exception e)            {                e.printStackTrace();            }

 

This callback will be triggered in the ontimedtextlistener that will be registered later. Then, with textview, subtitles will be implemented. Android has already completed the synchronization function!

 

The above is the normal operation when I play the MP4 stream! However, when playing m3u8 streams, the error runtimeexception is thrown because it may be related to the onprepared mechanism of the mediaplayer.

No solutions have been found to solve this problem ....

I tried to call this method after the video is played ~


If anyone has already solved the problem, please let me know. Thank you.

Added in
API level 16

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.