。java
。android
在MusicManager設置一個ListView成員變量做爲當前播放的控件。數據庫
MusicManager包括各類狀態以及操做,比方播放音樂。隨機一個音樂。下一曲等等。api
package com.huwei.sweetmusicplayer.datamanager; import java.util.ArrayList; import java.util.List; import java.util.Random; import android.content.Context; import android.content.Intent; import android.provider.MediaStore; import android.view.View; import android.widget.BaseAdapter; import android.widget.ListView; import com.huwei.sweetmusicplayer.enums.MusicState; import com.huwei.sweetmusicplayer.models.Song; import com.huwei.sweetmusicplayer.ui.fragments.PlayingFragment; public class MusicManager { private int nowplaying_index=-1; public static boolean isPlaying=false; public static final String BOTTOMACTIONBAR_UPDATE="BOTTOMACTIONBAR_UPDATE"; public static final String BOTTOMACTIONBAR_CHECKED="BOTTOMACTIONBAR_CHECKED"; public static final String BOTTOMACTIONBAR_UNCHECKED="BOTTOMACTIONBAR_UNCHECKED"; public static final String PLAY_BTN_CHECKED="PLAY_BTN_CHECKED"; public static final String PLAY_BTN_UNCHECKED="PLAY_BTN_UNCHECKED"; private static MusicManager instance; private Context context; private List<Song> songLists=new ArrayList<Song>(); //啓動應用從數據庫都進來時候的列表 private ListView nowPlayingListView; //正在播放音樂的列表控件 /** * 定義查找音樂信息數組。0.標題,1音樂時間,2.藝術家,3.音樂id,4.顯示名字,5.全路徑,6,專輯ID,7最後加入時間。 */ public static String[] media_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 ,MediaStore.MediaColumns.DATE_ADDED}; public static MusicManager getInstance(){ if(null==instance){ instance=new MusicManager(); } return instance; } //加入song到songLists public void addSong(Song song){ songLists.add(song); } public void bind(Context context){ this.context=context; } public void play(){ Intent intent=new Intent(); Song song=(Song) nowPlayingListView.getAdapter().getItem(nowplaying_index); intent.setAction("com.huwei.sweetmusicplayer.services.LocalMusicService"); intent.putExtra("song", song); intent.putExtra("op", MusicState.PLAYING.ordinal()); //播放選項 context.startService(intent); //通知playButton Intent intent3=new Intent(PLAY_BTN_CHECKED); context.sendBroadcast(intent3); isPlaying=true; } //先中止,再播放 public void restartPlay(){ stopUnNotifyView(); play(); } public void previousSong(){ setNowplaying_index(nowplaying_index-1); restartPlay(); } public void nextSong(){ //都要用set方法。便於通知更新視圖 setNowplaying_index(nowplaying_index+1); restartPlay(); } //隨機播放一首歌 public void random_a_song(){ getRandom_song(); restartPlay(); } public void pause(){ Intent intent=new Intent(); intent.setAction("com.huwei.sweetmusicplayer.services.LocalMusicService"); intent.putExtra("op", MusicState.PAUSE.ordinal()); //播放選項 context.startService(intent); isPlaying=false; //通知playButton Intent intent3=new Intent(PLAY_BTN_UNCHECKED); context.sendBroadcast(intent3); } //發送中止音樂廣播,但是不更新視圖 public void stopUnNotifyView(){ Intent intent=new Intent(); intent.setAction("com.huwei.sweetmusicplayer.services.LocalMusicService"); intent.putExtra("op", MusicState.STOP.ordinal()); //播放選項 context.startService(intent); } //中止音樂 public void stop(){ stopUnNotifyView(); } //設置音樂進度 public void setProgress(int progress){ Intent intent=new Intent("com.huwei.sweetmusicplayer.services.LocalMusicService"); intent.putExtra("progress", progress); intent.putExtra("op", MusicState.PROGRESS_CHANGE.ordinal()); context.startService(intent); } //隨機獲得一首歌 public int getRandom_song(){ int random=new Random().nextInt(nowPlayingListView.getAdapter().getCount()); setNowplaying_index(random); return nowplaying_index ; } public List<Song> getSongLists() { return songLists; } public int getNowplaying_index() { return nowplaying_index; } public void setNowplaying_index(int i) { int count=nowPlayingListView.getCount(); nowplaying_index=(i+count)%count; //通知ListView控件刷新 BaseAdapter adapter=(BaseAdapter) nowPlayingListView.getAdapter(); adapter.notifyDataSetChanged(); //通知bottomActionBar更新 Intent intent=new Intent(BOTTOMACTIONBAR_UPDATE); context.sendBroadcast(intent); //通知PlaySong界面更新 Intent intent2=new Intent(PlayingFragment.PLAYSONG_UPDATE); context.sendBroadcast(intent2); } public ListView getNowPlayingListView() { return nowPlayingListView; } public void setNowPlayingListView(ListView nowPlayingListView) { this.nowPlayingListView = nowPlayingListView; } public Song getNowPlayingSong(){ return (Song) nowPlayingListView.getAdapter().getItem(nowplaying_index); } //歌詞界面操做狀態 public static class OperateState{ public static String READLRC_SUCCESS="READLRC_SUCCESS"; public static String READLRC_LISTNULL="READLRC_LISTNULL"; public static String READLRC_ONLINE="READLRC_ONLINE"; public static String READLRCFILE_FAIL="READLRCFILE_FAIL"; public static String READLRCONLINE_FAIL="READLRCONLINE_FAIL"; } public Song getSongByIndex(int index){ return (Song) nowPlayingListView.getAdapter().getItem(index); } }