Android how can play song for 30 seconds only in mediaPlayer

后端 未结 3 1205
迷失自我
迷失自我 2021-01-18 05:27

I am working on Android , I am creating a player for audio songs. I want to play a song only for just 30 seconds. After that, player must be closed. It should be start again

3条回答
  •  一生所求
    2021-01-18 05:44

    private void playSoundForXSeconds(final Uri soundUri, int seconds) {
        if(soundUri!=null) {
            final MediaPlayer mp = new MediaPlayer();
            try {
                mp.setDataSource(Settings.this, soundUri);
                mp.prepare();
                mp.start();
            }catch(Exception e) {
                e.printStackTrace();
            }
    
            Handler mHandler = new Handler();
            mHandler.postDelayed(new Runnable() {
                public void run() {
                   try {
                       mp.stop();
                   }catch(Exception e) {
                      e.printStackTrace();
                   }
                }
            }, seconds * 1000);
        }
    }
    

提交回复
热议问题