如何通過無限循環更改錄音筆的持續時間?如何通過無限循環更改錄音筆的持續時間?
我知道必須audioTrack.play()
之前應用下面的行:
audioTrack.setLoopPoints (0, generatedSnd.length, -1);
例如,我想的持續時間將是半秒,因此,「持續時間」變量類型是「int」 ISN沒有用。 要做到這一點,我希望重複播放聲音,並控制時間停止與計時器。
的代碼是:
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();
}
即是沒有意義:/ – ephramd
我思考的人:/ – H4F