我在静态模式下使用 AudioTrack 来一遍又一遍地再现相同的信号。
我按照这里的示例进行操作,有时它工作得很好,但有时它会抛出此错误并且不产生声音:
AudioTrack: start called from a thread
01-23 15:26:16.902: W/libutils.threads(1133): Thread (this=0x3973b8): don't call waitForExit() from this Thread object's thread. It's a guaranteed deadlock!
Run Code Online (Sandbox Code Playgroud)
这是源代码。我试图确保调用停止并重新加载数据以供下一次“播放”执行。
public class SoundPlayer {
// originally from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html
private int numSamples;
private double sample[];
private byte generatedSnd[];
private AudioTrack audioTrack;
public SoundPlayer(float duration, int sampleRate, double freqOfTone) {
super();
this.numSamples = (int) (duration * sampleRate);
this.sample = new double[numSamples];
this.generatedSnd = new byte[2 * numSamples];
// fill out the array
for (int …Run Code Online (Sandbox Code Playgroud)