Android Studio实现简单的音乐播放(播放、暂停、停止、上一首、下一首)
简要介绍
采用Service组件实现后台播放,BroadcastReceiver实现消息传递。BroadcastReceiver是一种全局监听器,可以让不同组件之间进行通信。下面展示一个基于Service组件的音乐盒,音乐由后台运行的service组件播放,当后台播放状态改变时,通过发送广播通知前台Activity更新界面;当用户单击前台Activity界面按钮,发送广播通知后台Service改变播放状态。
界面效果
页面文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"/>
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"/>
<ImageButton
android:id="@+id/pre"
android:layout_width="38dp"
android:layout_height="41dp"
android:src="@android:drawable/ic_media_previous" />
<ImageButton
android:id="@+id/next"
android:layout_width="38dp"
android:layout_height="41dp"
android:src="@android:drawable/ic_media_next" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="83dp"
android:layout_height="16dp"
android:layout_weight="1"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:textColor="#000000"
android:textSize="25dp" />
<TextView
android:id="@+id/author"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:thumb="@drawable/ic_action_search" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:thumb="@drawable/ic_action_search" />
</LinearLayout>
MusicService.java
其中实现播放、暂停和停止使用一个status记录当前状态实现效果。而实现上一首、下一首的功能时,首先assets文件夹里导入了三首歌曲,music数组中也只有着三组数据,使用current记录下标,而上一首、下一首的关键点在于越界,当current<=0和current>=3的情况,以上一首到达current=0时为例,设置current指向数组最后一项,即music.length-1。普通情况下点击上一首,current只需减一,然后播放当前current所指歌曲即可。下一首的实现类似,也就是current>=3的情况。
package com.example.musicbox;
import java.io.IOException;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.widget.Toast;
public class MusicService extends Service implements Runnable {
MyReceiver serviceReceiver;
AssetManager am;
String[] musics = new String[] { "wish.mp3", "promise.mp3", "beautiful.mp3" };
public static MediaPlayer mPlayer;
// 当前的状态,0x11 代表没有播放 ;0x12代表 正在播放;0x13代表暂停
int status = 0x11;
// 记录当前正在播放的音乐
int current = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
am = getAssets();
// 指定该BroadcastReceiver能匹配的Intent
// 创建BroadcastReceiver
serviceReceiver = new MyReceiver();
// 创建IntentFilter
IntentFilter filter = new IntentFilter();
filter.addAction(MusicBox.CTL_ACTION);
registerReceiver(serviceReceiver, filter);
// 创建MediaPlayer
mPlayer = new MediaPlayer();
// 为MediaPlayer播放完成事件绑定监听器
mPlayer.setOnCompletionListener(new OnCompletionListener() // ①
{
@Override
public void onCompletion(MediaPlayer mp) {
current++;
MusicBox.audioSeekBar.setMax(0);
if (current >= 3) {
current = 0;
}
// 发送广播通知Activity更改文本框
Intent sendIntent = new Intent(MusicBox.UPDATE_ACTION);
sendIntent.putExtra("current", current);
// 发送广播 ,将被Activity组件中的BroadcastReceiver接收到
sendBroadcast(sendIntent);
prepareAndPlay(musics[current]);
}
});
super.onCreate();
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
int control = intent.getIntExtra("control", -1);
switch (control) {
// 播放或暂停
case 1:
// 原来处于没有播放状态
if (status == 0x11) {
// 准备、并播放音乐
prepareAndPlay(musics[current]);
status = 0x12;
}
// 原来处于播放状态
else if (status == 0x12) {
// 暂停
mPlayer.pause();
// 改变为暂停状态
status = 0x13;
}
// 原来处于暂停状态
else if (status == 0x13) {
// 播放
mPlayer.start();
// 改变状态
status = 0x12;
}
break;
// 停止声音
case 2:
// 如果原来正在播放或暂停
if (status == 0x12 || status == 0x13) {
// 停止播放
mPlayer.stop();
status = 0x11;
}
case 3:
//上一首切换
if (current <= 0) {
//停止播放
mPlayer.stop();
//修改current
current = musics.length-1;
//播放
prepareAndPlay(musics[current]);
status = 0x12;
}
else{
mPlayer.stop();
current--;
prepareAndPlay(musics[current]);
status = 0x12;
}
break;
//下一首切换
case 4:
if (current >= 2) {
mPlayer.stop();
current = 0;
prepareAndPlay(musics[current]);
status = 0x12;
}
else{
mPlayer.stop();
current++;
prepareAndPlay(musics[current]);
status = 0x12;
}
break;
}
// 发送广播通知Activity更改图标、文本框
Intent sendIntent = new Intent(MusicBox.UPDATE_ACTION);
sendIntent.putExtra("update", status);
sendIntent.putExtra("current", current);
// 发送广播 ,将被Activity组件中的BroadcastReceiver接收到
sendBroadcast(sendIntent);
}
}
private void prepareAndPlay(String music) {
try {
// 打开指定音乐文件
AssetFileDescriptor afd = am.openFd(music);
mPlayer.reset();
// 使用MediaPlayer加载指定的声音文件。
mPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
// 准备声音
mPlayer.prepare();
// 播放
mPlayer.start();
// 设置进度条最大值
MusicBox.audioSeekBar.setMax(MusicService.mPlayer.getDuration());
new Thread(this).start();
} catch (IOException e) {
e.printStackTrace();
}
}
// 刷新进度条
@Override
public void run() {
int CurrentPosition = 0;
int total = mPlayer.getDuration();
while (mPlayer != null && CurrentPosition < total) {
try {
Thread.sleep(1000);
if (mPlayer != null) {
CurrentPosition = mPlayer.getCurrentPosition();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
MusicBox.audioSeekBar.setProgress(CurrentPosition);
}
}
}
MusicBox.java
package com.example.musicbox;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
public class MusicBox extends Activity implements View.OnClickListener {
// 获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放/暂停、停止按钮
ImageButton play, stop,pre,next;
// 声明音量管理器
public AudioManager mAudioManager = null;
// 定义进度条
public static SeekBar audioSeekBar = null;
// 定义音量大小
public SeekBar audioVolume = null;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
String[] titleStrs = new String[] { "心愿", "约定", "美丽新世界" };
String[] authorStrs = new String[] { "未知艺术家", "周蕙", "伍佰" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面界面中的两个按钮
play = (ImageButton) this.findViewById(R.id.play);
stop = (ImageButton) this.findViewById(R.id.stop);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
audioVolume = (SeekBar) findViewById(R.id.seekBar2);
audioSeekBar = (SeekBar) findViewById(R.id.seekBar1);
pre=(ImageButton) this.findViewById(R.id.pre);
next=(ImageButton) this.findViewById(R.id.next);
// 为两个按钮的单击事件添加监听器
play.setOnClickListener(this);
stop.setOnClickListener(this);
pre.setOnClickListener(this);
next.setOnClickListener(this);
// 播放进度监听
audioSeekBar.setOnSeekBarChangeListener(new SeekBarChangeEvent());
// 退出后再次进去程序时,进度条保持持续更新
if (MusicService.mPlayer != null) {
// 设置进度条的最大值
MusicBox.audioSeekBar.setMax(MusicService.mPlayer.getDuration());
audioSeekBar.setProgress(MusicService.mPlayer.getCurrentPosition());
}
// 得到当前音量对象
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// 把当前音量值赋给进度条
audioVolume.setProgress(mAudioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
// 监听音量
audioVolume.setOnSeekBarChangeListener(new AudioVolumeChangeEvent());
activityReceiver = new ActivityReceiver();
// 创建IntentFilter
IntentFilter filter = new IntentFilter();
// 指定BroadcastReceiver监听的Action
filter.addAction(UPDATE_ACTION);
// 注册BroadcastReceiver
registerReceiver(activityReceiver, filter);
Intent intent = new Intent(this, MusicService.class);
// 启动后台Service
startService(intent);
}
// 音量监听
class AudioVolumeChangeEvent implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
// 播放进度监听,别忘了Service里面还有个进度条刷新
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 假设改变源于用户拖动
if (fromUser) {
MusicService.mPlayer.seekTo(progress);
// 当进度条的值改变时,音乐播放器从新的位置开始播放
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
MusicService.mPlayer.pause();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
MusicService.mPlayer.start();
}
}
// 自定义的BroadcastReceiver,负责监听从Service传回来的广播
public class ActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 获取Intent中的update消息,update代表播放状态
int update = intent.getIntExtra("update", -1);
// 获取Intent中的current消息,current代表当前正在播放的歌曲
int current = intent.getIntExtra("current", -1);
if (current >= 0) {
title.setText(titleStrs[current]);
author.setText(authorStrs[current]);
}
switch (update) {
case 0x11:
play.setImageResource(R.drawable.play);
status = 0x11;
break;
// 控制系统进入播放状态
case 0x12:
// 播放状态下设置使用暂停图标
play.setImageResource(R.drawable.pause);
// 设置当前状态
status = 0x12;
break;
// 控制系统进入暂停状态
case 0x13:
// 暂停状态下设置使用播放图标
play.setImageResource(R.drawable.play);
// 设置当前状态
status = 0x13;
break;
}
}
}
@Override
public void onClick(View source) {
// 创建Intent
Intent intent = new Intent("org.crazyit.action.CTL_ACTION");
switch (source.getId()) {
// 按下播放/暂停按钮
case R.id.play:
intent.putExtra("control", 1);
break;
// 按下停止按钮
case R.id.stop:
intent.putExtra("control", 2);
break;
//按下上一首按钮
case R.id.pre:
intent.putExtra("control", 3);
break;
//按下上一首按钮
case R.id.next:
intent.putExtra("control", 4);
break;
}
// 发送广播,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
}
可能遇到的问题:
由于现在的android版本的不同,androidx可能会导致出错,简单看下我的build.gradle(Module:app)文件。可以将gradle.properties中的android.useAndroidX=true以及android.enableJetifier=true注释掉。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}