2011-01-30 7 views
1

AudioRecordでオーディオサンプルを録音しようとしています。私は録音方法を実行するためにスレッドを使用しました。スレッドに問題があるようです。なぜどんなアイデア?オーディオを録音するときのスレッドの問題

package com.tecmark; 

import java.io.BufferedOutputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import android.app.Activity; 
import android.media.AudioFormat; 
import android.media.AudioRecord; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 

public class recorder extends Activity { 




    private Thread thread; 
    private boolean isRecording; 
    private AudioRecord recorder; 
    private FileOutputStream os; 
    private BufferedOutputStream bos; 
    private DataOutputStream dos; 
    private TextView text; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 



    } 



    public void onClickPlay(View v){ 


    } 


    public void record(){ 


     Log.i("inside record method", "******"); 

     text = (TextView)findViewById(R.id.TextView01); 
     text.setText("recording"); 

     int audioSource = MediaRecorder.AudioSource.MIC; 
     int sampleRate = 22050; 
     int channel = AudioFormat.CHANNEL_CONFIGURATION_MONO; 
     int encoding = AudioFormat.ENCODING_PCM_16BIT; 
     int result = 0; 



     File path = Environment.getExternalStorageDirectory(); 
     Log.v("file path", ""+path.getAbsolutePath()); 

     File file = new File(path, "test.wav"); 

     if(file.exists()){ 
      file.delete(); 
     } 

     path.mkdirs(); 
     Log.v("file path", ""+file.getAbsolutePath()); 

     recorder = new AudioRecord(audioSource, sampleRate,channel,encoding, 
        AudioRecord.getMinBufferSize(sampleRate, channel,encoding)); 
        Log.i("recorder", "recorder object created"); 

      try { 
       os = new FileOutputStream(file); 
       bos = new BufferedOutputStream(os);    
       dos = new DataOutputStream(bos); 
      } catch (Exception e1) { 

       e1.printStackTrace(); 
      } 


      int bufferSize = AudioRecord.getMinBufferSize(sampleRate,channel,encoding); 
      byte[] buffer = new byte[bufferSize]; 

      recorder.startRecording(); 
      isRecording = true; 

     try{ 

     while (isRecording){ 



       result = recorder.read(buffer, 0, bufferSize); 
       for(int a=0; a<result;a++){ 
        dos.write(buffer[a]); 

        if(!isRecording){ 
         recorder.stop(); 

         break; 
        } 

       } 

      } 
      dos.flush(); 
      dos.close(); 

     }catch(Exception e) { 

      e.printStackTrace(); 
     } 

    }// end of record method 




     public void onClickStop(View v){ 
       Log.v("onClickStop", "stop clicked"); 

       isRecording=false; 


      } 



    public void onClickReverse(View v){ 
     Log.v("onClickReverse", "reverse clicked"); 


    } 

    public void onClickRecord(View v){ 

     thread = new Thread(new Runnable() { 


      public void run() { 
       isRecording = true; 
       record(); 
      } 
     }); 

     thread.start(); 

     isRecording = false; 


    } 

}//end of class 

ここはエラーです。

01-30 12:27:50.434: ERROR/AndroidRuntime(2226): Uncaught handler: thread Thread-8 exiting due to uncaught exception 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.view.ViewRoot.checkThread(ViewRoot.java:2706) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.view.ViewRoot.invalidateChild(ViewRoot.java:573) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:599) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:2396) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.view.View.invalidate(View.java:4945) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.widget.TextView.checkForRelayout(TextView.java:5366) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.widget.TextView.setText(TextView.java:2684) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.widget.TextView.setText(TextView.java:2552) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at android.widget.TextView.setText(TextView.java:2527) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at com.tecmark.recorder.record(recorder.java:54) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at com.tecmark.recorder$1.run(recorder.java:151) 
01-30 12:27:50.459: ERROR/AndroidRuntime(2226):  at java.lang.Thread.run(Thread.java:1096) 

答えて

3

あなたはrecord()findViewById()を呼び出しているが、あなたはあなたの新しいスレッドのrun()メソッド内でそれを呼び出すようrecord()は、UIスレッドで実行されていません。エラーが言うように:

android.view.ViewRoot $ CalledFromWrongThreadException:

あなたrecord()からfindViewById()text.setText()への呼び出しを削除し、そのビュー触れることができるビュー階層を作成しただけ元のスレッド方法。代わりに、新しいスレッドを作成する前に、UI要素をonClickRecord()に設定してみてください。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    text = (TextView)findViewById(R.id.TextView01); 
} 
... 
public void onClickRecord(View v){ 
    text.setText("recording"); 
    thread = new Thread(new Runnable() { 
     public void run() { 
      isRecording = true; 
      record(); 
     } 
    }); 
    thread.start(); 
    isRecording = false; 
} 
+0

okありがとうございますが、私はsetContentViewでoncreateメソッドで設定されている4つのボタンを持っています。これは、私はどのようにアプリケーションをレイアウトします。私はどのようにボタンをレコードメソッドに入れますか?これはあなたの意味ですか? – turtleboy

+1

@turtleboy私は自分の答えを更新しました。最初に 'onCreate()'メソッドで 'TextView'への参照を作成して設定する必要があります。それから、あなたが更新したこと(つまり、 'text.setText()'の呼び出し)がUIスレッドで発生していることを確認してください。 –

+0

@ dave.c plz私の質問を見て、私が読んでいる別のスレッドを持っているのに私のUIスレッドがハングする理由を教えてください..... http://stackoverflow.com/questions/9413998/live-audio-recording and-in-playing-android-and-thread-callback-handling – aProgrammer

関連する問題