该Demo执行后,会显示全部你sd卡上的音乐文件列表, 并能够点击列表选择某一首歌曲进行播放。
执行效果:
点击download出现:
然后点击歌曲调用系统播放器播放。
源码:
activity_audio_browser.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
AudioBrowser.java:
package com.multimediademo5customaudioplayer2; import java.io.File; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.ListView; import android.widget.SimpleCursorAdapter; /** * 该Demo执行后,会显示全部你sd卡上的音乐文件列表。 并能够点击列表选择某一首歌曲进行播放。 * */ public class AudioBrowser extends ListActivity { private Cursor cursor; public static int STATE_SELECT_ALBUM = 0; public static int STATE_SELECT_SONG = 1; private int currentState = STATE_SELECT_ALBUM; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_audio_browser); /** * 创建一个字符串数组,表示当执行查询时将从MediaStore返回的列。 */ String[] columns = { android.provider.MediaStore.Audio.Albums._ID, android.provider.MediaStore.Audio.Albums.ALBUM }; /** * 返回一个全部可用唱片集的列表 */ cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, null, null, null); String[] displayFields = new String[] { MediaStore.Audio.Albums.ALBUM }; int[] displayViews = new int[] { android.R.id.text1 }; /** * 能够使用setListAdapter方法将Cursor对象绑定到ListView对象 */ setListAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, displayFields, displayViews)); } @SuppressWarnings("deprecation") @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, 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 = android.provider.MediaStore.Audio.Media.ALBUM + "=?"; String whereVal[] = { cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Albums.ALBUM)) }; String orderBy = android.provider.MediaStore.Audio.Media.TITLE; cursor = managedQuery( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, where, whereVal, orderBy); String[] displayFields = new String[] { MediaStore.Audio.Media.DISPLAY_NAME }; 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)) { 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(android.content.Intent.ACTION_VIEW); File newFile = new File(audioFilePath); intent.setDataAndType(Uri.fromFile(newFile), mimeType); startActivity(intent); } } } }
源码下载: