539
No
class MainActivity
package kr.co.braverokmc.myvideorecorder;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
SurfaceView surfaceView;
//서피스 제어
SurfaceHolder holder;
// 음성 녹음 뿐만 아니라 동영상 녹음 가능
MediaRecorder recorder ;
String path="/sdcard/recored_video.mp4";
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView=(SurfaceView)findViewById(R.id.surfaceView);
holder=surfaceView.getHolder();
}
//동영상 녹음시작
public void onButton1Clicked(View v){
try {
recorder =new MediaRecorder();
// 녹음기 설정
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//카메라 설정
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//출력 형식 설정
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// default 내장 default 형식으로 설정
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setOutputFile(path);
//화면에 어디에 뿌려질것인가 정보를 서피스 홀더 객체가 가져옴
recorder.setPreviewDisplay(holder.getSurface());
recorder.prepare();
recorder.start();
tostShow("녹화가 시작 되었습니다.");
}catch (Exception e){
e.printStackTrace();
}
}
//동영상 녹음중지
public void onButton2Clicked(View v){
if(recorder!=null){
recorder.stop();
recorder.release();
recorder=null;
tostShow("녹화가 중지 되었습니다.");
}
}
private void tostShow(String data){
if(toast==null){
toast=Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT);
}else {
toast.setText(data);
}
toast.show();
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.myvideorecorder.MainActivity">
<Button
android:text="녹화시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onButton1Clicked"
android:id="@+id/button" />
<Button
android:text="녹화중지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:onClick="onButton2Clicked"
android:id="@+id/button2" />
<SurfaceView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:id="@+id/surfaceView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
권한 허가
AndroidMainfest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
댓글 ( 4)
댓글 남기기