Obtain Audio Information through mediastore

Source: Internet
Author: User

The Android system provides interfaces such as mediascanner, mediaprovider, and mediastore, as well as a set of database tables, which are provided to users through content providers. When a mobile phone is turned on or has SD card plugging or removal, the system will automatically scan the media files on the SD card and mobile phone memory, such as audio, video, and images, put the corresponding information in the defined database table. In this program, we do not need to worry about how to scan files in the mobile phone, as long as we know how to query and use this information. Mediastore defines a series of data tables. Through the query interface provided by contentresolver, we can obtain various required information. The following describes how to query the music file information on the SD card.
First, let's take a look at the contentresolver query interface:
Cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder );

Uri: specify the name of the database to be queried and the name of the table. From mediastore, we can find the parameters for the corresponding information. For details, refer to the development documentation.
Projection: Specifies the columns in the database table to be queried. The returned cursor contains the corresponding information. Null returns all information.
Selection: Specify query Conditions
Selectionargs: Which of the following are in the selection parameter? This symbol is used to replace the question mark with the actual value. If selection does not exist? Then, the string array can be null.
Sortorder: Specify the order of query results
The following command returns information about all music files on the external memory card:
Cursor cursor = query (mediastore. Audio. Media. external_content_uri, null, null, mediastore. Audio. Media. default_sort_order );
After obtaining the cursor, we can call the specific music information of the cursor method:
Song ID: mediastore. Audio. Media. _ id

Int id = cursor. getint (cursor. getcolumnindexorthrow (mediastore. Audio. Media. _ id ));

Song name: mediastore. Audio. Media. titl
String tilte = cursor. getstring (cursor. getcolumnindexorthrow (mediastore. Audio. Media. Title ));

Album name: mediastore. Audio. Media. Album
String album = cursor. getstring (cursor. getcolumnindexorthrow (mediastore. Audio. Media. album ));

Artist name: mediastore. Audio. Media. Artist
String artist = cursor. getstring (cursor. getcolumnindexorthrow (mediastore. Audio. Media. Artist ));

Full path of the song file: mediastore. Audio. Media. Data

String url = cursor. getstring (cursor. getcolumnindexorthrow (mediastore. Audio. Media. Data ));

Name of the song file: mediastroe. Audio. Media. display_name

String display_name = cursor. getstring (cursor. getcolumnindexorthrow (mediastore. Audio. Media. display_name ));

Release Date of the song file: mediastore. Audio. Media. Year
String year = cursor. getstring (cursor. getcolumnindexorthrow (mediastore. Audio. Media. Year ));

Total playback duration of a song: mediastore. Audio. Media. Duration
Int duration = cursor. getint (cursor. getcolumnindexorthrow (mediastore. Audio. Media. duration ));

Size of the song file: mediastore. Audio. Media. Size
Int size = cursor. getlong (cursor. getcolumnindexorthrow (mediastore. Audio. Media. Size ));

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.