Android uses the Mediametadataretriever class to get media information

Source: Internet
Author: User
Tags event listener

I. Yesterday. Describes the use of the Mediametadataretriever class to get the first frame of video: http://blog.csdn.net/u012561176/article/details/47858099, today, To introduce the use of the Mediametadataretriever class to obtain media information.

?? 1. The Mediametadataretriever constructor is used for the class: Mediametadataretriever ()

2. Some of the regular usages of the Mediametadataretriever class are used: The Setdatasource (String path) or the Setdatasource () method of other parameters. and a extractmetadata(int keycode) method that returns a string.

Note: The KeyCode parameter in the extractmetadata(int keycode) method here can be obtained by the constants of the Mediametadataretriever class, Like Mediametadataretriever.metadata_key_album.

3. The constants of the Mediametadataretriever class are used, and here I just enclose a graph of the constants:

The remaining constants are able to view the API documentation on their own and can be viewed through the http://android.toolib.net/reference/android/media/MediaMetadataRetriever.html link. These constants are used after the Setdatasource method, and then return a string type of media information by using the extractmetadata(int keycode) method. The keycode is the constant of the above Mediametadataretriever class.



Two. Next attach a sample, before you write this example, do the following three steps:

1. Turn on the Android emulator.

2. Open the File Explorer view.

3. Put the music files under the/storage/sdcard/music, and note that assuming your SD card storage location, android2.x for/mnt/sdcard or/sdcard,android4.x/storage/sdcard , after the placement succeeds for example to see:



Three. Finally. will be able to write our Android sample, the project name is Android_mediametadataretriever1:

1. First, make the layout, open the Activity_main.xml layout file, when using a vertical linear layout. Added seven TextView controls and a button control, such as the following code:

<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:orien tation= "vertical" > <textview android:id= "@+id/ablum" android:layout_width= "Match_parent" an droid:layout_height= "Wrap_content" android:padding= "5DP" android:text= "haha" android:textsize= "20sp"/&    Gt <textview android:id= "@+id/artist" android:layout_width= "match_parent" android:layout_height= "wrap        _content "android:padding=" 5DP "android:text=" Oh Oh "android:textsize=" 20sp "/> <textview Android:id= "@+id/title" android:layout_width= "match_parent" android:layout_height= "Wrap_content" an droid:padding= "5DP" android:text= "mm-hmm" android:textsize= "20sp"/> <textview android:id= "@+id/ MimeType "Android:layout_width=" MAtch_parent "android:layout_height=" wrap_content "android:padding=" 5DP "android:text=" hehe "Android        Oid:textsize= "20sp"/> <textview android:id= "@+id/duration" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:padding= "5DP" android:text= "uh," android:textsize= "20 SP "/> <textview android:id=" @+id/bitrate "android:layout_width=" Match_parent "android:l        ayout_height= "Wrap_content" android:padding= "5DP" android:text= "Ah Ah" android:textsize= "20sp"/> <textview android:id= "@+id/date" android:layout_width= "match_parent" android:layout_height= "WR        Ap_content "android:padding=" 5DP "android:text=" Oh Oh "android:textsize=" 20sp "/> <button        Android:id= "@+id/button" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Get music Info"/&Gt;</linearlayout> 

2. Next, start writing our Mainactivity class and click on the button control of the above layout to display the obtained media information in 7 TextView controls, such as the following code:

Package Com.android_mediametadataretriever1;import Java.io.file;import Android.app.activity;import Android.media.mediametadataretriever;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;public Class Mainactivity extends Activity {private TextView ablum;//Declaration TextView Object Ablumprivate TextView artist;//declaration TextView Object Ablum_ Artistprivate TextView title;//Declaration TextView Object Titleprivate TextView mimetype;//declaration TextView Object Mimetypeprivate TextView duration;//declaration TextView Object Durationprivate TextView bitrate;//declaration TextView Object Bitrateprivate TextView date;// Declares a TextView object Dateprivate button button;//declares a button object button@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);/* Get control in Layout manager */ablum = (TextView) Findviewbyid (R.id.ablum); artist= (TextView) Findviewbyid (r.id.artist); title= (TextView) Findviewbyid (r.id.title); Mimetype= (TextView) Findviewbyid (r.iD.mimetype);d uration= (TextView) Findviewbyid (r.id.duration), bitrate= (TextView) Findviewbyid (r.id.bitrate);d ate= ( TextView) Findviewbyid (r.id.date); button = (button) Findviewbyid (R.id.button);// Add button click event Listener button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubmediametadataretriever mmr=new mediametadataretriever ();// Instantiate the Mediametadataretriever object mmrfile file=new file ("/storage/sdcard/music/music1.mp3");//Instantiate the Files object, specifying the path to Storage/sdcard/music/music1.mp3mmr.setdatasource (File.getabsolutepath ());//Set the data source for the MMR object to the absolute path of the file object above string Ablumstring=mmr.extractmetadata (mediametadataretriever.metadata_key_album);//Get the title of the music album string artiststring= Mmr.extractmetadata (mediametadataretriever.metadata_key_artist);//Get the music artist information string titlestring= Mmr.extractmetadata (mediametadataretriever.metadata_key_title);//Get Music title information string mimetypestring= Mmr.extractmetadata (Mediametadataretriever.metadata_key_mimetype);//Get music MIME type string Durationstring=mmr.extraCtmetadata (mediametadataretriever.metadata_key_duration);//Get Music duration string Bitratestring=mmr.extractmetadata ( mediametadataretriever.metadata_key_bitrate);//Get the music bit rate. Bit rate string Datestring=mmr.extractmetadata (mediametadataretriever.metadata_key_date);//Gets the date of the music/* Set the contents of the text */ Ablum.settext ("album title:" +ablumstring); Artist.settext ("Artist name:" +artiststring); Title.settext ("Music title:" +titlestring) ; Mimetype.settext ("Music MIME type:" +mimetypestring);d Uration.settext ("duration is:" +durationstring); Bitrate.settext ( "bitrate:" +bitratestring);d ate.settext ("Date:" +datestring),}});}

3. Finally. Since we are setting up the data source by visiting the path of the file SD card. Therefore, the following permissions must be added to the Androidmanifest.xml declaration file:

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


Note: It is important to note that using the Mediametadataretriever class requires Android's minsdkversion minimum of 14. So assuming that your androidmanifest.xml file android:minsdkversion= "8" is the smallest SDK version number less than 14, will be an error. The workaround is to change the minimum SDK version number in the Androidmanifest.xml file, and add the following code to 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 and perform the following effects such as:


Click the Get Music Info button. For example, as seen in:


So the media information will be obtained, some of the media information is not, will be empty.



Five. The above content is only for everyone to study the examination. Not well written. Please forgive me. If there are errors, please point out, thank you!

??

Android uses the Mediametadataretriever class to get media information

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.