Search code examples
androidaudioplayback

Android: Play part of a sound file


My sound file is 5 minute long. I want to play part of the sound file from minute 1:00 to 2:00. Any one has an idea:

mp = MediaPlayer.create(getApplicationContext(),soundID);  
mp.start();

Solution

  • You should read about MediaPlayer, in particular about seekTo(int time).

    seekTo receives time in milliseconds so for your case you should do as follows:

    private static final int MILLISECONDS_IN_SECOND = 1000;
    private static final int TIME_IN_SECONDS = 60;
    ...
    mPlayer.seekTo(TIME_IN_SECONDS * MILLISECONDS_IN_SECOND);
    mPlayer.start();
    

    whereas mPlayer is your MediaPlayer.