Running a thread for some seconds

前端 未结 2 544
予麋鹿
予麋鹿 2021-01-16 07:37

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

相关标签:
2条回答
  • 2021-01-16 08:09

    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

    0 讨论(0)
  • 2021-01-16 08:14

    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);
    
    0 讨论(0)
提交回复
热议问题