0

我从 Pro Android media 这本书中得到了帮助...

这是代码:

public class MicMeter extends Activity implements OnClickListener {


RecordAudio recordTask;
int blocksize = 256;
int frequency = 8000;
int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
TextView txt;
Button start;
boolean started = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mic_meter);
    start = (Button)findViewById(R.id.button1);
    txt = (TextView)findViewById(R.id.textView1);

    start.setOnClickListener(this);


}


private class RecordAudio extends AsyncTask <Void,double[],Void>{

    @Override
    protected Void doInBackground(Void... params) {
        try{
        int bufferSize = AudioRecord.getMinBufferSize(frequency,channelConfig,audioEncoding);
        AudioRecord audioRecord = new AudioRecord( MediaRecorder.AudioSource.MIC, frequency, channelConfig, audioEncoding, bufferSize);

        short[] buffer = new short[blocksize];
        double[] meter = new double[blocksize];

        audioRecord.startRecording();

        while(started){
            int bufferReadResult = audioRecord.read(buffer, 0, blocksize);

            for (int i = 0; i < blocksize && i < bufferReadResult; i++) { 
                meter[i] = (double) buffer[i] / 32768.0; // signed 16 bit
            }
            publishProgress(meter);
        }
        audioRecord.stop();

    }catch (Throwable t) {
        Log.e("AudioRecord","RecordingFail");
    }

        return null;
    }

    @Override
    protected void onProgressUpdate(double[]... meter) {

        for(int i = 0 ; i < meter[0].length ; i++){
            double[] helper = meter[i];
            txt.setText(Double.toString(helper));
        }

    }


}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(started){
        recordTask.cancel(true);
    }else{
        started = true;
        recordTask = new RecordAudio();
        recordTask.execute();

    }
}

}

当我按下按钮时。它显示 255.0 然后它没有响应......有没有办法解决它?有没有更好的版本?

感谢

4

1 回答 1

0
for(int i = 0 ; i < meter[0].length ; i++){
    double helper = i;
    txt.setText(Double.toString(helper));

setText 覆盖旧值。所以只有最后一个电话会显示。最后一次调用将其设置为 helper,它始终是meter[0].length。由于这是一个固定的数字,它不会改变。

于 2013-04-09T01:42:58.863 回答