1、编写布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@drawable/bg">
<EditText
android:id="@+id/et_inputPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Music/a.mp3"/><!--data/data/com.examplehq.musicplay-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:layout_gravity="center_vertical"
android:gravity="center"
>
<TextView
android:id="@+id/tv_play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/play"
android:drawablePadding="3dp"
android:gravity="center"
android:text="播放"/>
<TextView
android:id="@+id/tv_pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/pause"
android:drawablePadding="3dp"
android:gravity="center"
android:text="暂停"/>
</LinearLayout>
</LinearLayout>
2、创建服务编写MusicService服务实现音乐播放
package com.examplehq.musicplay;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.IOException;
public class MusicService extends Service {
private MediaPlayer mPlayer;
/* 绑定服务的实现流程:
* 1.服务 onCreate, onBind, onDestroy 方法
* 2.onBind 方法需要返回一个 IBinder 对象
* 3.如果 Activity 绑定,Activity 就可以取到 IBinder 对象,可以直接调用对象的方法
*/
public MusicService(){}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
if (mPlayer!=null){
mPlayer.stop();
mPlayer.release();
mPlayer=null;
}
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
//第一步执行onBinder方法
return new MyBinder();
}
//定义内部类继承Binder类
class MyBinder extends Binder {
//播放音乐
public void play(String path) {
try {
if (mPlayer==null){
//创建一个MediaPlayer播放器
mPlayer =new MediaPlayer();
//指参数为音频文件
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//指定播放路径
mPlayer.setDataSource(path);
//准备播放
mPlayer.prepare();
//开始播放
mPlayer.start();
Log.i("play","播放音乐");
}else {
if (!mPlayer.isPlaying()){
mPlayer.start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//暂停播放
public void pause(){
if (mPlayer!=null && mPlayer.isPlaying()){
mPlayer.pause();
}else if (mPlayer!=null && (mPlayer.isPlaying())){
mPlayer.start();
}
}
}
}
编写Activity编写逻辑代码传递数据
package com.examplehq.musicplay;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText etInputPath;
private TextView tvplay;
private TextView tvPause;
private Intent intent;
private MyConn myConn;
MusicService.MyBinder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int EXTERNAL_STORAGE_REQ_CODE = 10 ;
int permission = ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// 请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
EXTERNAL_STORAGE_REQ_CODE);
}
etInputPath = (EditText) findViewById(R.id.et_inputPath);
tvplay = (TextView) findViewById(R.id.tv_play);//播放
tvPause = (TextView) findViewById(R.id.tv_pause);//按钮
tvplay.setOnClickListener(this);
tvPause.setOnClickListener(this);
intent =new Intent(this,MuiscService.class);
myConn =new MyConn();
bindService(intent,myConn,BIND_AUTO_CREATE);
}
//实现播放暂停功能
public void onClick(View view){
String txPath =etInputPath.getText().toString().trim();
File sd_path = Environment.getExternalStorageDirectory();
File file =new File(sd_path,txPath);
String path =file.getAbsolutePath();
Log.i("路径",txPath+file.exists());
switch (view.getId()){
case R.id.tv_play:
if (file.exists()&&file.length()>0){
binder.play(path);
Log.i("play2","播放了"+txPath);
}
Log.i("play1","播放了"+sd_path);
break;
case R.id.tv_pause:
binder.pause();
break;
default:
break;
}
}
private class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder =(MuiscService.MyBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
protected void onDestory(){
unbindService(myConn);
super.onDestroy();
}
}
MainActivity.java代码块
8.0以上版本需手动注册权限
final int EXTERNAL_STORAGE_REQ_CODE = 10 ;
int permission = ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// 请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
EXTERNAL_STORAGE_REQ_CODE);
}
AndroidManifest.xml代码
/**/
<!--增加请求安装权限-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!--从sdcard读取数据的权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!--往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
推入mp3文件
选中文件夹 右键—>upload ---->选择推入文件---->确定
最后,注意啦!
一定要在AndroidManifest.xml清单文件中加入
android:requestLegacyExternalStorage=“true”
初学者还有诸多不足还望海涵!