I create small application which is media player. I have method where I have a song. I want to delay playing sound after I clicked a button. How I can do this delay. I want to click on the button and after 5 seconds music is playing. I am using: MediaPlayer.create
to get song and mediaplayer.start()
to start playing, but I don;t know how I can delay start playing my song.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use a Handler in your Activity to delay events such as starting the mediaplayer in your case:
private RefreshHandler mRedrawHandler = new RefreshHandler();
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
MyActivity.startMusic();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
In onClick of button, call mRedrawHandler.sleep(5000);
, startMusic()
is a method where you are starting the mucsic playback.
回答2:
In onClickListener of the button run a thread to delay for 5 seconds. Then call mediaplayer.start() after the delay.
回答3:
for Delay video we have SeekTo method,,try this it work fine.
final int playbackTime=5*1000;
System.out.println("Play Back Time====="+playbackTime);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
Uri video = Uri.parse(videourl);
videoView.setVideoURI(video);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
videoView.start();
videoView.seekTo(playbackTime);
}
});