android studio隐式intent实现音乐播放
时间: 2023-09-15 22:16:43 浏览: 125
首先,需要在AndroidManifest.xml文件中声明以下权限和服务:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<service android:name=".MusicService" />
```
然后,创建一个MusicService类来处理音乐播放:
```java
public class MusicService extends Service implements MediaPlayer.OnPreparedListener {
private MediaPlayer mediaPlayer = null;
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action.equals("play")) {
String path = intent.getStringExtra("path");
playMusic(path);
} else if (action.equals("stop")) {
stopMusic();
}
return super.onStartCommand(intent, flags, startId);
}
private void playMusic(String path) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopMusic() {
mediaPlayer.stop();
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
```
在主Activity中,可以使用以下代码来启动和停止MusicService:
```java
private void playMusic(String path) {
Intent intent = new Intent(this, MusicService.class);
intent.setAction("play");
intent.putExtra("path", path);
startService(intent);
}
private void stopMusic() {
Intent intent = new Intent(this, MusicService.class);
intent.setAction("stop");
startService(intent);
}
```
要播放音乐,只需使用以下代码:
```java
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(intent, 1);
```
在onActivityResult方法中,可以获取选定音乐文件的URI,并将其传递给MusicService:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
Uri uri = data.getData();
String path = getRealPathFromURI(uri);
playMusic(path);
}
}
private String getRealPathFromURI(Uri uri) {
String[] projection = {MediaStore.Audio.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
```
完整的MainActivity代码如下:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPlay = findViewById(R.id.btn_play);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(intent, 1);
}
});
Button btnStop = findViewById(R.id.btn_stop);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopMusic();
}
});
}
private void playMusic(String path) {
Intent intent = new Intent(this, MusicService.class);
intent.setAction("play");
intent.putExtra("path", path);
startService(intent);
}
private void stopMusic() {
Intent intent = new Intent(this, MusicService.class);
intent.setAction("stop");
startService(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
Uri uri = data.getData();
String path = getRealPathFromURI(uri);
playMusic(path);
}
}
private String getRealPathFromURI(Uri uri) {
String[] projection = {MediaStore.Audio.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
}
```
阅读全文