简体   繁体   中英

Java - Changing volume of a looping Clip seamlessly?

I have a looping Clip, and I change the volume like so:

clip.stop();
clip.flush();
FloatControl fc = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
fc.setValue(new_volume);
clip.loop(javax.sound.midi.Sequencer.LOOP_CONTINUOUSLY);

Problem: when the clip is restarted, it isn't playing at the same frame as it ended; there's an audible jump in the loop.

If I don't flush the clip this problem goes away, but then the volume doesn't change until the buffered data has drained, which is even worse.

I tried getting and resetting the frame position but that didn't seem to change anything.

Is there any way to resume playback where it left off? (Doesn't need to be exact...) Is there any way to measure the amount of data that will be flushed and set the frame based on that?

Solving my own question... duh.

getFramePosition for looping audio can return a multiple of the total frame length of the audio file. When setting frame position larger than the total frames, it restarts the clip from the beginning.

So the solution is:

loop.setFramePosition(old_pos % loop.getFrameLength());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM