Running a thread for some seconds

杀马特。学长 韩版系。学妹 提交于 2019-12-01 11:54:34

问题


I am using a media player instance to play a music file.I want to play the song for certain time then stop playing.I'm using a thread with counter decrementing but some how tis not workin properly.


回答1:


you have to use handler for that

try this

in your onCreate use this 

  //start media player
  mp.start();


  mTimer.sendMessageDelayed(new Message(),5*10000);

create a class in you activity class as

    private MusicTimer mTimer = new MusicTimer();

    private class MusicTimer extends Handler
    {
       @Override
       handleMessage(Message msg)
       {
           onTimerExpire();
       }  



        public void onTimerExpire()
        {
           //stop player here
        }

}

make media player object member variable this will play that for five seconde then stop nthat




回答2:


this is something you can do.. Play with media player normally and at the same Time initialise a handler and call its postDelayed method with interval you want.. and inside it stop the MEdia player.. Something like this..

new Handler().postDelayed(new Runnable(){

//stop playing
}, 400);


来源:https://stackoverflow.com/questions/10136494/running-a-thread-for-some-seconds

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