Android版音樂播放器
音樂播放器是一個非常常見的應(yīng)用,這篇博客就是介紹如何制作一個簡單的音樂播放器,這款音樂播放器具有以下的功能:播放歌曲、暫停播放歌曲、、顯示歌曲的總時長、顯示歌曲的當(dāng)前播放時長、調(diào)節(jié)滑塊可以將歌曲調(diào)節(jié)到任何時間播放、退出音樂播放器。
實現(xiàn)效果如下

實現(xiàn)方式:
第一步:使用Android Studio創(chuàng)建一個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>
第二步:新建一個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)建一個繼承自服務(wù)的音樂服務(wù)類
public class MusicService extends Service {
private MediaPlayer player;
private Timer timer;
//綁定服務(wù)時,調(diào)用此方法
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MusicControl();
}
//創(chuàng)建播放音樂的服務(wù)
@Override
public void onCreate() {
super.onCreate();
//創(chuàng)建音樂播放器對象
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();
//添加計時器
addTimer();
} catch (IOException e) {
e.printStackTrace();
}
}
//暫停播放音樂
public void pausePlay() {
player.pause();
}
//繼續(xù)播放音樂
public void continuePlay() {
player.start();
}
//創(chuàng)建一個實現(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);
}
//添加計時器用于設(shè)置音樂播放器中的播放進(jìn)度
public void addTimer() {
//如果沒有創(chuàng)建計時器對象
if(timer == null) {
//創(chuàng)建計時器對象
timer = new Timer();
timer.schedule(new TimerTask() {
//執(zhí)行計時任務(wù)
@Override
public void run() {
//獲得歌曲總時長
int duration = player.getDuration();
//獲得歌曲的當(dāng)前播放進(jìn)度
int currentPosition = player.getCurrentPosition();
//創(chuàng)建消息對象
Message msg = MainActivity.handler.obtainMessage();
//將音樂的播放進(jìn)度封裝至消息對象中
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
//將消息發(fā)送到主線程的消息隊列
MainActivity.handler.sendMessage(msg);
}
},
//開始計時任務(wù)后的5毫秒,第一次執(zhí)行run方法,以后每500毫秒執(zhí)行一次
5, 500);
}
}
}
第三步:創(chuàng)建一個MusicInterface.java文件創(chuàng)建用于操作音樂播放的接口
package com.fyt.musicplayer;
//創(chuàng)建一個音樂播放接口
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)建意圖對象
intent = new Intent(this, MusicService.class);
//啟動服務(wù)
startService(intent);
//創(chuàng)建服務(wù)連接對象
conn = new MyServiceConn();
//綁定服務(wù)
bindService(intent, conn, BIND_AUTO_CREATE);
//獲得布局文件上的滑動條
sb = (SeekBar) findViewById(R.id.sb);
//為滑動條添加事件監(jiān)聽
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//當(dāng)滑動條中的進(jìn)度改變后,此方法被調(diào)用
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
//滑動條剛開始滑動,此方法被調(diào)用
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
//當(dāng)滑動條停止滑動,此方法被調(diào)用
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//根據(jù)拖動的進(jìn)度改變音樂播放進(jìn)度
int progress = seekBar.getProgress();
//改變播放進(jìn)度
mi.seekTo(progress);
}
});
}
//創(chuàng)建消息處理器對象
public static Handler handler = new Handler(){
//在主線程中處理從子線程發(fā)送過來的消息
@Override
public void handleMessage(Message msg) {
//獲取從子線程發(fā)送過來的音樂播放的進(jìn)度
Bundle bundle = msg.getData();
//歌曲的總時長(毫秒)
int duration = bundle.getInt("duration");
//歌曲的當(dāng)前進(jìn)度(毫秒)
int currentPostition = bundle.getInt("currentPosition");
//刷新滑塊的進(jìn)度
sb.setMax(duration);
sb.setProgress(currentPostition);
//歌曲的總時長
int minute = duration / 1000 / 60;
int second = duration / 1000 % 60;
String strMinute = null;
String strSecond = null;
//如果歌曲的時間中的分鐘小于10
if(minute < 10) {
//在分鐘的前面加一個0
strMinute = "0" + minute;
} else {
strMinute = minute + "";
}
//如果歌曲的時間中的秒鐘小于10
if(second < 10)
{
//在秒鐘前面加一個0
strSecond = "0" + second;
} else {
strSecond = second + "";
}
tv_total.setText(strMinute + ":" + strSecond);
//歌曲當(dāng)前播放時長
minute = currentPostition / 1000 / 60;
second = currentPostition / 1000 % 60;
//如果歌曲的時間中的分鐘小于10
if(minute < 10) {
//在分鐘的前面加一個0
strMinute = "0" + minute;
} else {
strMinute = minute + "";
}
//如果歌曲的時間中的秒鐘小于10
if(second < 10) {
//在秒鐘前面加一個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é)束這個activity
finish();
}
//實現(xiàn)服務(wù)器連接接口
class MyServiceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//獲得中間人對象
mi = (MusicInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
第五步:在配置文件中的Application節(jié)點下添加服務(wù)組件
<service android:name="com.fyt.playmusic.MusicService"> </service>
最后一步:添加讀取SD卡的權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android UI自定義ListView實現(xiàn)下拉刷新和加載更多效果
這篇文章主要介紹了Android UI自定義ListView實現(xiàn)下拉刷新和加載更多效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
解析Android應(yīng)用程序運(yùn)行機(jī)制
這篇文章主要介紹了Android應(yīng)用程序運(yùn)行機(jī)制,有需要的朋友可以參考一下2014-01-01
Android activity動畫不生效原因及解決方案總結(jié)
android activity動畫是一個比較簡單的功能。但是使用時總會由于各種小問題導(dǎo)致動畫失效,筆者根據(jù)自己經(jīng)驗,整理了各種可能導(dǎo)致的原因,期望能對你有所幫助2021-11-11
Android實現(xiàn)H5與Native交互的兩種方式
Android實現(xiàn)H5頁面和Native頁面交互的方法有兩種,一種是Url攔截的方法,另一種是JavaScript注入,下面來通過這篇文章分別講解。有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
android中DatePicker和TimePicker的使用方法詳解
這篇文章主要介紹了android中DatePicker和TimePicker的使用方法,是Android中常用的功能,需要的朋友可以參考下2014-07-07

