gpt4 book ai didi

android - 我想使用 mediaplayer 来录制声音,然后计算 "amplitude"以随着声音的变化上下移动 Bars

转载 作者:行者123 更新时间:2023-11-29 14:02:12 25 4
gpt4 key购买 nike

我想使用 MediaRecorder 来录制声音,然后想计算随着声音的变化上下移动 Bars 的“振幅”。我还没有找到任何合适的解决方案。有什么建议。??编辑:我现在设法解决了问题,问题是我已经尝试使用 getMaxAmplitude() 来使用 Handler 查找振幅,之后我试图使用该值来显示条向上移动它没有工作然后我打印了值getMaxAmplitude() 当然这个值是零。这里可能有什么问题。??这是一段代码 公共(public)无效运行(){ 尝试{

            mRecorder.start();
while (this.mIsRunning) {
// creating these variables here so that
// the mode change can be handled
double amp = getAmplitude();
Message msg = mHandle.obtainMessage(MY_MSG, amp);
mHandle.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = mHandle.obtainMessage(ERROR_MSG,
e.getLocalizedMessage() + "");
mHandle.sendMessage(msg);
}
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}

public double getAmplitude() {
if (mRecorder != null) {
powerDb = 20 * Math.log10(mRecorder.getMaxAmplitude() / 2700.0);
return (powerDb);
}

else {
return 1;
}
}
public Handler mhandle = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MY_MSG:

// mSplModeTV.setText(" " + msg.obj);
if (audioEngineFlag == 1) {
String s1 = msg.obj.toString();
float f = Float.valueOf(s1.trim()).floatValue();
Log.v(TAG, "amplitude=" + f); }

最佳答案

您可以使用计时器每 .5 秒调用一次 getMaxAmplitue()

MyTimer tim = new new MyTimer(30000,500);;
mRecorder.start();
tim.start();
while (this.mIsRunning) {
// creating these variables here so that
// the mode change can be handled
double amp = getAmplitude();
Message msg = mHandle.obtainMessage(MY_MSG, amp);
mHandle.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = mHandle.obtainMessage(ERROR_MSG,
e.getLocalizedMessage() + "");
mHandle.sendMessage(msg);
}
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}

public double getAmplitude() {
if (mRecorder != null) {
powerDb = 20 * Math.log10(mRecorder.getMaxAmplitude() / 2700.0);
return (powerDb);
}

else {
return 1;
}
}
public Handler mhandle = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MY_MSG:

// mSplModeTV.setText(" " + msg.obj);
if (audioEngineFlag == 1) {
String s1 = msg.obj.toString();
float f = Float.valueOf(s1.trim()).floatValue();
Log.v(TAG, "amplitude=" + f); }


public class MyTimer extends CountDownTimer {
public MyTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub

}

@Override
public void onTick(long millisUntilFinished) {
int amplitude = mRecorder.getMaxAmplitude();
Log.i("AMPLITUDE", new Integer(amplitude).toString());
}
}

您可以在计时器中使用此处理程序,每 .5 秒获取一次结果。

关于android - 我想使用 mediaplayer 来录制声音,然后计算 "amplitude"以随着声音的变化上下移动 Bars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8951228/

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