這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Android中實現(xiàn)一個音樂播放器,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、高郵網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為高郵等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
實現(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"> <!--顯示播放進度--> <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)前進度--> <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00"/> <!--顯示總進度--> <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è)置音樂播放器中的播放進度
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)前播放進度
int currentPosition = player.getCurrentPosition();
//創(chuàng)建消息對象
Message msg = MainActivity.handler.obtainMessage();
//將音樂的播放進度封裝至消息對象中
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è)置音樂播放器的播放進度
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)滑動條中的進度改變后,此方法被調(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ù)拖動的進度改變音樂播放進度
int progress = seekBar.getProgress();
//改變播放進度
mi.seekTo(progress);
}
});
}
//創(chuàng)建消息處理器對象
public static Handler handler = new Handler(){
//在主線程中處理從子線程發(fā)送過來的消息
@Override
public void handleMessage(Message msg) {
//獲取從子線程發(fā)送過來的音樂播放的進度
Bundle bundle = msg.getData();
//歌曲的總時長(毫秒)
int duration = bundle.getInt("duration");
//歌曲的當(dāng)前進度(毫秒)
int currentPostition = bundle.getInt("currentPosition");
//刷新滑塊的進度
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"/>
上述就是小編為大家分享的怎么在Android中實現(xiàn)一個音樂播放器了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:怎么在Android中實現(xiàn)一個音樂播放器
文章起源:http://chinadenli.net/article36/pishsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、用戶體驗、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站制作、網(wǎng)站策劃、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)