xref: /aosp_15_r20/developers/build/prebuilts/gradle/BasicMediaDecoder/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin Li
2*d353a188SXin LiAndroid BasicMediaDecoder Sample
3*d353a188SXin Li===================================
4*d353a188SXin Li
5*d353a188SXin LiThis sample shows how to use the MediaCoder to decode a video,
6*d353a188SXin Liuse a TimeAnimator to sync the rendering commands with the system
7*d353a188SXin Lidisplay frame rendering and finally render it to a TextureView.
8*d353a188SXin Li
9*d353a188SXin LiIntroduction
10*d353a188SXin Li------------
11*d353a188SXin Li
12*d353a188SXin Li[MediaCodec][1] was introduced in API 16, and can be used for low level (decoding/encoding) operations.
13*d353a188SXin LiIn the same API was also introduced [TimeAnimator][2], which can be used to synchronise animation frames.
14*d353a188SXin LiFinally, [MediaExtractor][3] provides a simple way to extract demuxed media data from a data source.
15*d353a188SXin Li
16*d353a188SXin LiThe main steps are described below:
17*d353a188SXin Li
18*d353a188SXin Li1. Create a layout with a [TextureView][4] for your activity.
19*d353a188SXin Li2. Initialise a MediaExtractor instance with `new MediaExtractor()` and a TimeAnimator instance with
20*d353a188SXin Li`new TimeAnimator()`.
21*d353a188SXin Li3. To start video playback, call `setDataSource(this, videoUri, null)` on your MediaExtractor instance,
22*d353a188SXin Liwhere `videoUri` is the URI of your video source.
23*d353a188SXin Li4. On your MediaExtractor instance, call `getTrackCount()` to know how many tracks you have in your streams.
24*d353a188SXin LiThey may not all be video tracks. Deselect all tracks by calling `unselectTrack(i)` where `i` is
25*d353a188SXin Lithe index of the track.
26*d353a188SXin Li5. Get the mime type of a track by calling `getTrackFormat(i).getString(MediaFormat.KEY_MIME)`
27*d353a188SXin Lion your MediaExtractor instance, where `i` is the index of your selected track.
28*d353a188SXin LiIf the mime type contains "video/", then this is a video track so you can select it, using `selectTrack(i)`
29*d353a188SXin Lion your MediaExtractor instance.
30*d353a188SXin Li6. Create a MediaCodec instance by calling `MediaCodec.createDecoderByType(mimeType)`.
31*d353a188SXin Li7. Configure your MediaCodec instance with `configure(trackFormat, textureView, null,  0)`,
32*d353a188SXin Liwhere `trackFormat` is obtained by calling `getTrackFormat(i)` on your MediaExtractor instance.
33*d353a188SXin Li8. Set a TimeListener on your TimeAnimation instance, and override its `onTimeUpdate(final TimeAnimator animation,
34*d353a188SXin Lifinal long totalTime, final long deltaTime)` method.
35*d353a188SXin Li9. In `onTimeUpdate`, check if the media track has reached the end of stream, using `getSampleFlags()`
36*d353a188SXin Lion  your MediaExtractor instance and looking for `MediaCodec.BUFFER_FLAG_END_OF_STREAM` flag.
37*d353a188SXin Li10. Still in `onTimeUpdate`, assuming this isn't the end of the sample, write the media sample to your
38*d353a188SXin LiMediaDecoder instance, using `queueInputBuffer(index, 0, size, presentationTimeUs, flags)` method.
39*d353a188SXin LiYou will need to set up your buffers, refer to [MediaCodec][1] documentation for details.
40*d353a188SXin Li11. After writing the media sample, you need to advance the sample, calling `advance()` on your
41*d353a188SXin LiTimeExtractor instance (this is a blocking operation and should be done outside the main thread).
42*d353a188SXin Li12. Finally, you can release and render the media sample by calling
43*d353a188SXin Li`dequeueOutputBuffer(info, timeout)` and `releaseOutputBuffer(i, true)`, refer to [MediaCodec][1]
44*d353a188SXin Lidocumentation for details.
45*d353a188SXin Li13. In `onPause()` or if you have reached the end of the stream, call `end()` on your TimeAnimation instance,
46*d353a188SXin Lithen call `stop()` and `release()` on your MediaCodec instance, and finally, call `release()` on your
47*d353a188SXin LiMediaExtractor instance.
48*d353a188SXin Li
49*d353a188SXin Li[1]: http://developer.android.com/reference/android/media/MediaCodec.html
50*d353a188SXin Li[2]: http://developer.android.com/reference/android/animation/TimeAnimator.html
51*d353a188SXin Li[3]: http://developer.android.com/reference/android/media/MediaExtractor.html
52*d353a188SXin Li[4]: http://developer.android.com/reference/android/view/TextureView.html
53*d353a188SXin Li
54*d353a188SXin LiPre-requisites
55*d353a188SXin Li--------------
56*d353a188SXin Li
57*d353a188SXin Li- Android SDK 27
58*d353a188SXin Li- Android Build Tools v27.0.2
59*d353a188SXin Li- Android Support Repository
60*d353a188SXin Li
61*d353a188SXin LiScreenshots
62*d353a188SXin Li-------------
63*d353a188SXin Li
64*d353a188SXin Li<img src="screenshots/1-launch.png" height="400" alt="Screenshot"/> <img src="screenshots/2-play-video.png" height="400" alt="Screenshot"/>
65*d353a188SXin Li
66*d353a188SXin LiGetting Started
67*d353a188SXin Li---------------
68*d353a188SXin Li
69*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
70*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
71*d353a188SXin Li
72*d353a188SXin LiSupport
73*d353a188SXin Li-------
74*d353a188SXin Li
75*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
76*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
77*d353a188SXin Li
78*d353a188SXin LiIf you've found an error in this sample, please file an issue:
79*d353a188SXin Lihttps://github.com/googlesamples/android-BasicMediaDecoder
80*d353a188SXin Li
81*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
82*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
83*d353a188SXin Li
84*d353a188SXin LiLicense
85*d353a188SXin Li-------
86*d353a188SXin Li
87*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc.
88*d353a188SXin Li
89*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
90*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
91*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
92*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
93*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
94*d353a188SXin Lithe License at
95*d353a188SXin Li
96*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
97*d353a188SXin Li
98*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
99*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
100*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
101*d353a188SXin LiLicense for the specific language governing permissions and limitations under
102*d353a188SXin Lithe License.
103