音樂播放器是一個(gè)非常常見的應(yīng)用,這篇博客就是介紹如何制作一個(gè)簡(jiǎn)單的音樂播放器,這款音樂播放器具有以下的功能:播放歌曲、暫停播放歌曲、、顯示歌曲的總時(shí)長(zhǎng)、顯示歌曲的當(dāng)前播放時(shí)長(zhǎng)、調(diào)節(jié)滑塊可以將歌曲調(diào)節(jié)到任何時(shí)間播放、退出音樂播放器。
實(shí)現(xiàn)效果如下
	
實(shí)現(xiàn)方式:
第一步:使用Android Studio創(chuàng)建一個(gè)Android工程,并且修改activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.fyt.musicplayer.MainActivity" android:orientation="vertical"> <!--顯示播放進(jìn)度--> <SeekBar android:id="@+id/sb" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!--顯示當(dāng)前進(jìn)度--> <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00"/> <!--顯示總進(jìn)度--> <TextView android:id="@+id/tv_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="00:00"/> </RelativeLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放音樂" android:onClick="play"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暫停播放" android:onClick="pausePlay"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="繼續(xù)播放" android:onClick="continuePlay"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出" android:onClick="exit"/> </LinearLayout>
第二步:新建一個(gè)MusicService.java文件,用于處理音樂播放的邏輯
package com.fyt.musicplayer;  import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.support.annotation.Nullable;  import java.io.IOException; import java.util.Timer; import java.util.TimerTask;  //創(chuàng)建一個(gè)繼承自服務(wù)的音樂服務(wù)類 public class MusicService extends Service {   private MediaPlayer player;  private Timer timer;   //綁定服務(wù)時(shí),調(diào)用此方法  @Nullable  @Override  public IBinder onBind(Intent intent) {   return new MusicControl();  }   //創(chuàng)建播放音樂的服務(wù)  @Override  public void onCreate() {  super.onCreate();   //創(chuàng)建音樂播放器對(duì)象  player = new MediaPlayer();  }   //銷毀播放音樂服務(wù)  @Override  public void onDestroy() {  super.onDestroy();   //停止播放音樂  player.stop();   //釋放占用的資源  player.release();   //將player置為空  player = null;  }   //播放音樂  public void play() {   try {   if(player == null)  {  player = new MediaPlayer();  }   //重置  player.reset();   //加載多媒體文件  player.setDataSource("sdcard/zxmzf.mp3");   //準(zhǔn)備播放音樂  player.prepare();   //播放音樂  player.start();   //添加計(jì)時(shí)器  addTimer();   } catch (IOException e) {  e.printStackTrace();  }  }   //暫停播放音樂  public void pausePlay() {   player.pause();  }   //繼續(xù)播放音樂  public void continuePlay() {   player.start();  }   //創(chuàng)建一個(gè)實(shí)現(xiàn)音樂接口的音樂控制類  class MusicControl extends Binder implements MusicInterface {   @Override  public void play() {   MusicService.this.play();  }   @Override  public void pausePlay() {   MusicService.this.pausePlay();  }   @Override  public void continuePlay() {   MusicService.this.continuePlay();  }   @Override  public void seekTo(int progress) {   MusicService.this.seekTo(progress);  }  }   //設(shè)置音樂的播放位置  public void seekTo(int progress) {   player.seekTo(progress);  }   //添加計(jì)時(shí)器用于設(shè)置音樂播放器中的播放進(jìn)度  public void addTimer() {   //如果沒有創(chuàng)建計(jì)時(shí)器對(duì)象  if(timer == null) {   //創(chuàng)建計(jì)時(shí)器對(duì)象  timer = new Timer();   timer.schedule(new TimerTask() {   //執(zhí)行計(jì)時(shí)任務(wù)  @Override  public void run() {   //獲得歌曲總時(shí)長(zhǎng)  int duration = player.getDuration();   //獲得歌曲的當(dāng)前播放進(jìn)度  int currentPosition = player.getCurrentPosition();   //創(chuàng)建消息對(duì)象  Message msg = MainActivity.handler.obtainMessage();   //將音樂的播放進(jìn)度封裝至消息對(duì)象中  Bundle bundle = new Bundle();  bundle.putInt("duration", duration);  bundle.putInt("currentPosition", currentPosition);  msg.setData(bundle);   //將消息發(fā)送到主線程的消息隊(duì)列  MainActivity.handler.sendMessage(msg);  }  },   //開始計(jì)時(shí)任務(wù)后的5毫秒,第一次執(zhí)行run方法,以后每500毫秒執(zhí)行一次  5, 500);  }  } } 第三步:創(chuàng)建一個(gè)MusicInterface.java文件創(chuàng)建用于操作音樂播放的接口
package com.fyt.musicplayer;  //創(chuàng)建一個(gè)音樂播放接口 public interface MusicInterface {   //播放音樂  void play();   //暫停播放音樂  void pausePlay();   //繼續(xù)播放音樂  void continuePlay();   //修改音樂的播放位置  void seekTo(int progress); } 第四步:修改MainActivity.java文件
package com.fyt.musicplayer;  import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.view.View; import android.widget.SeekBar; import android.widget.TextView;  public class MainActivity extends Activity {   MyServiceConn conn;  Intent intent;  MusicInterface mi;   //用于設(shè)置音樂播放器的播放進(jìn)度  private static SeekBar sb;   private static TextView tv_progress;  private static TextView tv_total;   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);   tv_progress = (TextView) findViewById(R.id.tv_progress);  tv_total = (TextView) findViewById(R.id.tv_total);   //創(chuàng)建意圖對(duì)象  intent = new Intent(this, MusicService.class);   //啟動(dòng)服務(wù)  startService(intent);   //創(chuàng)建服務(wù)連接對(duì)象  conn = new MyServiceConn();   //綁定服務(wù)  bindService(intent, conn, BIND_AUTO_CREATE);   //獲得布局文件上的滑動(dòng)條  sb = (SeekBar) findViewById(R.id.sb);   //為滑動(dòng)條添加事件監(jiān)聽  sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {   //當(dāng)滑動(dòng)條中的進(jìn)度改變后,此方法被調(diào)用  @Override  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {   }   //滑動(dòng)條剛開始滑動(dòng),此方法被調(diào)用  @Override  public void onStartTrackingTouch(SeekBar seekBar) {   }   //當(dāng)滑動(dòng)條停止滑動(dòng),此方法被調(diào)用  @Override  public void onStopTrackingTouch(SeekBar seekBar) {   //根據(jù)拖動(dòng)的進(jìn)度改變音樂播放進(jìn)度  int progress = seekBar.getProgress();   //改變播放進(jìn)度  mi.seekTo(progress);  }  });  }   //創(chuàng)建消息處理器對(duì)象  public static Handler handler = new Handler(){   //在主線程中處理從子線程發(fā)送過來的消息  @Override  public void handleMessage(Message msg) {   //獲取從子線程發(fā)送過來的音樂播放的進(jìn)度  Bundle bundle = msg.getData();   //歌曲的總時(shí)長(zhǎng)(毫秒)  int duration = bundle.getInt("duration");   //歌曲的當(dāng)前進(jìn)度(毫秒)  int currentPostition = bundle.getInt("currentPosition");   //刷新滑塊的進(jìn)度  sb.setMax(duration);  sb.setProgress(currentPostition);   //歌曲的總時(shí)長(zhǎng)  int minute = duration / 1000 / 60;  int second = duration / 1000 % 60;   String strMinute = null;  String strSecond = null;   //如果歌曲的時(shí)間中的分鐘小于10  if(minute < 10) {   //在分鐘的前面加一個(gè)0  strMinute = "0" + minute;  } else {   strMinute = minute + "";  }   //如果歌曲的時(shí)間中的秒鐘小于10  if(second < 10)  {  //在秒鐘前面加一個(gè)0  strSecond = "0" + second;  } else {   strSecond = second + "";  }   tv_total.setText(strMinute + ":" + strSecond);   //歌曲當(dāng)前播放時(shí)長(zhǎng)  minute = currentPostition / 1000 / 60;  second = currentPostition / 1000 % 60;   //如果歌曲的時(shí)間中的分鐘小于10  if(minute < 10) {   //在分鐘的前面加一個(gè)0  strMinute = "0" + minute;  } else {   strMinute = minute + "";  }   //如果歌曲的時(shí)間中的秒鐘小于10  if(second < 10) {   //在秒鐘前面加一個(gè)0  strSecond = "0" + second;  } else {   strSecond = second + "";  }   tv_progress.setText(strMinute + ":" + strSecond);  }  };   //播放音樂按鈕響應(yīng)函數(shù)  public void play(View view) {   //播放音樂  mi.play();  }   //暫停播放音樂按鈕響應(yīng)函數(shù)  public void pausePlay(View view) {   //暫停播放音樂  mi.pausePlay();  }   //繼續(xù)播放音樂按鈕響應(yīng)函數(shù)  public void continuePlay (View view) {   //繼續(xù)播放音樂  mi.continuePlay();  }   //退出音樂播放按鈕響應(yīng)函數(shù)  public void exit(View view) {   //解綁服務(wù)  unbindService(conn);   //停止服務(wù)  stopService(intent);   //結(jié)束這個(gè)activity  finish();  }   //實(shí)現(xiàn)服務(wù)器連接接口  class MyServiceConn implements ServiceConnection {   @Override  public void onServiceConnected(ComponentName name, IBinder service) {   //獲得中間人對(duì)象  mi = (MusicInterface) service;  }   @Override  public void onServiceDisconnected(ComponentName name) {   }  } } 第五步:在配置文件中的Application節(jié)點(diǎn)下添加服務(wù)組件
<service android:name="com.fyt.playmusic.MusicService"> </service>
最后一步:添加讀取SD卡的權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注