我想使用媒体播放器录制声音,然后想计算声音的“振幅”用于随着声音的变化上下移动小节

发布于 2024-12-28 08:06:44 字数 1655 浏览 3 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2025-01-04 08:06:44

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

you can use timer to call getMaxAmplitue() after every .5 sec

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());
        }
    }

You can use this handler in timer to get the result after every .5 sec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文