How to Write a serious Android music player
I have written many music players before, but there are always some problems, such:
1. Long-time playing of Music (even if it is played in the service, it will still be killed );
2. How do I know the progress of playing music? (How to Use mediaplayer. getCurrentPosition () to effectively notify the page of change progress ?);
3. In my past experience, when the next piece of music is played, the next piece of music is often played in seconds.
Finding a tutorial from the Internet is usually a player demo. MediaPlayer is simply put in an Activity for operations. MediaPlayer is placed in Servic in a conscientious tutorial, however, this is far from enough.
The best tutorial is Google's official Training and API Guides. Although I had read the document about services roughly a long time ago, the ancients told me how to learn new things, compared with previous Code experiences, I think there is a better way to implement a music player. Then I decided to write a new music player.
If you have a better solution, I hope you will know me.
Note: This article is not suitable for students who do not know MediaPlayer. If you read this article, you will already use the Service.
I. Reading the music list.
For reading the music list, different music players have different solutions, and some have multiple solutions. For example, Android has a media library that can read data from a local media library to quickly learn the music on the device.
First, I encapsulated an Audio class to store the read Audio Information.
public class Audio {private String mTitle, mTitleKey, mArtist, mArtistKey, mComposer, mAlbum, mAlbumKey, mDisplayName, mMimeType, mPath;private int mId, mArtistId,mAlbumId, mYear,mTrack;private int mDuration = 0, mSize = 0;private boolean isRingtone = false,isPodcast = false,isAlarm = false,isMusic = false,isNotification = false;public Audio (Bundle bundle) { mId = bundle.getInt(MediaStore.Audio.Media._ID);mTitle = bundle.getString(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 encapsulates music media information. Some variables can also be used to see what the name is, such as title and duration. It is nothing more than the song name and duration, some variables are known as words, but they do not know what to use. In fact, I do not know what information it is. Although it has been printed out, I do not know where it can be used, the opposite is what the brain reads first, and then prints the output to see what it is.
Second, a method for reading 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.Audio.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, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.DATA }; public static List getAudioList(Context context) { List audioList = new ArrayList(); 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; }}
Returns an Audio List. Now we can use this List to present a List.