我正在使用的Android应用程序使用AudioTrack来播放音符。这会创建一个新的线程,对于每个被按下的音符都不会结束(直到我的应用程序崩溃)。
来自A组的:
AudioGenerator.playSound(AudioGenerator.genTone((tone)));
AudioGenerator类:
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
public class AudioGenerator {
private static final int sampleRate = 8000;
private static final double duration = 1; // seconds
private static double dnumSamples = duration * sampleRate;
private static final int numSamples = (int) dnumSamples;
private static double sample[] = new double[numSamples];
private static byte generatedSnd[] = new byte[2*numSamples];
static void playSound(byte[] sound){
final byte[] play = sound;
(new Thread(new Runnable() {
@Override
public void run() {
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, numSamples, AudioTrack.MODE_STATIC);
audioTrack.write(play, 0, play.length);
audioTrack.play();
}})).start();
}
static byte[] genTone(double freq){
for (int i = 0; i < numSamples; ++i) sample[i] = Math.sin(freq * 2 * Math.PI * i / (sampleRate));
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
int i = 0;
int ramp = numSamples/20; // Amplitude ramp as a percent of sample count
for (i = 0; i< ramp; ++i) { // Ramp amplitude up (to avoid clicks)
double dVal = sample[i];
// Ramp up to maximum
final short val = (short) ((dVal * 32767 * i/ramp));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
for (i = i; i< numSamples - ramp; ++i) { // Max amplitude for most of the samples
double dVal = sample[i];
// 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);
}
for (i = i; i< numSamples; ++i) { // Ramp amplitude down
double dVal = sample[i];
// Ramp down to zero
final short val = (short) ((dVal * 32767 * (numSamples-i)/ramp ));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
return generatedSnd;
}
}
我无法弄清楚我错过了什么。请帮忙
答案 0 :(得分:-1)
尝试修改playTone和playSamples方法:
private void playTone(final int freq, final double duration)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
genTone(freq, duration);
}
});
thread.start();
}
private void playSamples(byte[] generatedSound, int sampleRate, int numSamples)
{
AudioTrack audioTrack = null;
try
{
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
(int) numSamples * 2, AudioTrack.MODE_STATIC);
// Load the track
audioTrack.write(generatedSound, 0, generatedSound.length);
audioTrack.play();
}
catch (Exception e)
{
Log.e(MainActivity.DEBUG_TAG, "Audio track exception: " + e.getMessage());
}
int x = 0;
do
{ // Montior playback to find when done
if (audioTrack != null)
x = audioTrack.getPlaybackHeadPosition();
else
x = numSamples;
}
while (x < numSamples);
audioTrack.release();
}
最后,将playSamples(generatedSnd, sampleRate, numSamples);
添加到genTone方法的末尾;