开发者

Android - how to do background threading properly?

Was wondering if anyone could help me on background threading on Android.

I have a piece of code that records from the mic of the device and then plays back what it records through the ear piece(on 1.5).

I am trying to run it in a thread but have been unsuccessful in getting it to run as a background thread.

Currently it runs and locks up the activity so that all thats happening is the thread is running and the UI is locked up or appears to be hanging.

Here is the lastest way I tried to do it:

public class LoopProg extends Activity {


boolean isRecording; //currently not used

  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   AudioManager audio_service = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

   audio_service.setSpeakerphoneOn(false);
   audio_service.setMode(AudioManager.MODE_IN_CALL);
   audio_service.setRouting(AudioManager.MODE_NORMAL,
   AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);

   Record record = new Record();  
   record.run();

 }

  public class Record extends Thread
  {


          static final int bufferSize = 200000;
          final short[] buffer = new short[bufferSize];
          short[] readBuffer = new short[bufferSize];

          public void run() {  
            isRecording = true;
            android.os.Process.setThreadPriority
            (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

            int buffersize = AudioRecord.getMinBufferSize(11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

                           AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                           11025,
                                           AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                           AudioFormat.ENCODING_PCM_16BIT,
                                           buffersize);

                           AudioTrack atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                           11025,
                                           AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                           AudioFormat.ENCODING_PCM_16BIT,
                                           buffersize,
                                           AudioTrack.MODE_STREAM);

                           setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);


            开发者_运维百科               atrack.setPlaybackRate(11025);

                           byte[] buffer = new byte[buffersize];
                           arec.startRecording();
                           atrack.play();

                           while(isRecording) {
                                   arec.read(buffer, 0, buffersize);
                                   atrack.write(buffer, 0, buffer.length);
                           }

                           arec.stop();
                           atrack.stop();
                           isRecording = false;
              }
      }
}

I was wondering if anybody could guide me on how to turn this into a background thread? Or prehaps point me to some tutorial that may be relevant that I may have missed?

Thanks in advance


Try calling record.start() instead of .run().

From the Java API Docs:

start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

You also may want to look into AsyncTask.


You shouldn't call Thread.run, call Thread.start

public void run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜