gpt4 book ai didi

android - 是否可以生成小于 1 秒长的音调/音频?还是立即切断播放器?

转载 作者:行者123 更新时间:2023-11-30 04:00:17 25 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,根据您在屏幕上的触摸位置生成音调(目前频率由触摸点的 y 值确定)但是我无法生成更少的音调超过一秒钟而不会使我的应用程序崩溃!我从 the answer here 中获取了代码并调整它在我触摸屏幕时播放,但是当我将持续时间变量更改为小于 1 的值时,应用程序崩溃了!是否可以产生小于 1 秒的音调?

我想创建一个不到一秒的音调的唯一原因是当我暂停或停止音轨时,音频播放器似乎不会立即切断。这些是我演奏和产生音调的方法:

RelativeLayout background;
SoundPool soundPool;
TextView textView;


private final float duration = 1f; // seconds
private final int sampleRate = 8000;//was 8000
private final int numSamples = (int) duration * sampleRate;
private final double sample[] = new double[numSamples];
private final double freqOfTone = 250; // hz

AudioTrack audioTrack ;

int dispWidth = 0, dispHeight = 0;

Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
background = (RelativeLayout) findViewById(R.id.background);
textView = (TextView) findViewById(R.id.textView2);
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
dispWidth = AppHelper.getDisplayWidth(getApplicationContext());
dispHeight = AppHelper.getDisplayHeight(getApplicationContext());
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples,
AudioTrack.MODE_STREAM);
final byte genTone[]= genTone(freqOfTone);

background.setClickable(true);



background.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP)
{
Log.d(LOG_TAG, "Touch up! Stopping tone...");
audioTrack.pause();
audioTrack.flush();

}
else
{
//these apparently cause immediate stop
audioTrack.pause();
audioTrack.flush();

int R = (int) (event.getY()%255);
int G = (int)(event.getX()%255);
int B = (int) ((event.getY()+event.getX())%255);
Log.i(LOG_TAG, "Changine colour to "+R+","+G+","+B);
background.setBackgroundColor(Color.rgb(R, G, B));
int colorFromPressure = (int) (event.getPressure()*255);
textView.setTextColor(Color.rgb(colorFromPressure,colorFromPressure,colorFromPressure));
playSoundAtFrequency(250+event.getY(), event.getX()/dispWidth);
}
return false;
}
});
}

byte[] genTone(double toneFrequency){
final byte generatedSnd[] = new byte[2 * numSamples];
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/toneFrequency));
}

// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (double dVal : sample) {
short val = (short) (dVal * 32767);
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
return generatedSnd;
}


void playSound(final byte[] generatedSnd, float volume){

//these apparently cause immediate stop
audioTrack.pause();
audioTrack.flush();

audioTrack.write(generatedSnd, 0, numSamples);
audioTrack.setStereoVolume(volume, volume);


audioTrack.play();
Log.d(LOG_TAG, "Playing tone...");
}

public void playSoundAtFrequency(final double toneFrequency,float volume)
{

final byte genTone[]= genTone(toneFrequency);
playSound(genTone, volume);

}

最佳答案

我认为您的崩溃是因为使用的缓冲区大小对于您尝试创建的 AudioTrack 来说太小了。

int audioBufferSize = AudioTract.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM-16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, audioBufferSize,
AudioTrack.MODE_STREAM);

然后 numSamples 仍然适用于您的生成缓冲区,AudioTrack 将具有它想要的缓冲区大小。

关于android - 是否可以生成小于 1 秒长的音调/音频?还是立即切断播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12603942/

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