1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3 Copyright 2013 The Android Open Source Project
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16-->
17<sample>
18    <name>BasicMediaDecoder</name>
19    <group>Media</group>
20    <package>com.example.android.basicmediadecoder</package>
21
22    <!-- change minSdk if needed-->
23    <minSdk>17</minSdk>
24
25    <strings>
26        <intro>
27            <![CDATA[
28             This activity uses a TextureView to render the frames of a video decoded using the
29             MediaCodec API.
30            ]]>
31        </intro>
32    </strings>
33
34    <template src="base"/>
35    <common src="media"/>
36
37    <metadata>
38        <status>PUBLISHED</status>
39        <categories>Media</categories>
40        <technologies>Android</technologies>
41        <languages>Java</languages>
42        <solutions>Mobile</solutions>
43        <level>ADVANCED</level>
44        <icon>screenshots/icon-web.png</icon>
45        <screenshots>
46            <img>screenshots/1-launch.png</img>
47            <img>screenshots/2-play-video.png</img>
48        </screenshots>
49        <api_refs>
50            <android>android.media.MediaCodec</android>
51            <android>android.media.MediaExtractor</android>
52            <android>android.animation.TimeAnimator</android>
53        </api_refs>
54
55        <description>
56This sample shows how to use the MediaCoder to decode a video,
57use a TimeAnimator to sync the rendering commands with the system
58display frame rendering and finally render it to a TextureView.
59        </description>
60
61        <intro>
62<![CDATA[
63[MediaCodec][1] was introduced in API 16, and can be used for low level (decoding/encoding) operations.
64In the same API was also introduced [TimeAnimator][2], which can be used to synchronise animation frames.
65Finally, [MediaExtractor][3] provides a simple way to extract demuxed media data from a data source.
66
67The main steps are described below:
68
691. Create a layout with a [TextureView][4] for your activity.
702. Initialise a MediaExtractor instance with `new MediaExtractor()` and a TimeAnimator instance with
71`new TimeAnimator()`.
723. To start video playback, call `setDataSource(this, videoUri, null)` on your MediaExtractor instance,
73where `videoUri` is the URI of your video source.
744. On your MediaExtractor instance, call `getTrackCount()` to know how many tracks you have in your streams.
75They may not all be video tracks. Deselect all tracks by calling `unselectTrack(i)` where `i` is
76the index of the track.
775. Get the mime type of a track by calling `getTrackFormat(i).getString(MediaFormat.KEY_MIME)`
78on your MediaExtractor instance, where `i` is the index of your selected track.
79If the mime type contains "video/", then this is a video track so you can select it, using `selectTrack(i)`
80on your MediaExtractor instance.
816. Create a MediaCodec instance by calling `MediaCodec.createDecoderByType(mimeType)`.
827. Configure your MediaCodec instance with `configure(trackFormat, textureView, null,  0)`,
83where `trackFormat` is obtained by calling `getTrackFormat(i)` on your MediaExtractor instance.
848. Set a TimeListener on your TimeAnimation instance, and override its `onTimeUpdate(final TimeAnimator animation,
85final long totalTime, final long deltaTime)` method.
869. In `onTimeUpdate`, check if the media track has reached the end of stream, using `getSampleFlags()`
87on  your MediaExtractor instance and looking for `MediaCodec.BUFFER_FLAG_END_OF_STREAM` flag.
8810. Still in `onTimeUpdate`, assuming this isn't the end of the sample, write the media sample to your
89MediaDecoder instance, using `queueInputBuffer(index, 0, size, presentationTimeUs, flags)` method.
90You will need to set up your buffers, refer to [MediaCodec][1] documentation for details.
9111. After writing the media sample, you need to advance the sample, calling `advance()` on your
92TimeExtractor instance (this is a blocking operation and should be done outside the main thread).
9312. Finally, you can release and render the media sample by calling
94`dequeueOutputBuffer(info, timeout)` and `releaseOutputBuffer(i, true)`, refer to [MediaCodec][1]
95documentation for details.
9613. In `onPause()` or if you have reached the end of the stream, call `end()` on your TimeAnimation instance,
97then call `stop()` and `release()` on your MediaCodec instance, and finally, call `release()` on your
98MediaExtractor instance.
99
100[1]: http://developer.android.com/reference/android/media/MediaCodec.html
101[2]: http://developer.android.com/reference/android/animation/TimeAnimator.html
102[3]: http://developer.android.com/reference/android/media/MediaExtractor.html
103[4]: http://developer.android.com/reference/android/view/TextureView.html
104]]>
105        </intro>
106    </metadata>
107</sample>
108