gpt4 book ai didi

android - 有人可以帮我解决这个在Android上产生频率音调的问题吗

转载 作者:行者123 更新时间:2023-12-03 00:28:42 25 4
gpt4 key购买 nike

我用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: Error in method Tone.play(freq) java.lang.IllegalStateException: play() called on uninitialized AudioTrack. at android.media.AudioTrack.play(AudioTrack.java:1962) at com.milind.myapp.gpstrackingservice.Tone.playSound(Tone.java:47) at com.milind.myapp.gpstrackingservice.Tone.play(Tone.java:31) at com.milind.myapp.gpstrackingservice.MainActivity.onLocationChanged(MainActivity.java:155) at com.milind.myapp.gpstrackingservice.MyService.postLocationChange(MyService.java:207) at com.milind.myapp.gpstrackingservice.MyService.onLocationChanged(MyService.java:199) at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:291) at android.location.LocationManager$ListenerTransport.-wrap0(Unknown Source:0) at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:236) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

最佳答案

我认为您应该关闭audioTrack-> audioTrack.close();播放声音后。打开的audioTrack session 数是有限的。

关于android - 有人可以帮我解决这个在Android上产生频率音调的问题吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52861782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com