1
2Android BasicMediaDecoder Sample
3===================================
4
5This sample shows how to use the MediaCoder to decode a video,
6use a TimeAnimator to sync the rendering commands with the system
7display frame rendering and finally render it to a TextureView.
8
9Introduction
10------------
11
12[MediaCodec][1] was introduced in API 16, and can be used for low level (decoding/encoding) operations.
13In the same API was also introduced [TimeAnimator][2], which can be used to synchronise animation frames.
14Finally, [MediaExtractor][3] provides a simple way to extract demuxed media data from a data source.
15
16The main steps are described below:
17
181. Create a layout with a [TextureView][4] for your activity.
192. Initialise a MediaExtractor instance with `new MediaExtractor()` and a TimeAnimator instance with
20`new TimeAnimator()`.
213. To start video playback, call `setDataSource(this, videoUri, null)` on your MediaExtractor instance,
22where `videoUri` is the URI of your video source.
234. On your MediaExtractor instance, call `getTrackCount()` to know how many tracks you have in your streams.
24They may not all be video tracks. Deselect all tracks by calling `unselectTrack(i)` where `i` is
25the index of the track.
265. Get the mime type of a track by calling `getTrackFormat(i).getString(MediaFormat.KEY_MIME)`
27on your MediaExtractor instance, where `i` is the index of your selected track.
28If the mime type contains "video/", then this is a video track so you can select it, using `selectTrack(i)`
29on your MediaExtractor instance.
306. Create a MediaCodec instance by calling `MediaCodec.createDecoderByType(mimeType)`.
317. Configure your MediaCodec instance with `configure(trackFormat, textureView, null,  0)`,
32where `trackFormat` is obtained by calling `getTrackFormat(i)` on your MediaExtractor instance.
338. Set a TimeListener on your TimeAnimation instance, and override its `onTimeUpdate(final TimeAnimator animation,
34final long totalTime, final long deltaTime)` method.
359. In `onTimeUpdate`, check if the media track has reached the end of stream, using `getSampleFlags()`
36on  your MediaExtractor instance and looking for `MediaCodec.BUFFER_FLAG_END_OF_STREAM` flag.
3710. Still in `onTimeUpdate`, assuming this isn't the end of the sample, write the media sample to your
38MediaDecoder instance, using `queueInputBuffer(index, 0, size, presentationTimeUs, flags)` method.
39You will need to set up your buffers, refer to [MediaCodec][1] documentation for details.
4011. After writing the media sample, you need to advance the sample, calling `advance()` on your
41TimeExtractor instance (this is a blocking operation and should be done outside the main thread).
4212. Finally, you can release and render the media sample by calling
43`dequeueOutputBuffer(info, timeout)` and `releaseOutputBuffer(i, true)`, refer to [MediaCodec][1]
44documentation for details.
4513. In `onPause()` or if you have reached the end of the stream, call `end()` on your TimeAnimation instance,
46then call `stop()` and `release()` on your MediaCodec instance, and finally, call `release()` on your
47MediaExtractor instance.
48
49[1]: http://developer.android.com/reference/android/media/MediaCodec.html
50[2]: http://developer.android.com/reference/android/animation/TimeAnimator.html
51[3]: http://developer.android.com/reference/android/media/MediaExtractor.html
52[4]: http://developer.android.com/reference/android/view/TextureView.html
53
54Pre-requisites
55--------------
56
57- Android SDK 28
58- Android Build Tools v28.0.3
59- Android Support Repository
60
61Screenshots
62-------------
63
64<img src="screenshots/1-launch.png" height="400" alt="Screenshot"/> <img src="screenshots/2-play-video.png" height="400" alt="Screenshot"/>
65
66Getting Started
67---------------
68
69This sample uses the Gradle build system. To build this project, use the
70"gradlew build" command or use "Import Project" in Android Studio.
71
72Support
73-------
74
75- Google+ Community: https://plus.google.com/communities/105153134372062985968
76- Stack Overflow: http://stackoverflow.com/questions/tagged/android
77
78If you've found an error in this sample, please file an issue:
79https://github.com/googlesamples/android-BasicMediaDecoder
80
81Patches are encouraged, and may be submitted by forking this project and
82submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
83
84License
85-------
86
87Copyright 2019 The Android Open Source Project, Inc.
88
89Licensed to the Apache Software Foundation (ASF) under one or more contributor
90license agreements.  See the NOTICE file distributed with this work for
91additional information regarding copyright ownership.  The ASF licenses this
92file to you under the Apache License, Version 2.0 (the "License"); you may not
93use this file except in compliance with the License.  You may obtain a copy of
94the License at
95
96http://www.apache.org/licenses/LICENSE-2.0
97
98Unless required by applicable law or agreed to in writing, software
99distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
100WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
101License for the specific language governing permissions and limitations under
102the License.
103