如何测量声级计并显示有关噪音的动画列表

how to measue sound meter and to show animation list on noice

我是新 android 开发人员。我正在 android 开发香烟烟雾应用程序。当用户吹入麦克风时使用声音计然后显示动画列表的第一个图像等等。我该怎么做。请帮助我。

这是我的声表课

package com.example.practisework;

import java.io.IOException;

import android.media.MediaRecorder;

public class SoundMeter
    {
        // This file is used to record voice
        // static final private double EMA_FILTER = 0.6;

        private MediaRecorder mRecorder = null;

        // private double mEMA = 0.0;

        public void start()
            {

                if (mRecorder == null)
                    {

                        mRecorder = new MediaRecorder();
                        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecorder.setOutputFile("/dev/null");

                        try
                            {
                                mRecorder.prepare();
                            }
                        catch (IllegalStateException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        catch (IOException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        mRecorder.start();
                        // mEMA = 0.0;
                    }
            }

        public void stop()
            {
                if (mRecorder != null)
                    {
                        mRecorder.stop();
                        mRecorder.release();
                        mRecorder = null;
                    }
            }

        public double getAmplitude()
            {
                if (mRecorder != null)
                    return (mRecorder.getMaxAmplitude() / 2700.0);
                else
                    return 0;
            }
    }

这是我的活动

package com.example.practisework;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class SecondActivity extends Activity implements AnimationListener
    {
        ImageView imageView;
        AnimationDrawable animationDrawable;

        private static final int POLL_INTERVAL = 300;
        private boolean mRunning = false;

        private int mThreshold;
        private SoundMeter mSensor;

        private Handler mHandler = new Handler();

        private Runnable mPollTask = new Runnable()
            {
                public void run()
                    {
                        double amp = mSensor.getAmplitude();

                        if ((amp > mThreshold))
                            {
                                animationDrawable.start();
                            }
                        mHandler.postDelayed(mPollTask, POLL_INTERVAL);
                    }
            };

        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_second);

                imageView = (ImageView) findViewById(R.id.soundMeterImageView);
                imageView.setImageResource(R.anim.smokesoundmeter);
                animationDrawable = (AnimationDrawable) imageView.getDrawable();
                animationDrawable.start();

                mSensor = new SoundMeter();
            }

        @Override
        public void onAnimationEnd(Animation animation)
            {
                // TODO Auto-generated method stub

            }

        @Override
        public void onAnimationRepeat(Animation animation)
            {
                // TODO Auto-generated method stub

            }

        @Override
        public void onAnimationStart(Animation animation)
            {
                // TODO Auto-generated method stub

            }

        @Override
        protected void onResume()
            {
                mThreshold = 10;

                if (!mRunning)
                    {
                        mRunning = true;
                        mSensor.start();
                        mHandler.postDelayed(mPollTask, POLL_INTERVAL);
                    }
                super.onResume();
            }

        @Override
        protected void onStop()
            {
                super.onStop();
                stop();
            }

        private void stop()
            {
                mHandler.removeCallbacks(mPollTask);
                mSensor.stop();
                mRunning = false;
            }

    }

这是我的动画列表

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/cigarrete00"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete01"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete02"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete03"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete04"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete05"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete06"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete07"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete08"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete09"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete10"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete11"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete12"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete13"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete14"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete15"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete16"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete17"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete18"
        android:duration="200">
    </item>
    <item
        android:drawable="@drawable/cigarrete19"
        android:duration="200">
    </item>


</animation-list>

每当用户对着麦克风吹气时,cigeratte 动画应该一个接一个地显示

您可能正在寻找 ffmpeg 库,这将为您提供声音的音高,并在此基础上您将能够显示动画 https://github.com/guardianproject/android-ffmpeg