android 高仿多米音乐播放器 (有图有码有真相)
半年前写了个音乐播放器,仿的是多米的UI界面
之前发表在eoe社区,今天也发到****上来
不罗嗦,先上效果图:
下面简单介绍下代码:
MusicPlayer播放音乐的核心类,该类有以下成员变量
privatefinalstaticStringTAG="MusicPlayer";
privatefinalStringBROCAST_NAME="com.genius.musicplay.brocast";
privateMediaPlayermMediaPlayer;//播放器对象
privateList<MusicData>mMusicFileList;//音乐文件列表
privateintmCurPlayIndex;//当前播放索引
privateintmPlayState;//播放器状态
privateintmPLayMode;//歌曲播放模式
privateRandommRandom;
privateContextmContext;
其实该类就是将MediaPlayer做了一层封装,提供出一些播放音乐的相关操作如播放,暂停,上一曲,下一曲等等
MusicService是播放音乐的服务继承自service,MediaPlayer作为它的成员变量
所以对MediaPlayer的操作就委托给了MusicService
ServiceManager,管理服务的对象,采用AIDL接口方式实现远程操作,对AIDL熟悉的童鞋你懂得
privatefinalstaticStringTAG="ServiceManager";
privatefinalstaticStringSERVICE_NAME="com.genius.service.musicservices";
privateBooleanmConnectComplete;
privateServiceConnectionmServiceConnection;
privateMusicConnectmMusicConnect;
privateIOnServiceConnectCompletemIOnServiceConnectComplete;
privateContextmContext;
MusicConnect就是AIDL接口对象
MusicData音乐歌曲的信息,由于要在AIDL接口传递,所以需要实现Parcelable接口
publicclassMusicDataimplementsParcelable{
publicfinalstaticStringKEY_MUSIC_DATA="MusicData";
privatefinalstaticStringKEY_MUSIC_NAME="MusicName";
privatefinalstaticStringKEY_MUSIC_TIME="MusicTime";
privatefinalstaticStringKEY_MUSIC_PATH="MusicPath";
privatefinalstaticStringKEY_MUSIC_ARITST="MusicAritst";
publicStringmMusicName;
publicintmMusicTime;
publicStringmMusicPath;
publicStringmMusicAritst;
publicMusicData()
{
mMusicName="";
mMusicTime=0;
mMusicPath="";
mMusicAritst="";
}
@Override
publicintdescribeContents(){
//TODOAuto-generatedmethodstub
return0;
}
@Override
publicvoidwriteToParcel(Parceldest,intflags){
//TODOAuto-generatedmethodstub
BundlemBundle=newBundle();
mBundle.putString(KEY_MUSIC_NAME,mMusicName);
mBundle.putInt(KEY_MUSIC_TIME,mMusicTime);
mBundle.putString(KEY_MUSIC_PATH,mMusicPath);
mBundle.putString(KEY_MUSIC_ARITST,mMusicAritst);
dest.writeBundle(mBundle);
}
publicstaticfinalParcelable.Creator<MusicData>CREATOR=newParcelable.Creator<MusicData>()
{
@Override
publicMusicDatacreateFromParcel(Parcelsource){
//TODOAuto-generatedmethodstub
MusicDataData=newMusicData();
BundlemBundle=newBundle();
mBundle=source.readBundle();
Data.mMusicName=mBundle.getString(KEY_MUSIC_NAME);
Data.mMusicTime=mBundle.getInt(KEY_MUSIC_TIME);
Data.mMusicPath=mBundle.getString(KEY_MUSIC_PATH);
Data.mMusicAritst=mBundle.getString(KEY_MUSIC_ARITST);
returnData;
}
@Override
publicMusicData[]newArray(intsize){
//TODOAuto-generatedmethodstub
returnnewMusicData[size];
}
};
}
音乐播放模式类
publicclassMusicPlayMode{
publicstaticfinalintMPM_SINGLE_LOOP_PLAY=0;//单曲循环
publicstaticfinalintMPM_ORDER_PLAY=1;//顺序播放
publicstaticfinalintMPM_LIST_LOOP_PLAY=2;//列表循环
publicstaticfinalintMPM_RANDOM_PLAY=3;//随即播放
}
音乐播放状态类
publicclassMusicPlayState{
publicstaticfinalintMPS_NOFILE=-1;//无音乐文件
publicstaticfinalintMPS_INVALID=0;//当前音乐文件无效
publicstaticfinalintMPS_PREPARE=1;//准备就绪
publicstaticfinalintMPS_PLAYING=2;//播放中
publicstaticfinalintMPS_PAUSE=3;//暂停
publicstaticfinalStringPLAY_STATE_NAME="PLAY_STATE_NAME";
publicstaticfinalStringPLAY_MUSIC_INDEX="PLAY_MUSIC_INDEX";
publicstaticfinalStringMUSIC_INVALID="MUSIC_INVALID";
publicstaticfinalStringMUSIC_PREPARE="MUSIC_PREPARE";
publicstaticfinalStringMUSIC_PLAY="MUSIC_PLAY";
publicstaticfinalStringMUSIC_PAUSE="MUSIC_PAUSE";
publicstaticfinalStringMUSIC_STOP="MUSIC_STOP";
}
MusicPlayActivity,应用程序主界面,该类代码也最多,不过里面细分了几个内部类,如UIManager里面就包含了很多控件对象以及对它们的操作
privatefinalstaticStringTAG="MusicPlayActivity";
privatefinalStringBROCAST_NAME="com.genius.musicplay.brocast";
privatefinalstaticintREFRESH_PROGRESS_EVENT=0x100;
privatefinalstaticintABOUT_DIALOG_ID=1;
privateHandlermHandler;
privateUIManagermUIManager;
privateServiceManagermServiceManager;//本地服务管理
privateMusicTimermMusicTimer;//检测歌曲进度的定时器
privateMusicPlayStateBrocastmPlayStateBrocast;//音乐播放状态的广播接收器
privateSDStateBrocastmSDStateBrocast;//SD卡变化接收器
privateList<MusicData>m_MusicFileList;//音乐列表
privateListViewAdaptermListViewAdapter;
privatebooleanmIsSdExist=false;//SD卡是否存在
privatebooleanmIsHaveData=false;//是否有音乐文件
privateintmCurMusicTotalTime=0;
privateintmCurPlayMode=MusicPlayMode.MPM_LIST_LOOP_PLAY;
MySlidingDrawer,自定义抽屉控件,属于UIManager的一部分
MusicTimer,用于定时获取歌曲进度的定时器
其它的东东就不多说了,有兴趣的童鞋自个儿下代码(有注释)看吧