I've written a lot of music players before, but there are always some problems, such as:
1, music playback problem for a long time (that is, put in the service to play, will still be killed);
2, how to master the Music playback progress? (How can I use mediaplayer.getcurrentposition () to effectively notify the interface of change progress? );
3, in my previous experience, the music played the next time, often appear the current music playback is still a few seconds before the next song of the situation.
found in tutorials on the web, usually a player's demo mediaplayer activity mediaplayer servic
The best tutorials are Google's official Training and API Guides, though it's been along time since you've read the documentation about the service very simply and rudely, The ancients said that Wen know the new well, after this temperature, compared to previous code experience, think there can be a better way to achieve a music player. Then decided to re-write a music player.
If there is a better plan for the great God, I will know.
Note: This article, not suitable for MediaPlayer do not understand the classmate, assume that this article, are already preliminary use of the service students.
One, the reading of the music list.
With regard to the reading of the music list, different music players will have different schemes, some of which have multiple schemes to use. For example, a full scan of music format files, etc., but Android itself has a media library, you can read the local media Library data, to quickly learn the music on the device.
First, I encapsulated an audio class to store the read-through information.
public class Audio {private String mtitle, Mtitlekey, Martist, Martistkey, Mcomposer, Malbum, Malbumkey, MDi Splayname, Mmimetype, mpath;private int mId, Martistid,malbumid, myear,mtrack;private in T mduration = 0, Msize = 0;private Boolean isringtone = False,ispodcast = False,isalarm = False,ismusic = False,isnotifica tion = false;public Audio (bundle bundle) {mId = Bundle.getint (mediastore.audio.media._id); mtitle = Bundle.getstri Ng (MediaStore.Audio.Media.TITLE); Mtitlekey = bundle.getstring (MediaStore.Audio.Media.TITLE_KEY); martist = Bundle.getstring (MediaStore.Audio.Media.ARTIST); Martistkey = bundle.getstring (MediaStore.Audio.Media.ARTIST_KEY) ; mcomposer = bundle.getstring (MediaStore.Audio.Media.COMPOSER); malbum = Bundle.getstring ( MediaStore.Audio.Media.ALBUM); Malbumkey = bundle.getstring (MediaStore.Audio.Media.ALBUM_KEY); mdisplayname = Bundle.getstring (MediaStore.Audio.Media.DISPLAY_NAME); myear = Bundle.getint (MediaStore.Audio.MediA.year); mmimetype = bundle.getstring (MediaStore.Audio.Media.MIME_TYPE); MPath = Bundle.getstring ( MediaStore.Audio.Media.DATA); Martistid = Bundle.getint (MediaStore.Audio.Media.ARTIST_ID); malbumid = Bundle.getint ( MediaStore.Audio.Media.ALBUM_ID); mtrack = Bundle.getint (MediaStore.Audio.Media.TRACK); mduration = Bundle.getint ( MediaStore.Audio.Media.DURATION); msize = Bundle.getint (MediaStore.Audio.Media.SIZE); isringtone = Bundle.getint ( MediaStore.Audio.Media.IS_RINGTONE) = = 1;ispodcast = Bundle.getint (MediaStore.Audio.Media.IS_PODCAST) = = 1;isalarm = Bundle.getint (MediaStore.Audio.Media.IS_ALARM) = = 1;ismusic = Bundle.getint (MediaStore.Audio.Media.IS_MUSIC) = = 1; Isnotification = Bundle.getint (MediaStore.Audio.Media.IS_NOTIFICATION) = = 1; } public int getId () {return mId; } public String GetMimeType () {return mmimetype;} public int getduration () {return mduration;} public int GetSize () {return msize;} public Boolean Isringtone () {return isringtone;} public boolean ispodcast () {return ispodcast;} public Boolean isalarm () {return isalarm;} public Boolean ismusic () {return ismusic;} public Boolean isnotification () {return isnotification;} Public String GetTitle () {return mtitle;} Public String Gettitlekey () {return mtitlekey;} Public String getartist () {return martist;} public int Getartistid () {return martistid;} Public String Getartistkey () {return martistkey;} Public String Getcomposer () {return mcomposer;} Public String Getalbum () {return malbum;} public int Getalbumid () {return malbumid;} Public String Getalbumkey () {return malbumkey;} Public String GetDisplayName () {return mdisplayname;} public int getYear () {return myear;} public int Gettrack () {return mtrack;} Public String GetPath () {return mPath;}}
The above class to encapsulate music media information, some variables to see the name can also see what is, for example, said title,duration This class, is nothing more than song name, time-length and other information, some variable name word, but do not know what to use, In fact, I do not know what information, although printed out, but do not know exactly where to use, anyway, a brain is read out first, after the printout to see what is.
Second, a way to read the media Audio:
public class Mediautils {public static final string[] Audio_keys = new string[]{mediastore.audio.media._id , MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.TITLE_KEY, MediaStore.Audio.Media. ARTIST, MediaStore.Audio.Media.ARTIST_ID, MediaStore.Audio.Media.ARTIST_KEY, Mediastore.a Udio. Media.composer, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ALBUM_ID, Mediastore . Audio.Media.ALBUM_KEY, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.TRACK, MediaStore.Audio.Media.IS_RINGTONE, MediaStore.Audio.Media.IS_PODCAST, MediaStore.Audio.Media.IS_ ALARM, MediaStore.Audio.Media.IS_MUSIC, MediaStore.Audio.Media.IS_NOTIFICATION, MediaStor E.audio.media.mime_type, MediaStore.Audio.Media.DATA}; public static list<audio> Getaudiolist (context context) {list<audio> audiolist = new Arraylist<audi O> (); Contentresolver resolver = Context.getcontentresolver (); cursor cursor = resolver.query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Audio_keys, NULL, NULL, NULL); For (Cursor.movetofirst ();!cursor.isafterlast (); Cursor.movetonext ()) {Bundle bundle = new bundle (); for (int i = 0; i < audio_keys.length; i++) {final String key = Audio_keys[i]; Final int columnindex = Cursor.getcolumnindex (key); Final int type = Cursor.gettype (columnindex); Switch (type) {case Cursor.FIELD_TYPE_BLOB:break; Case Cursor.FIELD_TYPE_FLOAT:float Floatvalue = CUrsor.getfloat (columnindex); Bundle.putfloat (key, Floatvalue); Break Case Cursor.FIELD_TYPE_INTEGER:int intvalue = Cursor.getint (columnindex); Bundle.putint (key, Intvalue); Break Case Cursor.FIELD_TYPE_NULL:break; Case Cursor.FIELD_TYPE_STRING:String strvalue = cursor.getstring (columnindex); Bundle.putstring (key, strvalue); Break }} Audio audio = new audio (bundle); Audiolist.add (audio); } cursor.close (); return audiolist; }}
So return to an Audio list, and now we can use this list to do a listing.
How to write a serious Android music player one