线程执行之前

时间:2012-08-23 05:06:58

标签: android android-widget android-2.2-froyo

我在调用新帖子时遇到一些问题。

我正在制作录音应用程序,我在不同的主题中调用录制/播放。

有一个按钮可以开始录制。我试图通过handler.post对象和方法用新文本更新按钮。

问题是更新需要很长时间。在线程运行+5秒之后,文本才会更新。

有人能帮帮我吗?请?

  package com.EJH.Industries.microkr;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaSyncEvent;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
//CLASS VARIABLES
    //CHAR SEQUENCE
    CharSequence easyChar = "PLAYING";
    public Handler textViewHandler = new Handler();

    //CREATE THE RECORDING OBJECT
    int audioSrc = MediaRecorder.AudioSource.MIC;
    int sampleRate = 44100;
    int chanConfig = AudioFormat.CHANNEL_IN_MONO;
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    int getMinBuffSize = 200*AudioRecord.getMinBufferSize(sampleRate, chanConfig, audioFormat);
    int minBuffSize = (int) getMinBuffSize;
    short audioBuff[] = new short[minBuffSize];
    public AudioRecord micRecorder = new AudioRecord(audioSrc, 22050, chanConfig, audioFormat, minBuffSize);



    //CREATE THE PLAYBACK OBJECT
    int streamType = AudioManager.STREAM_MUSIC;
    int playMode = AudioTrack.MODE_STREAM;
    int playChanConfig = AudioFormat. CHANNEL_OUT_MONO;
    public AudioTrack speakerPlay = new AudioTrack(streamType, sampleRate, playChanConfig, audioFormat, 8192, playMode);



    public void startRec(){
        micRecorder.startRecording();
        micRecorder.read(audioBuff, 0, minBuffSize);
        micRecorder.stop();
        micRecorder.release();
    }

    public void startPlayback(){

        speakerPlay.play();
        speakerPlay.write(audioBuff, 0, minBuffSize);
        speakerPlay.stop();

    }









    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button startBtn = (Button) findViewById(R.id.startButton);
        startBtn.setOnClickListener(new OnClickListener(){

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


                Thread recThread = new Thread( new Runnable(){

                    public void run() {
                        // TODO Auto-generated method stub


                        textViewHandler.post(new Runnable () {
                            public void run(){
                                startBtn.setText("Recording!");
                            }

                        });


                        startRec();
                    }

                });

                // RUN RECORDING FUNCTION
                recThread.run();
                try {
                    recThread.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                Thread playThread = new Thread( new Runnable(){

                    public void run() {
                        // TODO Auto-generated method stub

                        startPlayback();
                    }

                });





                playThread.run();
                try {
                    playThread.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

        });

    ///////END onCreate//////////    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

1 个答案:

答案 0 :(得分:0)

我认为是因为你首先调用startRec()然后运行recThread..try来修改你的代码,如下所示...这是从recThread.run()行上面删除你的startRec()函数并添加尝试捕捉后,它帮助了你......

Thread recThread = new Thread( new Runnable(){

                    public void run() {
                        // TODO Auto-generated method stub


                        textViewHandler.post(new Runnable () {
                            public void run(){
                                startBtn.setText("Recording!");
                            }

                        });

                  //remove it from here..
                       // startRec();
                    }

                });

     // RUN RECORDING FUNCTION
                recThread.run();
                try {
                    recThread.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    //add here..
                startRec();