/**
* 打开内置音乐播放器
*/
private void getAudio() {
Intent intent = new Intent(Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/music/young_for_you.mp3");
intent.setDataAndType(Uri.fromFile(audioFile), "audio/mp3");
startActivity(intent);
}
/**
* 音频播放器---播放列表
*
* @time 下午01:33:53
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class AudioBrowserActivity extends ListActivity {
public static int STATE_SELECT_ALBUM = 0;
public static int STATE_SELECT_SONG = 1;
int currentState = STATE_SELECT_ALBUM;
private Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_browser);
String[] columns = { MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM };
cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
String[] displayFields = new String[] { MediaStore.Audio.Albums.ALBUM };
int[] displayViews = new int[] { android.R.id.text1 };
setListAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, displayFields, displayViews));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if (currentState == STATE_SELECT_ALBUM) {
if (cursor.moveToPosition(position)) {
String[] columns = {//
MediaStore.Audio.Media.DATA,//
MediaStore.Audio.Media._ID,//
MediaStore.Audio.Media.TITLE,//
MediaStore.Audio.Media.DISPLAY_NAME,//
MediaStore.Audio.Media.MIME_TYPE };//
String where = MediaStore.Audio.Media.ALBUM + "= ? ";
String whereValue[] = { cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)) };
String orderBy = MediaStore.Audio.Media.TITLE;
cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, where, whereValue, orderBy);
cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
String[] displayFields = new String[] { MediaStore.Audio.Albums.ALBUM };
int[] displayViews = new int[] { android.R.id.text1 };
setListAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, displayFields, displayViews));
currentState = STATE_SELECT_SONG;
}
} else if (currentState == STATE_SELECT_SONG) {
if (cursor.moveToPosition(position)) {
if (cursor.moveToPosition(position)) {
int fileColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
int mimeTypeColumn = cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE);
String audioFilePath = cursor.getString(fileColumn);
String mimeType = cursor.getString(mimeTypeColumn);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(audioFilePath);
intent.setDataAndType(Uri.fromFile(file), mimeType);
startActivity(intent);
}
}
}
}
}