我想使用媒体播放器录制声音,然后想计算声音的“振幅”用于随着声音的变化上下移动小节
我想使用 MediaRecorder 来录制声音,然后想计算随着声音的变化上下移动 Bars 的“幅度”。我还没有找到任何合适的解决方案。有什么建议吗??编辑:我现在设法把手伸进去,问题是我尝试使用 getMaxAmplitude() 来使用处理程序查找振幅,之后我尝试使用该值来显示条向上移动,但它不起作用,然后我打印了值getMaxAmplitude() 的值肯定为零。这里可能出了什么问题。?这是一段代码 公共无效运行(){ 尝试 {
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); }
I want to use MediaRecorder for recording the sounds and then want to calculate the "amplitude" for moving Bars up and down with the change in sound.I haven't Founded any proper solution yet. any suggestions.?? Edited: I managed to get my hand into it now the problem is i have Tried getMaxAmplitude() to find amplitude using Handler after that i am trying to use that value to show a bar movement up it did nt work then i printed the value of getMaxAmplitude() certainly the value comes out to be zero.what could be wrong here.?? Here is the piece of code
public void run() {
try {
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); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用计时器每 0.5 秒后调用 getMaxAmplitue()
您可以在计时器中使用此处理程序以每 0.5 秒后获取结果。
you can use timer to call getMaxAmplitue() after every .5 sec
You can use this handler in timer to get the result after every .5 sec.