欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

教你輕松制作Android音樂播放器

 更新時間:2015年11月10日 09:20:07   投稿:lijiao  
這篇文章主要教大家輕松制作Android音樂播放器,制作一款屬于自己的Android音樂播放器,希望大家喜歡。

欣賞一下我們清爽的界面吧~

如果是只用activity來制作這樣的東西簡直是太小兒科了,此處我們當(dāng)然用的是service

首先我們先上service的代碼:

1、如果我們要訪問service的屬性和方法,那么在activity肯定是以bindservice的方法實現(xiàn)的,而在service中的onbind方法也是必須要實現(xiàn)的,onbind返回的Ibinder對象在activity的serviceconnection中得到使用。

2、activity獲取到Ibinder對象,可以進一步獲取服務(wù)對象和player對象,來進行訪問。

3、Environment.getExternalStorageDirectory()是獲取sd中的內(nèi)容的,不管是手機出場就已經(jīng)內(nèi)置的sd卡,還是用戶后來自己添加的sd卡;而getExternalFilesDir()獲取的真正是手機內(nèi)部的存儲空間,,/data/data/your_package/,隨著應(yīng)用的卸載存儲的文件會被刪除。

4、service通過發(fā)送廣播與activity進行界面交互

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("長度",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對象獲取服務(wù)對象
2、獲取到服務(wù)對象以后,再訪問服務(wù)的方法。
3、通過receiver刷新頁面

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的方式啟動服務(wù),這樣結(jié)束了activity以后并不會結(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)容請點擊《java播放器功能》進行學(xué)習(xí)。

以上就是制作Android音樂播放器的全部代碼,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Android開發(fā)之DiffUtil的使用詳解

    Android開發(fā)之DiffUtil的使用詳解

    這篇文章文給大家介紹了DiffUtil的使用,相信大家每位Android開發(fā)者們都知道谷歌最近更新了Support Library 24.2.0,而DiffUtil就是在這個版本添加的一個工具類。下面就跟著小編一起來看看,有需要的可以參考借鑒。
    2016-09-09
  • Android 中對JSON數(shù)據(jù)解析實例代碼

    Android 中對JSON數(shù)據(jù)解析實例代碼

    這篇文章主要介紹了Android 中對JSON數(shù)據(jù)解析實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • android自定義環(huán)形統(tǒng)計圖動畫

    android自定義環(huán)形統(tǒng)計圖動畫

    這篇文章主要為大家詳細介紹了android自定義環(huán)形統(tǒng)計圖動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android  Retrofit和Rxjava的網(wǎng)絡(luò)請求

    Android Retrofit和Rxjava的網(wǎng)絡(luò)請求

    這篇文章主要介紹了Android Retrofit和Rxjava的網(wǎng)絡(luò)請求的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android嵌套滾動與協(xié)調(diào)滾動的實現(xiàn)方式匯總

    Android嵌套滾動與協(xié)調(diào)滾動的實現(xiàn)方式匯總

    如何實現(xiàn)這種協(xié)調(diào)滾動的布局呢,我們使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior實現(xiàn),另一種方案是MotionLayout,我們看看都是怎么實現(xiàn)的吧
    2022-06-06
  • Android AsyncTask詳解及使用方法

    Android AsyncTask詳解及使用方法

    這篇文章主要介紹了 Android AsyncTask詳解及使用方法的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android自定義GestureDetector實現(xiàn)手勢ImageView

    Android自定義GestureDetector實現(xiàn)手勢ImageView

    這篇文章主要為大家詳細介紹了Android自定義GestureDetector實現(xiàn)手勢ImageView的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Listview的異步加載性能優(yōu)化

    Listview的異步加載性能優(yōu)化

    Android中ListView是使用平率最高的控件之一(GridView跟ListView是兄弟,都是繼承AbsListView),ListView優(yōu)化最有效的無非就是采用ViewHolder來減少頻繁的對view查詢和更新,緩存圖片加快解碼,減小圖片尺寸
    2016-01-01
  • Flutter中漸變色的使用案例分享

    Flutter中漸變色的使用案例分享

    在日常的開發(fā)中,UI為了讓界面更加吸引人往往會在界面上用到大量的漸變色,本文將通過幾個案例更好的去了解Flutter中漸變色的使用,需要的可以參考一下
    2023-06-06
  • MacBook M1 Flutter環(huán)境搭建的實現(xiàn)步驟

    MacBook M1 Flutter環(huán)境搭建的實現(xiàn)步驟

    本文主要介紹了MacBook M1 Flutter環(huán)境搭建,F(xiàn)lutter官方和各項配套的軟件環(huán)境也還沒有成熟,導(dǎo)致搭建環(huán)境時碰到了不少坑,本文就詳細的介紹一下
    2021-08-08

最新評論