如何测量声级计并在噪声上显示动画列表

时间:2015-01-02 05:37:37

标签: java android eclipse animation

我是新的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动画应逐一显示

1 个答案:

答案 0 :(得分:0)

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