In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail how to use MediaExtactor for Android audio and video development. The content is detailed, the steps are clear, and the details are handled properly. I hope this article, "how to use MediaExtactor for Android audio and video development," can help you solve your doubts.
MediaExtactor
MediaExtactor is responsible for the function of extracting audio and video information and data stream in Android audio and video development, which can be used to realize the ability to get audio and video from multimedia files.
Use MediaExtactor
More than one audio and video file may contain multiple data streams (audio data, video data, etc., usually a video combination of multiple audio)
Therefore, MediaExtactor is required to load audio and video files to get all the data tracks.
Get the desired track by traversing
Then specify that the track is all parsed data streams of MediaExtactor
Obtain the track MediaFormat through MediaExtactor
Load audio and video file code
The process of loading audio and video files can transfer Uri, path, URL and so on. After confirming the data tracks you want to parse, you can call selectTrack to lock the tracks to be used by the current MediaExtactor for later data decoding.
/ / load resource MediaExtractor extractor = new MediaExtractor (); extractor.setDataSource (path); / / traverse to get video track int trackIndex = getTrackIndex (extractor, "audio/"); / / selected track extractor.selectTrack (trackIndex); get track code
After loading the file, you can get all the data tracks through the getTrackCount method of MediaExtactor. Then the track information can be obtained through getTrackFormat, and the track format can be compared with all the expected tracks through MediaFormat.KEY_MIME. For example, comparing "audio/" is to get audio track data.
/ / the method private static int getTrackIndex (MediaExtractor extractor, String mediaType) {int trackIndex =-1; for (int I = 0; I < extractor.getTrackCount (); iTunes +) {MediaFormat mediaFormat = extractor.getTrackFormat (I); String mime = mediaFormat.getString (MediaFormat.KEY_MIME); if (mime.startsWith (mediaType)) {trackIndex = I; break;}} return trackIndex } extract track data information
After selecting the track you want to obtain, you can obtain the basic information of the current selection track, such as video size, format, duration, bit rate and other basic audio and video related information. It is convenient to select an appropriate decoder and other necessary information to confirm when decoding.
Acquisition of basic information of audio track
MediaFormatInfo mediaFormatInfo = new MediaFormatInfo (); mediaFormatInfo.mediaFormat = mediaFormat;mediaFormatInfo.audioChannels = mediaFormat.getInteger (MediaFormat.KEY_CHANNEL_COUNT); mediaFormatInfo.audioSampleRate = mediaFormat.getInteger (MediaFormat.KEY_SAMPLE_RATE); mediaFormatInfo.maxInputSize = mediaFormat.getInteger (MediaFormat.KEY_MAX_INPUT_SIZE); mediaFormatInfo.mime = mediaFormat.getString (MediaFormat.KEY_MIME)
Acquisition of basic information of video track
MediaFormatInfo mediaFormatInfo = new MediaFormatInfo (); mediaFormatInfo.mediaFormat = mediaFormat;mediaFormatInfo.videoHeight = mediaFormat.getInteger (MediaFormat.KEY_HEIGHT); mediaFormatInfo.videoWidth = mediaFormat.getInteger (MediaFormat.KEY_WIDTH); mediaFormatInfo.timeDuration = mediaFormat.getLong (MediaFormat.KEY_DURATION); mediaFormatInfo.mime = mediaFormat.getString (MediaFormat.KEY_MIME)
However, if the track you selected is a video track but the audio-related information is extracted incorrectly, there may be an error crash (for example, KEY_IS_ADTS can only extract audio tracks), so try to ensure that the extraction parameters are correctly protected by the code.
Analysis of some source code details
MediaExtactor as an extractor, its underlying code logic is also implemented by calling JNI, and it is only provided by the upper API. You can see in the source details that MediaExtactor also loads the media_jniso library, which is the same logic as MediaPlayer.
Public MediaExtractor () {native_setup ();} private native final void native_setup (); static {System.loadLibrary ("media_jni"); native_init ();}
At the same time, the setDataSoure method calls the underlying native interface and MediaPlayer with the same method name and input parameter.
Private native final void nativeSetDataSource (@ NonNull IBinder httpServiceBinder, @ NonNull String path, @ Nullable String [] keys, @ Nullable String [] values) throws IOException
It is also possible to understand that MediaExtactor and MediaPlayer should belong to the same set of logic and functions on the underlying implementation. However, MediaExtactor can be understood to provide extraction capabilities for a small part of the functions of MediaPlayer, while MediaPlayer is a packaged tool class that only needs to load audio and video resource files to play, and the intermediate extraction track and parsing process are directly implemented for developers at the bottom.
After reading this, the article "how to use MediaExtactor for Android Audio and Video Development" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.