Я использовал код генератора частоты для генерации тона частоты.Мое приложение аварийно завершает работу после генерации тона примерно 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: play (), вызываемая для неинициализированного AudioTrack.на 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.gpstrackingser.MyService.onLocationChanged (MyService.java:199) в android.location.LocationManager $ ListenerTransport._handleMessage (LocationManager.java:291) в android.location.LocationManager $ ListenerTransport.-wrap0 (неизвестный источник: 0 в) и в неизвестный источник: 0 на и$ 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)