android怎么用Bundle和putstring传MP3,实现不同MP3根据item的不同传不同的MP3
时间: 2024-05-06 14:16:21 浏览: 19
可以通过使用Bundle和putString传递MP3的文件路径,来实现不同的MP3根据item的不同传递不同的MP3。
首先,需要在每个item中绑定对应的MP3文件路径,可以使用一个数组来存储这些文件路径,例如:
```
String[] mp3Paths = {"path1.mp3", "path2.mp3", "path3.mp3"};
```
然后,在点击item时,获取对应的MP3文件路径,并将其放入Bundle中,传递给下一个页面,代码如下:
```
// 获取当前点击的item的位置
int position = recyclerView.getChildAdapterPosition(view);
// 创建Bundle对象,并将对应的MP3文件路径放入其中
Bundle bundle = new Bundle();
bundle.putString("mp3Path", mp3Paths[position]);
// 跳转到下一个页面,并将Bundle传递给该页面
Intent intent = new Intent(context, NextActivity.class);
intent.putExtras(bundle);
context.startActivity(intent);
```
最后,在下一个页面中,获取Bundle中的MP3文件路径,可以使用以下代码:
```
// 获取Bundle对象
Bundle bundle = getIntent().getExtras();
// 获取MP3文件路径
String mp3Path = bundle.getString("mp3Path");
```
有了这个MP3文件路径,就可以使用MediaPlayer来播放对应的MP3了。