android之相机 VideoSource和AudioSource

public class MediaRecoderActivity extends AppCompatActivity {

    private SurfaceView sv_recoder;
    private MediaRecorder mediaRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_recoder);
        sv_recoder = (SurfaceView) findViewById(R.id.sv_recoder);
        //实例化媒体录制器
        mediaRecorder = new MediaRecorder();

    }
    public  void  start(View view){
        mediaRecorder.reset();
        //设置视频和音频来源

        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置输出格式
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        //设置每秒几栈
        mediaRecorder.setVideoFrameRate(3);

        //音频和视频的编码格式
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        //保存路径

        try {
            mediaRecorder.setOutputFile("/mnt/sdcard/G150725_"+System.currentTimeMillis()+".mp4");
            mediaRecorder.setPreviewDisplay(sv_recoder.getHolder().getSurface());
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
 
  
 
  
<LinearLayout 
    >
    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/sv_recoder"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        android:onClick="start"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"
        android:onClick="stop"
        />
LinearLayout>

你可能感兴趣的:(android)