2

我想使用 MediaRecorder 来录制声音,然后想计算随着声音的变化上下移动 Bars 的“幅度”。我还没有建立任何合适的解决方案。有什么建议么。??已编辑:我现在设法把手伸进去,问题是我已经尝试使用 Handler getMaxAmplitude() 来查找幅度,之后我尝试使用该值来显示条向上移动它不起作用然后我打印了getMaxAmplitude() 的值肯定为零。这里可能有什么问题。??这是一段代码 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); }
4

1 回答 1

0

您可以在每 0.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());
        }
    }

您可以在计时器中使用此处理程序以在每 0.5 秒后获取结果。

于 2013-01-10T10:29:09.570 回答