Android uses the Mediametadataretriever class to get the first frame of video

Source: Internet
Author: User

First, to introduce the Mediametadataretriever class, this class is located under the Android.media package, here, first attached to see this class API address: Mediametadataretriever class. You can see for yourself.

1.MediaMetadataRetriever class Overview: Mediametadataretriever class provides a unified interface for retrieving frame and meta data f Rom an input media file.

Translation come on. The Mediametadataretriever class provides a unified interface to retrieve frames and retrieve metadata from an input media file.

The constants provided by the 2.MediaMetadataRetriever class: The Mediametadataretriever class provides very many constants. are int constants, some to get the metadata of the media file, some to get the video frame operation, here you can view the API, here are a few constants:

The first one is the album title of the media file that we set up, the second is the album artist who gets the media files we set up, the third artist to get the media files we set up, and the fourth to get the author of the media files we set up.

The construction method of the 3.MediaMetadataRetriever class:mediametadataretriever() is not a construction method.

The public method of the 4.MediaMetadataRetriever class. For example, as seen in:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /southeast ">

It is possible to understand the usefulness of these methods according to the explanations. One of the most frequently used methods is the Setdatasource method. Used to set the data source, which can be set from the file's path, file description descriptor, URI address and Uri object.

Another frequently used method is the Getframeattime method, which is used to get the frame and return a bitmap object, which is able to obtain the frame after the first frame and how much time.



Two. This article is mainly about how to use the Mediametadataretriever class to get the first frame of the video before writing our Android project. Still do the following three steps:

1. Turn on the Android emulator.

2. Open the view file Explorer, which shows the files folders and files in the Android emulator, it must be noted that the version number of the Android emulator is a problem, the version number is different from the location of the SD card. android2.x system SD Card storage location is/mnt/sdcard or/sdcard, and android4.x system SD card storage location is/storage/sdcard/.

3. Then put our video files into the location of the SD card. My simulator version number is 4.x, so I store the video file in the location:

Suppose you have a file folder that you don't have permission to put in a file. The red warning will be output on the console. No permissions.



Three. Below, you will be able to write our Android project, create a new project Android_mediametadataretriever:

1. Open our layout file Activity_main.xml, code such as the following:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" >    <imageview         android:id= "@+id/imageview"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_gravity= "center"        android: src= "@drawable/ic_launcher"/>        <button         android:id= "@+id/button" android:layout_width= "        Match_parent "        android:layout_height=" wrap_content "        android:text=" get video thumbnails "/>    </ Linearlayout>

2. Open our Mainactivity.java file. The code is as follows:

Package Com.android_mediametadataretriever;import Java.io.file;import Android.app.activity;import Android.graphics.bitmap;import Android.media.mediametadataretriever;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity {private ImageView imageview;//declares the ImageView object private button button;//declares the Button object @overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); imageView= ( ImageView) Findviewbyid (R.id.imageview);//Gets the ImageView control button= (Button) Findviewbyid (R.id.button) in the layout manager;// Get the button control in Layout Manager//settings buttons Click event Listener Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {mediametadataretriever mmr=new mediametadataretriever ();//Instantiate Mediametadataretriever Object file File=new file ("/storage /sdcard/movies/music1.mp4 ");//Instantiate the file object with a path of/storage/sdcard/movies/music1.mp4if (File.exists ()) {Mmr.setdatasource (File.getabsolutepath ());// Sets the absolute path specified by the data source to the file object bitmap bitmap=mmr.getframeattime ();//Gets the bitmap object of the first frame of the video if (bitmap!=null) { Imageview.setimagebitmap (bitmap);//Set ImageView display picture Toast.maketext (mainactivity.this, "Get video thumbnail success", toast.length_ Short). Show ()//Get video thumbnail successfully, pop up message prompt box}else{toast.maketext (mainactivity.this, "Get video thumbnail failed", Toast.length_short). Show () ;//Get video thumbnail failed, pop-up message prompt box}}else{toast.maketext (mainactivity.this, "file does not exist", toast.length_short). Show ();//file does not exist. Pop-up Message Prompt}});}}

The file object in the above code specifies the path to the folder under the SD card path of the Android 4.x system, assuming the lower version number. Or, look at the path to the SD card of the File Explorer view. Both the/mnt/sdcard folder and the/sdcard folder belong to the/storage/sdcard folder's connection file. So the real storage path is/storage/sdcard.

3. Finally. Be sure to add our license to the Androidmanifest.xml declaration file. That is, permissions. In this file plus a line to read the SD card file permission code can be, code such as the following:

<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Note: It is important to note that using the Mediametadataretriever class requires Android minsdkversion minimum of 14, so assume your Androidmanifest.xml file in Android: Minsdkversion= "8" is the smallest SDK version number is less than 14, will be an error, the solution can be changed in the Androidmanifest.xml file to the Minimum SDK version number, you can also add the following code in the Mainactivity class:

@TargetApi (build.version_codes. Ice_cream_sandwich)

So that we use this Mediametadataretriever class will not error.


Four. Deploy this project to the Android emulator to perform effects such as the following:

Click the Get Video thumbnail button, for example, as seen in:



Five. The above content is only for everyone to study the test, write not well. Please note that if there is an error, please indicate. Thank you!







Android uses the Mediametadataretriever class to get the first frame of video

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.