Java-无缝更改循环剪辑的音量? [英] Java - Changing volume of a looping Clip seamlessly?

查看:56
本文介绍了Java-无缝更改循环剪辑的音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环播放的剪辑,并且像这样改变音量:

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可以返回音频文件的总帧长的倍数.如果将帧位置设置为大于总帧数,则会从头开始重新播放剪辑.

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.

因此解决方案是:

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

这篇关于Java-无缝更改循环剪辑的音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆