Android Media Recording using threads

2019-03-31 05:09发布

问题:

I am developing an android application that simply start and stop recording using Buttons. I used threads. I created three classes.. One to start recording.. one to stop recording and the main class..

The problem is that I can see the file in my mobile but it is empty and the mobile give me a msg "Unable to play video" .. I want it to work with the threads.. I dont want other methods..

This is my code The main class:

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");
        }

  }

      }

The StartRecording class

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" );
}


}

The stopClass

 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");

}

回答1:

There is a issue in how you are using the object of the MediaRecorder. You need to create the object in the Activity class and then pass the object to two Runnable..

So you need to make following changes:

Create the object in the Activity class as following code:

 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();
        }

pass the same object to both of the Runnable class as you already doing.

Don't create the object in the startRecording() method since it would create a local object and assign it to a local variable that would not be accessed from the 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();
    }

}

try and let us know the result...