0

I am adding a minute (60*1000*1000 microseconds) to the audio clip and starting it and I achieve the desired result only the first time i.e, I'm reaching the correct point in audio file. Next time I call getMicrosecondPosition on the Clip object, I get the microsecond position as if I didn't add anything. Have attached the sample snippet and output for this behavior. Can't quite understand what I'm doing wrong.

clip.stop();
currentPlaybackTime=clip.getMicrosecondPosition()+60L*1000*1000;
System.out.println("Playing from "+currentPlaybackTime+" ms i.e "+currentPlaybackTime/1000000.0+" seconds");
clip.setMicrosecondPosition((currentPlaybackTime));
System.out.println("Verifying: currentPlaybackTime is "+clip.getMicrosecondPosition());
clip.start();
System.out.println("Verifying 2: currentPlaybackTime is "+clip.getMicrosecondPosition());

Output is

Playing from 63326000 ms i.e 63.326 seconds [Note:this is seeking to correct point in audio file!]
Verifying: currentPlaybackTime is 3326000 [Why not here?]
Verifying 2: currentPlaybackTime is 3326000 [and here?]
Play/Pause Clicked
Pausing at 5120000 ms 5.12 seconds
Srinivas
  • 332
  • 4
  • 18
  • Don't add `60*1000*1000 milliseconds` - it's easy to make typos and/or reados. Add `TimeUnit.MINUTES.toMillis(1)`. – Andy Turner Apr 26 '16 at 13:25
  • Thanks for the pointer. This is not the case here though :) – Srinivas Apr 26 '16 at 14:13
  • 1
    Well, it is. In fact, your question illustrates why you should avoid the explicit multiplication: `60*1000*1000 milliseconds` is 60,000 seconds, not 1 minute as you claim. You either mean *microseconds*, or *1000 minutes*. – Andy Turner Apr 26 '16 at 14:14
  • Meant microseconds! Yeah – Srinivas Apr 26 '16 at 17:51
  • `clip.start();` is not a blocking method call (a `Clip` has its own daemon thread), so `System.out.println("Verifying 2:..` will be called within milliseconds of the first `println`. – Andrew Thompson Apr 27 '16 at 02:50
  • Why didn't Verifying:... didn't work? I set it before getting the value. Also, I realized that the MicrosecondPosition counts the microseconds of data actually played even if you forward or rewind the data? i.e if you play for 5 seconds and forward for 1 minute and play 5 seconds, getMicrosecondPosition gives only 10 seconds (although in microseconds). Is my observation right? – Srinivas Apr 27 '16 at 08:08

0 Answers0