如何通过一个无限循环改变audiotrack的持续时间? [英] How to change the duration of the audiotrack by an infinite loop?
问题描述
如何通过一个无限循环改变audiotrack的持续时间
我知道我必须在 audioTrack.play应用以下行()
:
audioTrack.setLoopPoints(0,generatedSnd.length,-1);
例如,我想时间会半秒,因此,持续时间类型的变量INT是无效的。
要做到这一点我想要的声音反复播放和控制时间停止了定时器。
在code是:
私人INT持续时间= 1; //秒
私人INT采样率= 44100;
私人最终诠释NUMSAMPLES =持续时间*采样率;
私人最终双样本[] =新的双[NUMSAMPLES]
私人双freqOfTone = 261.626; //例如音符C(DO)是261.626Hz。
私人最后一个字节generatedSnd [] =新的字节[2 * NUMSAMPLES]。最终线程线程=新主题(新的Runnable(){
公共无效的run(){
genTone();
handler.post(新的Runnable(){ 公共无效的run(){
播放声音();
}
});
}
});
thread.start();
}
});
无效genTone(){
的for(int i = 0; I< NUMSAMPLES ++我){
样本[I] = Math.sin(2 * Math.PI * I /(采样率/ freqOfTone));
} //转换成16位PCM阵列声音
//假设样品缓冲液是标准化。
INT IDX = 0;
对于(最终双DVAL:样品){
//规模最大振幅
最终短VAL =(短)((* DVAL 32767));
//在16位WAV PCM,第一个字节是低位字节
generatedSnd [IDX +] =(字节)(VAL&安培;设为0x00FF);
generatedSnd [IDX ++] =(字节)((VAL&放大器;为0xFF00)GT;>→8); }
} 无效playSound(){
最后AudioTrack audioTrack =新AudioTrack(AudioManager.STREAM_MUSIC,
采样率,AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,generatedSnd.length,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd,0,generatedSnd.length);
audioTrack.play();
}
对于我来说这一次的工作:
audioTrack.setLoopPoints(0,generatedSnd.length / FRAME_SIZE,-1);
如果你正在玩的那么5.1系统帧大小,如果12(6 * 2),或7.1是16(8×2),支持立体声这是4。
How to change the duration of the audiotrack by an infinite loop?
I understand that I must apply the following line before audioTrack.play()
:
audioTrack.setLoopPoints (0, generatedSnd.length, -1);
For example, I want the duration will be half a second, therefore, the "duration" variable of type "int" isn't valid. To do so I want the sound to play repeatedly and control the time to stop with a Timer.
The code is:
private int duration = 1; // seconds
private int sampleRate = 44100;
private final int numSamples = duration * sampleRate;
private final double sample[] = new double[numSamples];
private double freqOfTone = 261.626; // For example musical note C (Do) is 261.626Hz.
private final byte generatedSnd[] = new byte[2 * numSamples];
final Thread thread = new Thread(new Runnable() {
public void run() {
genTone();
handler.post(new Runnable() {
public void run() {
playSound();
}
});
}
});
thread.start();
}
});
void genTone() {
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
void playSound(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
This one worked for me:
audioTrack.setLoopPoints(0, generatedSnd.length/FRAME_SIZE, -1);
if you are playing on 5.1 system then frame size if 12 (6*2), or for 7.1 it's 16 (8*2), for stereo it's 4.
这篇关于如何通过一个无限循环改变audiotrack的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!