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