2

MediaRecorder を使用してサウンドを録音し、サウンドの変化に合わせてバーを上下に移動するための「振幅」を計算したいと考えています。まだ適切なソリューションを見つけていません。助言がありますか。??編集: 私はなんとか手に入れることができました。問題は、 getMaxAmplitude() を使用して Handler を使用して振幅を見つけようとした後、その値を使用してバーの動きを表示しようとしているのですが、機能しませんでした。 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 に答える