Mediaplayer and delay in playing

人走茶凉 提交于 2019-12-19 10:52:45

问题


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);

                              }
                          });  


来源:https://stackoverflow.com/questions/9884515/mediaplayer-and-delay-in-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!