4

ボタンを使用して記録を開始および停止するだけの Android アプリケーションを開発しています。スレッドを使用しました。3 つのクラスを作成しました.1 つは記録を開始し、もう 1 つは記録を停止し、メイン クラスは..

問題は、モバイルでファイルを見ることができるが、ファイルが空で、モバイルに「ビデオを再生できません」というメッセージが表示されることです..スレッドで動作させたい..他の方法は必要ありません..

これは私のコードですメインクラス:

public class MediaRecorderSampleActivity extends Activity {


    Button start;
    Button stop ; 
    private MediaRecorder recorder ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button)findViewById(R.id.startbtn); 
        stop = (Button)findViewById(R.id.stopbtn); 
        start.setOnClickListener(new btnClick());
        stop.setOnClickListener(new StopbtnClick());
        }



    class btnClick implements View.OnClickListener {
                public void onClick(View arg0) {
            Log.i("Media", "Start Clicked...");
            Thread startThread = new Thread ( new startRe (recorder));
            Log.i("Media", "start Thread Created");
            startThread.start() ; 
             Log.i("Media", "start Recording");

                }           
    }


    class StopbtnClick implements View.OnClickListener {
        public void onClick(View arg0) {
              Log.i("Media", "Stop Clicked...");
        // TODO Auto-generated method stub
            Thread stopThread = new Thread ( new stopRecording (recorder));
              Log.i("Media", "stop Thread Created");
    stopThread.start();
    Log.i("Media", "stop Recording");
        }

  }

      }

StartRecording クラス

public class startRe implements Runnable {
private MediaRecorder recorder;


startRe( MediaRecorder r ) {
    Log.i("Media", "start cons");
    this.recorder = r ; 

}
public void run() {
    // TODO Auto-generated method stub
    Log.i("Media", "IN RUN start Recording");
    startRecording();
}


public void startRecording() {
    Log.i("Media", "IN Method start Recording");
    recorder = new MediaRecorder();
    Log.i("Media", "create variable");
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Log.i("Media", "1");
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    Log.i("Media", "2");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    Log.i("Media", "3");
    recorder.setOutputFile(getFilePath());
    try{
        Log.i("Media", "prepar");
        recorder.prepare();
        Log.i("Media", "before");
        recorder.start();
        Log.i("Media", "after");
    }catch (Exception e){
        e.printStackTrace();
    }

}


private String getFilePath() {
    String filePath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filePath, "MediaRecorderSample");

    if(!file.exists())
        file.mkdirs();

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp4" );
}


}

停止クラス

 public class stopRecording implements Runnable {
private MediaRecorder recorder ;

public stopRecording(MediaRecorder recorder2) {
    Log.i("Media", "Stop in Cos");
    // TODO Auto-generated constructor stub
    try {
    this.recorder = recorder2 ; }
    catch ( Exception e )
    {       
        Log.i("Media", "Stop out  Cos" + e.getMessage()) ;
        } 

}
public void run() {
    Log.i("Media", "Stop in RUN");
    stopRecording();
    Log.i("Media", "Stop out of RUN");

}
4

1 に答える 1

4

のオブジェクトの使用方法に問題がありますMediaRecorder。Activity クラスでオブジェクトを作成し、そのオブジェクトを two Runnable..

したがって、次の変更を行う必要があります。

次のコードとして、Activity クラスにオブジェクトを作成します。

 private MediaRecorder recorder ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        start = (Button)findViewById(R.id.startbtn); 
        stop = (Button)findViewById(R.id.stopbtn); 
        start.setOnClickListener(new btnClick());
        stop.setOnClickListener(new StopbtnClick());
         // Create the object in Activity so that both Runnable works on the same object...
         recorder = new MediaRecorder();
        }

Runnableすでに行っているように、両方のクラスに同じオブジェクトを渡します。

メソッドでオブジェクトを作成しないでくださいstartRecording()。ローカル オブジェクトが作成され、stopRecording Runnable..

public void startRecording() {
    Log.i("Media", "IN Method start Recording");
    // comment this recorder = new MediaRecorder();
    Log.i("Media", "create variable");
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Log.i("Media", "1");
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    Log.i("Media", "2");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    Log.i("Media", "3");
    recorder.setOutputFile(getFilePath());
    try{
        Log.i("Media", "prepar");
        recorder.prepare();
        Log.i("Media", "before");
        recorder.start();
        Log.i("Media", "after");
    }catch (Exception e){
        e.printStackTrace();
    }

}

試してみて、結果をお知らせください...

于 2013-01-02T09:35:57.740 に答える