android怎么用Bundle传MP3,实现不同MP3
时间: 2024-05-03 09:18:38 浏览: 27
可以使用以下步骤来使用 Bundle 传递 MP3 文件:
1. 将 MP3 文件放入 app 的 `raw` 目录中。
2. 使用 `MediaPlayer` 类来播放 MP3 文件,需要传入 MP3 文件的资源 id,可以使用 `R.raw.file_name` 来获取资源 id。
3. 在要传递 MP3 文件的 Activity 中,创建一个 Bundle 对象,并使用 `putInt()` 方法将 MP3 文件的资源 id 存储到 Bundle 中。
4. 在启动目标 Activity 时,将 Bundle 对象传递给 Intent 对象,并使用 `putExtras()` 方法将 Bundle 对象添加到 Intent 中。
5. 在目标 Activity 中,使用 `getIntent()` 方法获取 Intent 对象,再使用 `getExtras()` 方法获取传递的 Bundle 对象。最后,使用 `getInt()` 方法获取 MP3 文件的资源 id,并使用 `MediaPlayer` 类播放对应的 MP3 文件。
以下是示例代码:
在发送方的 Activity 中:
```java
Bundle bundle = new Bundle();
bundle.putInt("mp3_resource_id", R.raw.mp3_file_name);
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtras(bundle);
startActivity(intent);
```
在接收方的 Activity 中:
```java
Bundle bundle = getIntent().getExtras();
int mp3ResourceId = bundle.getInt("mp3_resource_id");
MediaPlayer mediaPlayer = MediaPlayer.create(this, mp3ResourceId);
mediaPlayer.start();
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)