Android的媒体录制使用线程(Android Media Recording using thr

2019-08-05 06:08发布

我正在开发一个Android应用程序,简单地启动和使用按钮停止录制。 我使用的线程。 我创建了三个班。其中,开始录制..一个停止录制和主类..

问题是,我可以看到我的手机文件,但它是空的,移动给我一个消息“无法播放视频” ...我希望它与线程工作..我不想其他方法..

这是我的代码的主类:

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


}

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

}

Answer 1:

有一个在你是如何使用的对象问题MediaRecorder 。 您需要创建在活动类的对象,然后将对象传递两个Runnable ..

所以,你需要做以下修改:

创建在活动类如以下代码的对象:

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

}

尝试,让我们知道结果...



文章来源: Android Media Recording using threads