Android: How to stop media (mp3) in playing when specific milliseconds come?

后端 未结 2 1343
醉酒成梦
醉酒成梦 2020-12-09 12:14

I have an mp3 file and I want to play a specific word in it. I have a start time (6889 ms) and end time (7254 ms).

I have these codes:

2条回答
  •  天涯浪人
    2020-12-09 12:32

    In Oncreate Method ,use the below 4 lines code:
    
    
         MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.nicholas);
         mPlayer.start();
         mPlayer.seekTo(startTime);   
         updatePlayerPostion(); 
    
    
    private Handler handler=new Handler(){
    
                @Override
                public void handleMessage(Message msg) {
                            // TODO Auto-generated method stub
                            super.handleMessage(msg);
                            updatePlayerPostion();
                    }
                };
                updatePlayerPostion()
                {
                   handler.removeCallbacks(r);
                   if(player.getCurrentPosition==7254)
    
                  player.stop();
    
    
                else
                   handler.postDelayed(r, 1); //1 millisecond
    
                }
            Runnable r=new Runnable(){
            @Override
                    public void run() {
                        // TODO Auto-generated method stub
                         updatePlayerPostion()
                    }
            };
    

提交回复
热议问题