我用Frequency generator code产生了频率音。我的应用程序发出约13-14次声音后崩溃了
package com.milind.myapp.gpstrackingservice;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Handler;
import android.util.Log;
public class Tone
{
private static final String TAG = Tone.class.getSimpleName();
// originally from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html
// and modified by Steve Pomeroy <steve@staticfree.info>
private final float duration = .12f; // seconds
private final int sampleRate = 8000;
private final int numSamples = (int)(Math.ceil(duration * sampleRate));
private final double sample[] = new double[numSamples];
private double freqOfTone = 440; // hz
private final byte generatedSnd[] = new byte[2 * numSamples];
Handler handler = new Handler();
public void play(int freq)
{
// Use a new tread as this can take a while
freqOfTone = freq;
genTone();
try {
playSound();
}
catch (Throwable t)
{
Log.w(TAG, "Error in method Tone.play(freq)", t);
}
}
public void playSound()
{
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
public void genTone()
{
// fill out the array
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);
}
}
}
这是堆栈跟踪
2018-10-17 15:14:38.099 3690-3690 / com.milind.myapp.gpstrackingservice W / Tone:方法Tone.play(freq)中的错误 java.lang.IllegalStateException:在未初始化的AudioTrack上调用play()。 在android.media.AudioTrack.play(AudioTrack.java:1962) 在com.milind.myapp.gpstrackingservice.Tone.playSound(Tone.java:47) 在com.milind.myapp.gpstrackingservice.Tone.play(Tone.java:31) 在com.milind.myapp.gpstrackingservice.MainActivity.onLocationChanged(MainActivity.java:155) 在com.milind.myapp.gpstrackingservice.MyService.postLocationChange(MyService.java:207) 在com.milind.myapp.gpstrackingservice.MyService.onLocationChanged(MyService.java:199) 在android.location.LocationManager $ ListenerTransport._handleMessage(LocationManager.java:291) 在android.location.LocationManager $ ListenerTransport.-wrap0(未知 来源:0) 在android.location.LocationManager $ ListenerTransport $ 1.handleMessage(LocationManager.java:236) 在android.os.Handler.dispatchMessage(Handler.java:105) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6944) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:0)
我认为您应该关闭audioTrack-> audioTrack.close();播放声音后。打开的audioTrack会话数是有限的。