开发一个简易音乐播放器,要求实现: 综合使用Service,BroadCast,ContentProvider等组件实现后台播放。
提交 MusicService 和 MainActivity,运行结果截图。
评分: 1.基本分,60分,实现基本功能,提交的内容完整,条理清晰,可读性好。 2.上一首、下一首;+20分 3.UI销毁后再次打开应能与当前的播放状态保持一致;+20分
在模拟器中上传音乐:
找到路径:C:\Users\17511\Documents\AndroidStudio\DeviceExplorer\emulator-5554\storage\emulated\legacy\Music
MainActivity.java
package com.example.player;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String CONTROL = "iet.jxufe.cn.android.control";//控制播放、暂停
public static final String UPDATE = "iet.jxufe.cn.android.update";//更新界面显示
// 定义音乐的播放状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
//获取界面中显示歌曲标题、作者文本框
TextView title, author;
// 播放/暂停、停止按钮
ImageButton play, stop,next,pre;
ActivityReceiver activityReceiver;
//“启动”服务的intent
Intent MusicServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取程序界面中的两个按钮以及两个文本显示框
play = (ImageButton) this.findViewById(R.id.play);
stop = (ImageButton) this.findViewById(R.id.stop);
next = (ImageButton)this.findViewById(R.id.next);
pre = (ImageButton)this.findViewById(R.id.pre);
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
// 为两个按钮的单击事件添加监听器
play.setOnClickListener(this);
stop.setOnClickListener(this);
next.setOnClickListener(this);
pre.setOnClickListener(this);
activityReceiver = new ActivityReceiver();
IntentFilter filter = new IntentFilter(UPDATE);
// 注册BroadcastReceiver
registerReceiver(activityReceiver, filter);
MusicServiceIntent = new Intent(this, MusicService.class);
//判断权限够不够,不够就给
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1);
} else {
//够了就设置路径等,准备播放
startService(MusicServiceIntent);
}
}
//获取到权限回调方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startService(MusicServiceIntent);
} else {
Toast.makeText(this, "权限不够获取不到音乐,程序将退出", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
public void onClick(View source) {
// 创建Intent
Intent intent = new Intent(CONTROL);
//System.out.println(source.getId());
//System.out.println(source.getId() == R.id.play);
switch (source.getId()) {
// 按下播放/暂停按钮
case R.id.play:
intent.putExtra("control", 1);
Log.d("MusicService","play pressed");
break;
// 按下停止按钮
case R.id.stop:
intent.putExtra("control", 2);
Log.d("MusicService","stop pressed");
break;
case R.id.next:
intent.putExtra("control", 3);
Log.d("MusicService","next pressed");
break;
case R.id.pre:
intent.putExtra("control", 4);
Log.d("MusicService","pre pressed");
break;
}
// 发送广播 ,将被Service组件中的BroadcastReceiver接收到
sendBroadcast(intent);
}
// 自定义的BroadcastReceiver,负责监听从Service传回来的广播
public class ActivityReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// 获取Intent中的update消息,update代表播放状态,默认为-1
int update = intent.getIntExtra("update", -1);
// 获取Intent中的current消息,current代表当前正在播放的歌曲,默认为-1
String songs_title = "";
songs_title = intent.getStringExtra("songs_title");
String songs_author = "";
songs_author = intent.getStringExtra("songs_author");
if (update >= 0) {
title.setText(songs_title);
author.setText(songs_author);
}
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
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(activityReceiver);
}
}
这样就会自动添加权限
MusicService.java
package com.example.player;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MusicService extends Service {
private static final String TAG = "MusicService";
private List
activity_main.xml
AndroidManifest.xml
初始界面:
点击播放:
点击暂停:
点击下一首:
点击停止: