如何寫一個正經的Android音樂播放器 一
阿新 • • 發佈:2019-02-11
關於音樂列表的讀取,不同的音樂播放器都會有不同的方案,有的是有多重方案並用。例如說,全盤掃描音樂格式檔案等,但是Android本身有媒體庫,可以讀取本地媒體庫的資料,來快速獲知裝置上的音樂。
首先我封裝了一個Audio類來儲存讀取的音訊資訊。
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; } }
以上類來封裝音樂媒體資訊,一些變數看名字也能看出來是什麼,例如說title,duration此類,無非就是歌曲名稱,時長之類的資訊,有些變數名字單詞認識,但是並不知道幹什麼用,其實我也不知道是什麼資訊,雖然打印出來過,但是並不懂到底哪裡能用到,反正一股腦都讀出來了先,以後再列印輸出看看都是些什麼。
其次,一個讀取媒體音訊的方法:
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<Audio> getAudioList(Context context) { List<Audio> audioList = new ArrayList<Audio>(); 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; } }
如此返回一個Audio的List,現在我們可以用這個List做一個列表展示。