教你輕松制作Android音樂播放器
欣賞一下我們清爽的界面吧~
如果是只用activity來制作這樣的東西簡(jiǎn)直是太小兒科了,此處我們當(dāng)然用的是service
首先我們先上service的代碼:
1、如果我們要訪問service的屬性和方法,那么在activity肯定是以bindservice的方法實(shí)現(xiàn)的,而在service中的onbind方法也是必須要實(shí)現(xiàn)的,onbind返回的Ibinder對(duì)象在activity的serviceconnection中得到使用。
2、activity獲取到Ibinder對(duì)象,可以進(jìn)一步獲取服務(wù)對(duì)象和player對(duì)象,來進(jìn)行訪問。
3、Environment.getExternalStorageDirectory()是獲取sd中的內(nèi)容的,不管是手機(jī)出場(chǎng)就已經(jīng)內(nèi)置的sd卡,還是用戶后來自己添加的sd卡;而getExternalFilesDir()獲取的真正是手機(jī)內(nèi)部的存儲(chǔ)空間,,/data/data/your_package/,隨著應(yīng)用的卸載存儲(chǔ)的文件會(huì)被刪除。
4、service通過發(fā)送廣播與activity進(jìn)行界面交互
public class MusicService extends Service{ private List<File> musicList; private MediaPlayer player; private int curPage; public static final String MFILTER = "broadcast.intent.action.text"; public static final String NAME = "name"; public static final String TOTALTIME = "totaltime"; public static final String CURTIME = "curtime"; @Override public IBinder onBind(Intent intent) {//1 // TODO Auto-generated method stub return new MBinder(); } public class MBinder extends Binder{//2 public MusicService getService(){ return MusicService.this; } public MediaPlayer getPlayer(){ return player; } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); musicList = new ArrayList<File>(); File rootDir = Environment.getExternalStorageDirectory();//3 Log.d("rootname",rootDir.getName()); Log.d("rootname",rootDir.getAbsolutePath()); fillMusicList(rootDir); Log.d("musiclist",String.valueOf(musicList.size())); player = new MediaPlayer(); if (musicList.size() != 0) { startPlay(); } player.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub player.reset(); curPage = curPage==musicList.size()-1? (curPage+1)%musicList.size() : curPage+1; startPlay(); } }); } /*迭代獲取 音樂 文件*/ private void fillMusicList(File dir){ File[] sourceFiles = dir.listFiles(); Log.d("長(zhǎng)度",String.valueOf(sourceFiles.length)); for(File file : sourceFiles){ if (file.isDirectory()) { Log.d("文件夾名稱",String.valueOf(file.getName())); // if (!file.getName().equals("lost+found")) { fillMusicList(file); // } } else { String name = file.getName(); Log.d("childname",file.getName()); if (name.endsWith(".mp3")||name.endsWith(".acc")) {//支持的格式 musicList.add(file); } } } } private void startPlay(){ mSendBroadCast(NAME,musicList.get(curPage).getName());//4 try { player.setDataSource(musicList.get(curPage).getAbsolutePath()); player.prepare(); player.start(); player.getDuration(); mSendBroadCast(TOTALTIME,player.getDuration()); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub mSendBroadCast(CURTIME,player.getCurrentPosition()); } },0,1000); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void playNext(){ curPage = curPage==musicList.size()-1? (curPage+1)%musicList.size() : curPage+1; Log.d("curpage",String.valueOf(curPage)); player.reset(); startPlay(); } public void playPrevious(){ curPage = curPage==0? 0 : curPage-1; Log.d("curpage",String.valueOf(curPage)); player.reset(); startPlay(); } public void parse(){ player.pause(); } public void restart(){ player.start(); } private void mSendBroadCast(String key, String value){ Intent intent = new Intent(MFILTER); intent.putExtra(key,value);//發(fā)送廣播 sendBroadcast(intent); } private void mSendBroadCast(String key, int value){ Intent intent = new Intent(MFILTER); intent.putExtra(key,value);//發(fā)送廣播 sendBroadcast(intent); } }
接下來上activity代碼:
1、通過Ibinder對(duì)象獲取服務(wù)對(duì)象
2、獲取到服務(wù)對(duì)象以后,再訪問服務(wù)的方法。
3、通過receiver刷新頁(yè)面
public class MainActivity extends Activity implements OnClickListener{ SeekBar seekBar; TextView curTime,totalTime; TextView title; private ServiceConnection sc; private MusicService ms; private boolean isStop; private double totalTimeInt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(MusicService.MFILTER); registerReceiver(new MusicReceiver(),filter); sc = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub ms = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub ms = ((MBinder)service).getService();//1 } }; Button previous = (Button) findViewById(R.id.previous); Button next = (Button) findViewById(R.id.next); Button stop = (Button) findViewById(R.id.stop); Button stopService = (Button) findViewById(R.id.stopService); seekBar = (SeekBar) findViewById(R.id.mSeekbar); curTime = (TextView) findViewById(R.id.curTime); totalTime = (TextView) findViewById(R.id.totalTime); title = (TextView) findViewById(R.id.title); previous.setOnClickListener(this); next.setOnClickListener(this); stop.setOnClickListener(this); stopService.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.previous: ms.playPrevious();//2 break; case R.id.next: ms.playNext(); break; case R.id.stop: if (isStop) { ms.restart(); } else { ms.parse(); } isStop = !isStop; break; case R.id.stopService: Intent intent = new Intent("com.intent.musicplayer.MusicService"); unbindService(sc); stopService(intent); break; default: break; } } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Intent intent = new Intent("com.intent.musicplayer.MusicService"); bindService(intent,sc,Context.BIND_AUTO_CREATE);//當(dāng)然你可以用startService的方式啟動(dòng)服務(wù),這樣結(jié)束了activity以后并不會(huì)結(jié)束service } private String transferMilliToTime(int millis){ DateFormat format = new SimpleDateFormat("mm:ss"); String result = format.format(new Date(millis)); return result; } private class MusicReceiver extends BroadcastReceiver{//3 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getIntExtra(MusicService.CURTIME,0)!=0) { double curTimeInt = intent.getIntExtra(MusicService.CURTIME,0); curTime.setText(transferMilliToTime((int)curTimeInt)); double result = curTimeInt/totalTimeInt*100; seekBar.setProgress((int) Math.floor(result)); } else if(intent.getIntExtra(MusicService.TOTALTIME,0)!=0) { totalTimeInt = intent.getIntExtra(MusicService.TOTALTIME,0); totalTime.setText(transferMilliToTime((int)(totalTimeInt))); } else if (!TextUtils.isEmpty(intent.getStringExtra(MusicService.NAME))) { title.setText(intent.getStringExtra(MusicService.NAME)); } } } }
4、最后附上xml布局文件,算是代碼上傳完全了:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="25sp" android:textColor="#444444" /> <SeekBar android:id="@+id/mSeekbar" android:layout_gravity="center_horizontal" android:layout_width="400dp" android:layout_height="wrap_content" android:max="100" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/curTime" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentLeft="true" /> <TextView android:id="@+id/totalTime" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/previous" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="上一曲" android:layout_alignParentLeft="true" /> <Button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="停止音樂" android:layout_toRightOf="@id/previous" /> <Button android:id="@+id/next" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="下一曲" android:layout_alignParentRight="true" /> <Button android:id="@+id/stopService" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="停止音樂服務(wù)" android:layout_toLeftOf="@id/next" /> </RelativeLayout> </LinearLayout>
更多關(guān)于播放器的內(nèi)容請(qǐng)點(diǎn)擊《java播放器功能》進(jìn)行學(xué)習(xí)。
以上就是制作Android音樂播放器的全部代碼,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android實(shí)現(xiàn)簡(jiǎn)單音樂播放器(MediaPlayer)
- Android簡(jiǎn)易音樂播放器實(shí)現(xiàn)代碼
- android暫?;蛲V蛊渌魳凡シ牌鞯牟シ艑?shí)現(xiàn)代碼
- Android編程開發(fā)音樂播放器實(shí)例
- Android音樂播放器制作 掃描本地音樂顯示在手機(jī)(一)
- android實(shí)現(xiàn)音樂播放器進(jìn)度條效果
- Android MediaPlayer實(shí)現(xiàn)音樂播放器實(shí)例代碼
- 簡(jiǎn)單實(shí)現(xiàn)Android本地音樂播放器
- Android 音樂播放器的開發(fā)實(shí)例詳解
- Android實(shí)現(xiàn)簡(jiǎn)單的音樂播放器
相關(guān)文章
Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼
這篇文章主要介紹了Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03android自定義環(huán)形統(tǒng)計(jì)圖動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了android自定義環(huán)形統(tǒng)計(jì)圖動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求
這篇文章主要介紹了Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2017-03-03Android嵌套滾動(dòng)與協(xié)調(diào)滾動(dòng)的實(shí)現(xiàn)方式匯總
如何實(shí)現(xiàn)這種協(xié)調(diào)滾動(dòng)的布局呢,我們使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior實(shí)現(xiàn),另一種方案是MotionLayout,我們看看都是怎么實(shí)現(xiàn)的吧2022-06-06Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView
這篇文章主要為大家詳細(xì)介紹了Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03MacBook M1 Flutter環(huán)境搭建的實(shí)現(xiàn)步驟
本文主要介紹了MacBook M1 Flutter環(huán)境搭建,F(xiàn)lutter官方和各項(xiàng)配套的軟件環(huán)境也還沒有成熟,導(dǎo)致搭建環(huán)境時(shí)碰到了不少坑,本文就詳細(xì)的介紹一下2021-08-08