[英]MediaPlayer plays only one time
I get a path of music file from my listview using startActivityForResult and then set that string for MediaPlayer with MediaPlayer.setDataSource(STRING);
我使用startActivityForResult从列表视图中获取音乐文件的路径,然后使用
MediaPlayer.setDataSource(STRING);
为MediaPlayer设置该字符串MediaPlayer.setDataSource(STRING);
First time I play the song with button it plays all right, and if I click button while song is playing it will reset and play again. 第一次播放带有按钮的歌曲时,它会正确播放,如果在播放歌曲时单击按钮,它将重置并再次播放。
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
But if I tap on button playback is completed it will not play again. 但是,如果我点击按钮播放已完成,它将不会再次播放。
startActivityForResult startActivityForResult
Intent i = new Intent(MainActivity.this, Activityone.class);
startActivityForResult(i, 0);
mMediaPlayer.reset();
In Listview : 在Listview中:
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 : onActivityResult:
if (resultCode == RESULT_OK){
if(requestCode == 0) {
// handling code of startActivityForResult of first button
Bundle basket = data.getExtras();
s = basket.getString("arnswer");
}
and Button to start MediaPlayer : 和Button启动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) {
}
LogCat : LogCat:
I think you need to mMediaPlayer.seekTo(0)
in order to replay, because it has finished. 我认为您需要
mMediaPlayer.seekTo(0)
才能重播,因为它已经完成了。
See the android doc page . 请参阅android doc页面 。
Edit: 编辑:
Looking the diagram 看图
between
Started
<-> PlaybackCompleted
seems to be only one action in order to restart the player... you do not need prepare
which you have :( ... 在
Started
<-> PlaybackCompleted
似乎只是重启播放器的一项操作...您不需要prepare
具有的:( ...
I understand the reason why you want prepare
so I suggest OnCompletetionListener
to mMediaPlayer.stop()
so as to move to Stopped
state and be ready for next prepare()
! 我明白为什么你要
prepare
,所以我建议OnCompletetionListener
到mMediaPlayer.stop()
以便转移到Stopped
状态,并准备下一次prepare()
This code works perfectly. 此代码完美地工作。 I hope to help you.
希望对您有所帮助。
public class PianoActivity extends Activity {
private MediaPlayer mediaPlayer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piano);
setupUI();
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
private void setupUI() {
findViewById(R.id.doo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
managerOfSound();
}
});
}
private void managerOfSound() {
mediaPlayer = MediaPlayer.create(this, R.raw.doo);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
} else {
mediaPlayer.stop();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.