嘿伙计们我是java的新手,试图完成我的第一个大学项目。我想通过设置OnCompletionListener
来完成代码,将声音设置为null
。所以,它看起来像这样:
package com.example.alaiborys.newfuckingshit;
public class campfire extends AppCompatActivity {
MediaPlayer campfiresound;
int paused;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campfire);
}
public void play(View view) {
if (campfiresound == null) {
campfiresound = MediaPlayer.create(this, R.raw.campfire);
campfiresound.start();
}
else if(!campfiresound.isPlaying())
{
campfiresound.seekTo(paused);
campfiresound.start();
}
}
public void stop(View view) {
campfiresound.release();
campfiresound = null;
}
public void pause(View view) {
campfiresound.pause();
paused = campfiresound.getCurrentPosition();
}
}
所以,我想添加OnCompletionListener():
,但不知道在哪里放置它以及如何编码,任何想法?
答案 0 :(得分:0)
这很简单......你有一个媒体播放器对象 campfiresound 所以你需要注册接口OnCompletionListener
campfiresound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//toast...am done!!
}
});