setOnCompletionListener问题

时间:2016-11-23 17:19:20

标签: java android-studio android-mediaplayer event-listener

嘿伙计们我是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():,但不知道在哪里放置它以及如何编码,任何想法?

1 个答案:

答案 0 :(得分:0)

这很简单......你有一个媒体播放器对象 campfiresound 所以你需要注册接口OnCompletionListener

campfiresound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
       //toast...am done!!
    }
});