歌曲列表是来显示SD卡或手机内存中的歌曲,因为android会自动扫描媒体对象,直接使用MediaStore就可以显示歌曲名称、艺术家、缩略图等。再次使用ListView来显示这些信息,xml布局很简单就是一个ListView:
[html]
<SPAN style="FONT-SIZE: 14px"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/music_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/listbg"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout></SPAN><PRE class=java name="code"><SPAN style="FONT-SIZE: 14px">
</SPAN></PRE>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/music_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/listbg"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>[java] view plaincopyprint?<SPAN style="FONT-SIZE: 14px"> </SPAN>
既然使用ListView来显示歌曲信息,例如歌曲标题、歌手、缩略图以及时长等等。那么如何获取这些信息以及如何显示到ListView中呢?要分为两个步骤:
(1)使用MediaStore获取歌曲信息
(2)自定义Adapter将MediaStore获取歌曲信息显示出来
[html]
/**
*
*/
package com.android.musicplayer.activity;
import java.io.File;
import com.android.musicplayer.adapter.MusicListAdapter;
import com.android.musicplayer.util.ScanSdReceiver;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInte易做图ce;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.AudioManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
/**
* @author Administrator
*
*/
public class SongsActivity extends Activity {
private int[] _ids;// 保存音乐ID临时数组
private String[] _artists;// 保存艺术家
private String[] _titles;// 标题临时数组
private ListView listView;// 列表对象
private Cursor cursor;
private MusicListAdapter musicListAdapter;
private AlertDialog ad = null;
private AlertDialog.Builder builder = null;
private static final String MUSIC_LIST = "com.android.musicplayer.activity.list";
/**
* 定义查找音乐信息数组,1.标题 2.音乐时间 3.艺术家 4.音乐id 5.显示名字 6.数据
*/
String[] media_music_info = new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
listView = (ListView) findViewById(R.id.music_list);
listView.setOnItemClickListener(new MusicListOnClickListener());// 创建一个ListView易做图对象
showMusicList();
}
private void showMusicList() {
cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, media_music_info,
null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
cursor.moveToFirst();// 将游标移动到初始位置
_ids = new int[cursor.getCount()];// 返回int的一个列
_artists = new String[cursor.getCount()];// 返回String的一个列
_titles = new String[cursor.getCount()];// 返回String的一个列
for (int i = 0; i < cursor.getCount(); i++) {
_ids[i] = cursor.getInt(3);
_titles[i] = cursor.getString(0);
_artists[i] = cursor.getString(2);
cursor.moveToNext();// 将游标移到下一行 &nbs
补充:移动开发 , Android ,