Multimedia files in android sdcard operations (1)-music list creation

Source: Internet
Author: User

Recently I made an android music player. I personally felt that the most difficult part was "playing back" and "playing list", but I finally found the implementation method. Different people may implement different methods. Here I will share some of my implementation methods for the "playlist" module. "playing back" will be introduced in the next blog, I hope you can share some of your ideas.

Android uses ContentProvider to support data sharing between different applications. To facilitate operations on data in sdcard by other applications, sdcard also provides the ContentProvider interface. Here we use access to audio files as an example, operations on videos and images are similar. We will not describe them here.

The URI used to access the audio file in sdcard is MediaStore. audio. media. EXTERNAL_CONTENT_URI: to display the information of the music file in the playlist, You need to query the audio file in the sdcard and save the queried information in the Cursor. The Code is as follows:

Cursor c = this.getContentResolver().
query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,

/* This string array indicates the column to be queried */

New String [] {MediaStore. video. media. TITLE, // music name MediaStore. audio. media. DURATION, // total time of music MediaStore. audio. media. ARTIST, // ARTIST MediaStore. audio. media. _ ID, // ID: MediaStore. audio. media. DISPLAY_NAME, // music file name MediaStore. audio. media. DATA // music file path}, null, // query condition, equivalent to null in SQL where statement, // null in query condition ); // sorting method of query results
You can use MediaStore. Audio. Media. XXX to access some information about music files. Only a part is listed here and can be added or deleted as needed.
Now, Cursor c has saved the information of the audio file in the sdcard. The following operations are performed on this Cursor.
First, define three Arrays:
Private int [] _ ids; // array of ids for storing music files private String [] _ titles; // array of titles for storing music files private String [] _ path; // path for storing music files

_ Ids stores the _ ID of all music files to determine which song to play. _ titles stores the music name and is used to display it on the playback interface.

Path of the music file (used when deleting the file ).
Next, define a variable to locate the selected music:
private int pos;
Next, store the information of the music file in the corresponding array:
c.moveToFirst();_ids = new int[c.getCount()];_titles = new String[c.getCount()];_path = new String[c.getCount()];for(int i=0;i<c.getCount();i++){_ids[i] = c.getInt(3);          _titles[i] = c.getString(0);_path[i] = c.getString(5).substring(4);c.moveToNext();}        

Someone may ask why the format of the obtained path is _ path [I] = c. geString (5). substring (4 )? Because MediaStore. Audio. Media. DATA

The obtained content is in the format of/mnt/sdcard/[subfolder name/] Music file name. What we want to get is/sdcard/[subfolder name] Music file name,
Therefore, you must crop the image.
Next, display the information in Cursor to listview:
MusicListAdapter adapter = new MusicListAdapter(this, c);listview.setAdapter(adapter);

MusicListAdapter is a custom Adapter inherited from BaseAdapter. Only the code is pasted here, which is not explained.

Package com. alex. video; import android. content. context; import android. database. cursor; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. textView; public class MusicListAdapter extends BaseAdapter {private Context myCon; private Cursor myCur; private int pos =-1; public MusicListA Dapter (Context con, Cursor cur) {this. myCon = con; this. myCur = cur ;}@ Overridepublic int getCount () {return this. myCur. getCount () ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, view convertView, ViewGroup parent) {convertView = LayoutInflater. from (myCon ). inflate (R. layout. musiclis T, null); myCur. moveToPosition (position); TextView videoTitle = (TextView) convertView. findViewById (R. id. musictitle); if (myCur. getString (0 ). length ()> 24) {try {String musicTitle = bSubstring (myCur. getString (0 ). trim (), 24); videoTitle. setText (musicTitle);} catch (Exception e) {e. printStackTrace () ;}} else {videoTitle. setText (myCur. getString (0 ). trim ();} TextView videoArtist = (TextView) convertView. findViewByI D (R. id. musicartist); if (myCur. getString (2 ). equals ("<unknown>") {videoArtist. setText ("unknown artist");} else {videoArtist. setText (myCur. getString (2);} TextView videoTime = (TextView) convertView. findViewById (R. id. musictime); videoTime. setText (toTime (myCur. getInt (1); ImageView videoItem = (ImageView) convertView. findViewById (R. id. musicitem); videoItem. setImageResource (R. drawable. item); return convertView;}/* Time Format Conversion */Public String toTime (int time) {time/= 1000; int minute = time/60; int hour = minute/60; int second = time % 60; minute % = 60; return String. format ("% 02d: % 02d", minute, second);}/* String cropping */public static String bSubstring (String s, int length) throws Exception {byte [] bytes = s. getBytes ("Unicode"); int n = 0; // indicates the current number of bytes int I = 2; // The number of bytes to be truncated, starting from 3rd bytes (; I <bytes. length & n <length; I ++ ){ // The odd position, such as 3, 5, and 7. if (I % 2 = 1) {n ++ is the second byte in UCS2 encoding; // when the second byte of UCS2 is n plus 1} else {// when the first byte of UCS2 encoding is not equal to 0, the UCS2 is a Chinese character, one Chinese character counts two bytes if (bytes [I]! = 0) {n ++ ;}}// if I is an odd number, process it as an even number if (I % 2 = 1) {// when the UCS2 character is a Chinese character, remove the half-cut Chinese character if (bytes [I-1]! = 0) I = I-1; // If the UCS2 character is a letter or number, else I = I + 1;} return new String (bytes, 0, i, "Unicode ");}}

In this way, the music information is displayed in the list.

The next section describes how to update a list.

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.