Android&读取Sdcard中的歌曲,ListView操作
music.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout android:layout_width="fill_parent" 3 android:layout_height="wrap_content" 4 android:orientation="horizontal" 5 xmlns:android="http://schemas.android.com/apk/res/android"> 6 <ImageView android:id="@+id/img" 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 /> 10 <LinearLayout android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:orientation="vertical"> 13 <TextView android:id="@+id/title" 14 android:layout_width="170px" 15 android:layout_height="wrap_content" 16 /> 17 <TextView android:id="@+id/artist" 18 android:layout_width="wrap_content" 19 android:layout_height="150px" 20 /> 21 <TextView android:id="@+id/album" 22 android:layout_width="wrap_content" 23 android:layout_height="150px" 24 /> 25 </LinearLayout>" 26 </LinearLayout>
activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" 10 android:orientation="vertical" > 11 12 <TextView 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:text="歌曲列表" /> 16 <ListView android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:id="@+id/musiclist"/> 19 </LinearLayout>
MainActivity.java
1 package com.zachary.simpleadapter; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.database.Cursor; 11 import android.os.Bundle; 12 import android.provider.MediaStore; 13 import android.util.Log; 14 import android.view.Menu; 15 import android.view.View; 16 import android.widget.AdapterView; 17 import android.widget.AdapterView.OnItemClickListener; 18 import android.widget.ListView; 19 import android.widget.SimpleAdapter; 20 import android.widget.TextView; 21 22 import com.zachary.mediaplayer.MediaPlayerService; 23 24 public class MainActivity extends Activity { 25 private HashMap<String, MusicInfo> MusicInfos; 26 private ListView MusicList; 27 final static String TAG = "LOGCAT"; 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 MusicInfos = new HashMap<String, MusicInfo>(); 33 MusicList = (ListView)findViewById(R.id.musiclist); 34 Log.i(TAG, "begin to showList"); 35 showList(); 36 MusicList.setOnItemClickListener( 37 new OnItemClickListener() { 38 39 @Override 40 public void onItemClick(AdapterView<?> arg0, View arg1, 41 int arg2, long arg3) { 42 Log.i(TAG, "MusicList onItemClickListener"); 43 String title = ((TextView)arg1.findViewById(R.id.title)).getText().toString(); 44 Log.i(TAG, title); 45 MusicInfo nowSong = MusicInfos.get(title); 46 String path = nowSong.getUrl(); 47 Log.i(TAG, path); 48 49 Intent intent = new Intent(MainActivity.this, MediaPlayerService.class); 50 intent.putExtra("path", path); 51 MainActivity.this.startService(intent); 52 } 53 } 54 55 ); 56 } 57 58 @Override 59 public boolean onCreateOptionsMenu(Menu menu) { 60 // Inflate the menu; this adds items to the action bar if it is present. 61 getMenuInflater().inflate(R.menu.main, menu); 62 return true; 63 } 64 65 public void showList(){ 66 Cursor mAudioCursor = this.getContentResolver().query( 67 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 68 null, 69 null, 70 null, 71 MediaStore.Audio.AudioColumns.TITLE); 72 List<Map<String,Object>> mListData = new ArrayList<Map<String, Object>>(); 73 for(int i=0; i<mAudioCursor.getCount(); i++) { 74 mAudioCursor.moveToNext(); 75 MusicInfo mi = new MusicInfo( mAudioCursor.getString(mAudioCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE)), 76 mAudioCursor.getString(mAudioCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST)), 77 mAudioCursor.getString(mAudioCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM)), 78 mAudioCursor.getString(mAudioCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA)) 79 ); 80 HashMap<String, Object> nowMap = new HashMap<String, Object>(); 81 nowMap.put("img",R.drawable.ic_launcher ); 82 nowMap.put("title", mi.getTitle()); 83 nowMap.put("artist", mi.getArtist()); 84 nowMap.put("album", mi.getAlbum()); 85 mListData.add(nowMap); 86 MusicInfos.put(mi.getTitle(),mi); 87 } 88 SimpleAdapter musicSA = new SimpleAdapter( 89 this, 90 mListData, 91 R.layout.music, 92 new String[]{"img", "title", "artist", "album"}, 93 new int[] {R.id.img, R.id.title, R.id.artist, R.id.album} 94 ); 95 MusicList.setAdapter(musicSA); 96 } 97 }
MusicInfo.java
1 package com.zachary.simpleadapter; 2 3 /** 4 * @author Zachary 5 * 6 */ 7 public class MusicInfo { 8 private String title; 9 private String artist; 10 private String album; 11 private String url; 12 public MusicInfo(String title, String artist, String album, String url){ 13 this.title = title; 14 this.artist =artist; 15 this.album = album; 16 this.url = url; 17 } 18 19 public String getTitle() { 20 return title; 21 } 22 public void setTitle(String title) { 23 this.title = title; 24 } 25 26 public String getArtist() { 27 return artist; 28 } 29 public void setArtist(String artist) { 30 this.artist = artist; 31 } 32 public String getAlbum() { 33 return album; 34 } 35 public void setAlbum(String album) { 36 this.album = album; 37 } 38 public String getUrl() { 39 return url; 40 } 41 public void setUrl(String url) { 42 this.url = url; 43 } 44 45 @Override 46 public String toString() { 47 return "MusicInfo [title=" + title + ", artist=" 48 + artist + ", album=" + album +", url=" + url +", getClass()=" + getClass() 49 + "]"; 50 } 51 52 }
MusicPlayerSerivice.java
1 package com.zachary.mediaplayer; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaPlayer; 6 import android.net.Uri; 7 import android.os.IBinder; 8 import android.util.Log; 9 import android.widget.Toast; 10 11 public class MediaPlayerService extends Service { 12 13 private static final String TAG = "Media Player Service"; 14 private MediaPlayer player = null; 15 @Override 16 public IBinder onBind(Intent arg0) { 17 // TODO Auto-generated method stub 18 return null; 19 } 20 21 @Override 22 public void onCreate() { 23 Toast.makeText(this, "MediaPlayerService Created", Toast.LENGTH_LONG).show(); 24 Log.i(TAG, "onCreate"); 25 System.out.println("onCreate"); 26 } 27 28 @Override 29 public void onStart(Intent intent, int startId) { 30 Toast.makeText(this, "MediaPlayerService Start", Toast.LENGTH_LONG).show(); 31 32 String path = intent.getStringExtra("path"); 33 34 Log.i(TAG, "onStart"); 35 System.out.println("onStart"); 36 Log.i(TAG, path); 37 System.out.println(path); 38 Uri playerUri = Uri.parse(path); 39 if (player != null) { 40 player.stop(); 41 } 42 player = MediaPlayer.create(MediaPlayerService.this, playerUri); 43 player.start(); 44 } 45 46 @Override 47 public void onDestroy() { 48 super.onDestroy(); 49 Toast.makeText(this, "MediaPlayerService Stoped", Toast.LENGTH_LONG).show(); 50 Log.i(TAG, "onDestory"); 51 System.out.println("onDestory"); 52 if(player != null) { 53 player.release(); 54 player = null; 55 } 56 } 57 58 }
posted on 2013-04-18 21:07 Zachary_wiz 阅读(328) 评论(0) 编辑 收藏 举报