編輯:關於Android編程
需求1:將內存卡中的MP3音樂讀取出來並顯示到列表當中
1.從數據庫中查詢所有音樂數據,保存到List集合當中,List當中存放的是Mp3Info對象
2.迭代List集合,把每一個Mp3Info對象的所有屬性,保存到Map對象當中
3.定義一個List集合,把Map對象添加到List集合當中
4.通過定義一個SimpleAdpter,調用setAdpter方法,將數據顯示到列表當中
/** * 用於從數據庫中查詢歌曲的信息,保存在List當中 * * @return */ public List<Mp3Info> getMp3Infos() { Cursor cursor = getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>(); for (int i = 0; i < cursor.getCount(); i++) { Mp3Info mp3Info = new Mp3Info(); cursor.moveToNext(); long id = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media._ID)); //音樂id String title = cursor.getString((cursor .getColumnIndex(MediaStore.Audio.Media.TITLE)));//音樂標題 String artist = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.ARTIST));//藝術家 long duration = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.DURATION));//時長 long size = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.SIZE)); //文件大小 String url = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA)); //文件路徑 int isMusic = cursor.getInt(cursor .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否為音樂 if (isMusic != 0) { //只把音樂添加到集合當中 mp3Info.setId(id); mp3Info.setTitle(title); mp3Info.setArtist(artist); mp3Info.setDuration(duration); mp3Info.setSize(size); mp3Info.setUrl(url); mp3Infos.add(mp3Info); } } return mp3Infos; }
/** * 填充列表 * @param mp3Infos */ public void setListAdpter(List<Mp3Info> mp3Infos) { List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>(); for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) { Mp3Info mp3Info = (Mp3Info) iterator.next(); HashMap<String, String> map = new HashMap<String, String>(); map.put("title", mp3Info.getTitle()); map.put("Artist", mp3Info.getArtist()); map.put("duration", String.valueOf(mp3Info.getDuration())); map.put("size", String.valueOf(mp3Info.getSize())); map.put("url", mp3Info.getUrl()); mp3list.add(map); } mAdapter = new SimpleAdapter(this, mp3list, R.layout.music_list_item_layout, new String[] { "title", "Artist", "duration" }, new int[] { R.id.music_title, R.id.music_Artist, R.id.music_duration }); mMusiclist.setAdapter(mAdapter); }
實現效果如圖:
需求2:點擊列表的每一項,啟動一個服務來播放音樂
1. 注冊條目點擊監聽器OnItemClickListener
2. 定義一個實現OnItemClickListener接口的內部類
3. 定義一個繼承Service的服務類PlayerServie
1. 注冊條目點擊監聽器OnItemClickListener
mMusiclist = (ListView) findViewById(R.id.music_list); mMusiclist.setOnItemClickListener(new MusicListItemClickListener()); mp3Infos = getMp3Infos(); setListAdpter(getMp3Infos());
2. 定義一個實現OnItemClickListener接口的內部類
private class MusicListItemClickListener implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(mp3Infos != null) { Mp3Info mp3Info = mp3Infos.get(position); Log.d("mp3Info-->", mp3Info.toString()); Intent intent = new Intent(); intent.putExtra("url", mp3Info.getUrl()); intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG); intent.setClass(MainActivity.this, PlayerService.class); startService(intent); //啟動服務 } } }
3. 定義一個繼承Service的服務類PlayerServie(未完善的Service類)
package com.wwj.sb.service; import android.annotation.SuppressLint; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.IBinder; import com.wwj.sb.domain.AppConstant; @SuppressLint("NewApi") public class PlayerService extends Service { private MediaPlayer mediaPlayer = new MediaPlayer(); //媒體播放器對象 private String path; //音樂文件路徑 private boolean isPause; //暫停狀態 @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { if(mediaPlayer.isPlaying()) { stop(); } path = intent.getStringExtra("url"); int msg = intent.getIntExtra("MSG", 0); if(msg == AppConstant.PlayerMsg.PLAY_MSG) { play(0); } else if(msg == AppConstant.PlayerMsg.PAUSE_MSG) { pause(); } else if(msg == AppConstant.PlayerMsg.STOP_MSG) { stop(); } return super.onStartCommand(intent, flags, startId); } /** * 播放音樂 * @param position */ private void play(int position) { try { mediaPlayer.reset();//把各項參數恢復到初始狀態 mediaPlayer.setDataSource(path); mediaPlayer.prepare(); //進行緩沖 mediaPlayer.setOnPreparedListener(new PreparedListener(position));//注冊一個監聽器 } catch (Exception e) { e.printStackTrace(); } } /** * 暫停音樂 */ private void pause() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); isPause = true; } } /** * 停止音樂 */ private void stop(){ if(mediaPlayer != null) { mediaPlayer.stop(); try { mediaPlayer.prepare(); // 在調用stop後如果需要再次通過start進行播放,需要之前調用prepare函數 } catch (Exception e) { e.printStackTrace(); } } } @Override public void onDestroy() { if(mediaPlayer != null){ mediaPlayer.stop(); mediaPlayer.release(); } } /** * * 實現一個OnPrepareLister接口,當音樂准備好的時候開始播放 * */ private final class PreparedListener implements OnPreparedListener { private int positon; public PreparedListener(int positon) { this.positon = positon; } @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); //開始播放 if(positon > 0) { //如果音樂不是從頭播放 mediaPlayer.seekTo(positon); } } } }
以上代碼實現了兩個需求,顯示音樂列表和實現列表點擊事件啟動Servic。下次會實現控制音樂播放的需求。希望對大家的學習有所幫助,也希望大家多多支持本站。
Android系統 小米,三星,索尼手機發送桌面快鍵提醒數字圖標,在Android系統中,眾所周知不支持BadgeNumber,雖然第三方控件BadgeView可以實現應
main.xml custom_dialog.xml package com.example.p
現在,熱修復的具體實現方案開源的也有很多,原理也大同小異,本篇文章以Nuwa為例,深入剖析。Nuwa的github地址https://github.com/jasonro
實現效果:圖片素材: --> 首先, 寫先下拉刷新時的刷新布局 pull_to_refresh.xml:<resources> <string
錯誤日志:Error:java.lang.RuntimeExceptio