【问题标题】:I want to use mediaplayer for recording the sounds and then want to calculate the "amplitude" for moving Bars up and down with the change in sound我想使用 mediaplayer 来录制声音,然后想计算随着声音的变化上下移动 Bars 的“幅度”
【发布时间】:2012-01-21 06:41:21
【问题描述】:

我想使用 MediaRecorder 录制声音,然后想计算随着声音变化上下移动 Bars 的“幅度”。我还没有建立任何合适的解决方案。有什么建议么。??已编辑:我现在设法把手伸进去,问题是我已经尝试使用 Handler 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); }

【问题讨论】:

  • 太好了,没有人可以帮忙..!!!至少让我知道该怎么做.. 如果我想在每 0.5 秒后调用 getMaxAmplitue()。在处理程序中,我怎样才能连续获得读数..??

标签: android handler android-mediarecorder


【解决方案1】:

您可以使用计时器在每 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 秒后获取结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    相关资源
    最近更新 更多