2015-01-02 35 views
0

我是新的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动画应该表现出一个接一个,每当用户吹入麦克风

回答

0

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

+0

我已经阅读过这个。但它不适合我的解决方案。你能给我发一个可能的代码来解决我的问题吗? – user3588072