我使用 startActivityForResult 从我的列表视图中获取音乐文件的路径,然后为 MediaPlayer 设置该字符串,MediaPlayer.setDataSource(STRING);
第一次使用按钮播放歌曲时,它可以正常播放,如果我在播放歌曲时单击按钮,它将重置并再次播放。
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
但是如果我点击按钮播放完成,它将不会再次播放。
开始活动结果
Intent i = new Intent(MainActivity.this, Activityone.class);
startActivityForResult(i, 0);
mMediaPlayer.reset();
在列表视图中:
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
String filename = musiccursor.getString(music_column_index);
Intent person = new Intent();
Bundle backpack = new Bundle();
backpack.putString("arnswer", filename);
person.putExtras(backpack);
setResult(RESULT_OK, person);
finish();
onActivityResult :
if (resultCode == RESULT_OK){
if(requestCode == 0) {
// handling code of startActivityForResult of first button
Bundle basket = data.getExtras();
s = basket.getString("arnswer");
}
和按钮启动 MediaPlayer :
public void pbutton1(View view) {
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(s); //using string here
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
日志猫: