2

電話のマイクから音をキャプチャし、1000 サンプルごとの平均値をリアルタイムで画面に表示したいと考えています。これは私がこれまでに得たコードですが、画面に値が表示されず、空白です。私は何を間違っていますか?私は Android 開発に不慣れなので、どんな助けも役に立ちます。

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import android.media.AudioFormat;

public class MainActivity extends Activity {

    public boolean recording;  //variable to start or stop recording
    public TextView averagevalue; //text to appear on screen

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

        averagevalue = (TextView) findViewById(R.id.text1);
    }


   public void Record(){
       AudioRecord recorder;
       short audioData[];
       int bufferSize;

       bufferSize=AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT); //get the buffer size to use with this audio record

recorder = new AudioRecord (AudioSource.MIC,44100,AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

recording=true; //variable to use start or stop recording
audioData = new short [bufferSize]; //short array that pcm data is put into.


while (recording) {  //loop while recording is needed
if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
     recorder.startRecording();  //check to see if the Recorder has stopped or is not recording, and make it record.

else {

recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array
//analyze sound
        int totalAbsValue = 0;
        int samplesquantity = 1000;
        short sample = 0; 
        float averageAbsValue = 0.0f;

     for (int i = 0; i < samplesquantity; i += 1) {
         sample = (short)audioData[i];
         totalAbsValue += Math.abs(sample);
     }
     averageAbsValue = totalAbsValue / samplesquantity;     

     averagevalue.setText(Float.toString(averageAbsValue));                                           

        }//else recorder started

} //while recording

if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
recorder.release(); //release the recorders resources
recorder=null; //set the recorder to be garbage collected.



   }

}

XML ファイルは次のとおりです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>


</RelativeLayout>
4

2 に答える 2

0

int / int は int です。

averageAbsValue = (float)totalAbsValue / samplesquantity;

トリックを行う必要があります。

于 2013-11-11T12:39:08.987 に答える