gpt4 book ai didi

java - 如何让我的动画更流畅 Android

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:16 26 4
gpt4 key购买 nike

我有一个应用程序,它有一个球在屏幕上运行。当球移动到一半时,应用程序会记录一些音频、计算 FFT 并进行一些额外的分析。

这是由 Asynctask 处理的,但是动画仍然会出现短暂的断断续续。

有没有人对如何使其运行更流畅有任何建议?

谢谢

代码如下:

import com.ben.applicationLayer.GameView;
import dataObjectLayer.Sprite;
import dataObjectLayer.MusicalNote;
import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import android.media.AudioFormat;
import android.os.AsyncTask;

public class recorderThread extends AsyncTask<Sprite, Void, Integer> {

short[] audioData;
int bufferSize;
Sprite ballComp;

@Override
protected Integer doInBackground(Sprite... ball) {

MusicalNote note = ball[0].expectedNote;
ballComp = ball[0];
boolean recorded = false;
double frequency;
int sampleRate = 8192;
AudioRecord recorder = instatiateRecorder(sampleRate);
double[] magnitude = new double[1024];
double[] audioDataDoubles = new double[2048];

while (!recorded) { //loop until recording is running

if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED)
// check to see if the recorder has initialized yet.
{
if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
recorder.startRecording();
//check to see if the Recorder has stopped or is not recording, and make it record.
else {
double max_index;
recorder.read(audioData,0,bufferSize);
//read the PCM audio data into the audioData array

computeFFT(audioDataDoubles, magnitude);

max_index = getLargestPeakIndex(magnitude);

frequency = getFrequencyFromIndex(max_index, sampleRate);

//checks if correct frequency, assigns number
int correctNo = correctNumber(frequency, note);

checkIfMultipleNotes(correctNo, max_index, frequency, sampleRate, magnitude, note);

if (audioDataIsNotEmpty())
recorded = true;
if (correctNo!=1)
return correctNo;
}
}
else
{
recorded = false;
recorder = instatiateRecorder(sampleRate);
}
}

if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING)
{
killRecorder(recorder);
}

return 1;
}


private void killRecorder(AudioRecord recorder) {
recorder.stop(); //stop the recorder before ending the thread
recorder.release(); //release the recorders resources
recorder=null; //set the recorder to be garbage collected
}

@Override
protected void onPostExecute(Integer result) {
(result == 2)
GameView.score++;
}

private AudioRecord instatiateRecorder(int sampleRate) {
bufferSize= AudioRecord.getMinBufferSize(sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT)*2;
//get the buffer size to use with this audio record

AudioRecord recorder = new AudioRecord (AudioSource.MIC,sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

audioData = new short [bufferSize]; //short array that pcm data is put into.
return recorder;
}

}

最佳答案

使用 Asynctask 是按照您想要做的事情进行的。但是,我建议使用线程池。然后,您可以将动画作为一项任务添加,将录音作为一项任务添加,将 FFT 作为另一项任务添加,将附加分析作为一项任务添加。

短暂的卡顿可能是在动画线程中为录制分配资源的结果。使用池意味着您不必在创建线程来运行音频任务时暂停。显然,一些代码可以方便地完全理解您的问题。

看看:

调度线程池执行器 http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

线程池执行器 http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

您可能想要执行的操作的一个非常简单的示例:

在您的 Activity 中,您可以定义它来启动任务

ExecutorService threadPool = new ScheduledThreadPoolExecutor(2);
Recording record = new Recording();
Ball ball = new Ball(threadPool, record);
threadPool.submit(ball);

球.java

import java.util.concurrent.ExecutorService;

public class Ball implements Runnable {
private final Recording record;
private final ExecutorService threadPool;

private boolean condition;
private boolean active;

public Ball(ExecutorService threadPool, Recording record) {
this.record = record;
this.threadPool = threadPool;
this.active = true;
}

@Override public void run() {
while (this.active) {
moveBall();
if (isBallHalfway()) {
threadPool.submit(record);
}
}
}

private boolean isBallHalfway() {
return condition; // Condition for determining when ball is halfway
}

private void moveBall() {
// Code to move the ball
}
}

录音.java

public class Recording implements Runnable {
@Override public void run() {
// Do recording tasks here
}
}

关于java - 如何让我的动画更流畅 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8976039/

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