This sample shows how to use the MediaCoder to decode a video, use a TimeAnimator to sync the rendering commands with the system display frame rendering and finally render it to a TextureView.
MediaCodec was introduced in API 16, and can be used for low level (decoding/encoding) operations. In the same API was also introduced TimeAnimator, which can be used to synchronise animation frames. Finally, MediaExtractor provides a simple way to extract demuxed media data from a data source.
The main steps are described below:
- Create a layout with a TextureView for your activity.
- Initialise a MediaExtractor instance with
new MediaExtractor()
and a TimeAnimator instance withnew TimeAnimator()
. - To start video playback, call
setDataSource(this, videoUri, null)
on your MediaExtractor instance, wherevideoUri
is the URI of your video source. - On your MediaExtractor instance, call
getTrackCount()
to know how many tracks you have in your streams. They may not all be video tracks. Deselect all tracks by callingunselectTrack(i)
wherei
is the index of the track. - Get the mime type of a track by calling
getTrackFormat(i).getString(MediaFormat.KEY_MIME)
on your MediaExtractor instance, wherei
is the index of your selected track. If the mime type contains "video/", then this is a video track so you can select it, usingselectTrack(i)
on your MediaExtractor instance. - Create a MediaCodec instance by calling
MediaCodec.createDecoderByType(mimeType)
. - Configure your MediaCodec instance with
configure(trackFormat, textureView, null, 0)
, wheretrackFormat
is obtained by callinggetTrackFormat(i)
on your MediaExtractor instance. - Set a TimeListener on your TimeAnimation instance, and override its
onTimeUpdate(final TimeAnimator animation, final long totalTime, final long deltaTime)
method. - In
onTimeUpdate
, check if the media track has reached the end of stream, usinggetSampleFlags()
on your MediaExtractor instance and looking forMediaCodec.BUFFER_FLAG_END_OF_STREAM
flag. - Still in
onTimeUpdate
, assuming this isn't the end of the sample, write the media sample to your MediaDecoder instance, usingqueueInputBuffer(index, 0, size, presentationTimeUs, flags)
method. You will need to set up your buffers, refer to MediaCodec documentation for details. - After writing the media sample, you need to advance the sample, calling
advance()
on your TimeExtractor instance (this is a blocking operation and should be done outside the main thread). - Finally, you can release and render the media sample by calling
dequeueOutputBuffer(info, timeout)
andreleaseOutputBuffer(i, true)
, refer to MediaCodec documentation for details. - In
onPause()
or if you have reached the end of the stream, callend()
on your TimeAnimation instance, then callstop()
andrelease()
on your MediaCodec instance, and finally, callrelease()
on your MediaExtractor instance.
- Android SDK 27
- Android Build Tools v27.0.2
- Android Support Repository
This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.
- Google+ Community: https://plus.google.com/communities/105153134372062985968
- Stack Overflow: http://stackoverflow.com/questions/tagged/android
If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-BasicMediaDecoder
Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
Copyright 2017 The Android Open Source Project, Inc.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.