Android音楽プレーヤー(一):携帯電話に格納されている音楽を検索する

6067 ワード

Android音楽プレーヤー(一):携帯電話に格納されている音楽を検索する
一、MusicInfo類——音楽実体類
まず、検索した音楽の情報を格納するエンティティクラスが必要です.後でIntentのデータ転送を行うので、ここではエンティティクラスをシーケンス化する必要があります.
Androidでシーケンス化を実現するには,Serializableインタフェースを利用する方法と,Parcelableインタフェースを利用する方法の2つがある.後者はAndroid特有で、前者に比べてParcelableの効率を利用しています
より高いですが、実装は少し複雑ですが、Serializableインタフェースを利用するにはimplementsだけが必要で、システムは自動的にシーケンス化されます.ここではParcelableインタフェースを使用します.
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

public class MusicInfo implements Parcelable {
	
	public final static String KEY_MUSIC= "music";
	
	public static final String KEY_ID = "_id";
	public static final String KEY_SONG_ID = "songid";
	public static final String KEY_ALBUM_ID = "albumid";
	public static final String KEY_DURATION = "duration";
	public static final String KEY_MUSIC_NAME = "musicname";
	public static final String KEY_ARTIST = "artist";
	public static final String KEY_DATA = "data";
	public static final String KEY_FOLDER = "folder";
	public static final String KEY_MUSIC_NAME_KEY = "musicnamekey";
	public static final String KEY_ARTIST_KEY = "artistkey";
	public static final String KEY_FAVORITE = "favorite";
	
	/**      _id */
	public int _id = -1;
	public int songId = -1;
	public int albumId = -1;
	public int duration;
	public String musicName;
	public String artist;
	public String data;
	public String folder;
	public String musicNameKey;
	public String artistKey;
	/** 0       1     */
	public int favorite = 0;
	
	
	@Override
	public int describeContents() {
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Bundle bundle = new Bundle();
		bundle.putInt(KEY_ID, _id);
		bundle.putInt(KEY_SONG_ID, songId);
		bundle.putInt(KEY_ALBUM_ID, albumId);
		bundle.putInt(KEY_DURATION, duration);
		bundle.putString(KEY_MUSIC_NAME, musicName);
		bundle.putString(KEY_ARTIST, artist);
		bundle.putString(KEY_DATA, data);
		bundle.putString(KEY_FOLDER, folder);
		bundle.putString(KEY_MUSIC_NAME_KEY, musicNameKey);
		bundle.putInt(KEY_FAVORITE, favorite);
		dest.writeBundle(bundle);
	}
	
	public static final Parcelable.Creator<MusicInfo> CREATOR = new Parcelable.Creator<MusicInfo>() {

		@Override
		public MusicInfo createFromParcel(Parcel source) {
			MusicInfo music = new MusicInfo();
			Bundle bundle = new Bundle();
			bundle = source.readBundle();
			music._id = bundle.getInt(KEY_ID);
			music.songId = bundle.getInt(KEY_SONG_ID);
			music.albumId = bundle.getInt(KEY_ALBUM_ID);
			music.duration = bundle.getInt(KEY_DURATION);
			music.musicName = bundle.getString(KEY_MUSIC_NAME);
			music.artist = bundle.getString(KEY_ARTIST);
			music.data = bundle.getString(KEY_DATA);
			music.folder = bundle.getString(KEY_FOLDER);
			music.musicNameKey = bundle.getString(KEY_MUSIC_NAME_KEY);
			music.favorite = bundle.getInt(KEY_FAVORITE);
			return music;
		}

		@Override
		public MusicInfo[] newArray(int size) {
			return new MusicInfo[size];
		}
	};


	public int getFavorite() {
		return favorite;
	}
	public void setFavorite(int favorite) {
		this.favorite = favorite;
	}
}
 
  

二 、搜索音乐的核心方法实现

package com.music.wangmusic;

import java.io.File;
import java.util.ArrayList;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

public class MusicUtils {
	
	public static final int FILTER_SIZE = 1 * 1024 * 1024;// 1MB
	public static final int FILTER_DURATION = 1 * 60 * 1000;// 1  
	
	public static String[] proj_music = new String[] {
		MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
		MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID,
		MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ARTIST_ID,
		MediaStore.Audio.Media.DURATION };
	
	//       
	public static ArrayList<MusicInfo> getMusicList(Cursor cursor) {
		if (cursor == null) {
			return null;
		}
		ArrayList<MusicInfo> musicList = new ArrayList<MusicInfo>();
		while (cursor.moveToNext()) {
			MusicInfo music = new MusicInfo();
			music.songId = cursor.getInt(cursor
					.getColumnIndex(MediaStore.Audio.Media._ID));
			music.albumId = cursor.getInt(cursor
					.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
			music.duration = cursor.getInt(cursor
					.getColumnIndex(MediaStore.Audio.Media.DURATION));
			Log.i("Name",""+music.duration);//    
			music.musicName = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.TITLE));
			Log.i("Name",music.musicName);//    
			music.artist = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.ARTIST));

			String filePath = cursor.getString(cursor
					.getColumnIndex(MediaStore.Audio.Media.DATA));
			music.data = filePath;
			Log.i("Name",music.data);//    
			String folderPath = filePath.substring(0,
					filePath.lastIndexOf(File.separator));
			music.folder = folderPath;
			music.musicNameKey = StringHelper.getPingYin(music.musicName);
			music.artistKey = StringHelper.getPingYin(music.artist);
			musicList.add(music);
		}
		cursor.close();
		return musicList;
	}
}

三、検索開始及び出力結果
private void startSearch(){
		new Thread(new Runnable() {

			public void run() {
				Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;//-------------
				ContentResolver cr = MainActivity.this.getContentResolver();
				StringBuffer select = new StringBuffer(" 1=1 ");
				//     :       1  ,      1MB     
				select.append(" and " + Media.SIZE + " > " + MusicUtils.FILTER_SIZE);
				select.append(" and " + Media.DURATION + " > " + MusicUtils.FILTER_DURATION);

				musicArrayList = MusicUtils.getMusicList(cr.query(uri, MusicUtils.proj_music,
						select.toString(), null,
						MediaStore.Audio.Media.ARTIST_KEY));
			}
		}).start();
	}

四、参考
https://github.com/longdw/MyMusic