반응형
오디오 녹음에 사용하던 MediaRecorder 객체는 동영상 녹화에도 사용할 수 있다
동영상 녹화가 오디오 녹음과 다른 점은 영상을 녹화하기 위한 입력 소스로 카메라를 지정하여 사용자가 카메라 미리보기를 할 수 있도록 만들어주어야 한다는 점이다
입력 소스로 지정할 수 있는 마이크는 MIC라는 상수로 정의되어 있고, 카메라는 CAMERA라는 상수로 정의되어 있다
MediaRecorder.AudioSource.MIC
MediaRecorder.VidoeSource.CAMERA
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:layout_gravity="center"
android:text="녹화시작"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:layout_gravity="center"
android:text="녹화중지"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:layout_gravity="center"
android:text="재생시작"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:layout_gravity="center"
android:text="재생중지"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a76_videorecorder">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.A76_VideoRecorder">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity
package com.example.a76_videorecorder;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
import java.io.File;
import java.util.List;
public class MainActivity extends AppCompatActivity {
MediaPlayer player;
MediaRecorder recorder;
File file;
String filename;
SurfaceHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 미리보기 구현을 위해 SurfaceView 객체 생성
SurfaceView surface = new SurfaceView(this);
holder = surface.getHolder();
FrameLayout frame = findViewById(R.id.container);
frame.addView(surface);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRecording();
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopRecording();
}
});
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startPlay();
}
});
Button button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlay();
}
});
file = getOutputFile();
if (file != null) {
filename = file.getAbsolutePath();
}
AndPermission.with(this)
.runtime()
.permission(
Permission.CAMERA,
Permission.RECORD_AUDIO,
Permission.READ_EXTERNAL_STORAGE,
Permission.WRITE_EXTERNAL_STORAGE)
.onGranted(new Action<List<String>>() {
@Override
public void onAction(List<String> permissions) {
showToast("허용된 권한 갯수 : " + permissions.size());
}
})
.onDenied(new Action<List<String>>() {
@Override
public void onAction(List<String> permissions) {
showToast("거부된 권한 갯수 : " + permissions.size());
}
})
.start();
}
public void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
public File getOutputFile() {
File mediaFile = null;
try {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyApp");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "recorded.mp4");
} catch(Exception e) {
e.printStackTrace();
}
return mediaFile;
}
public void startRecording() {
if (recorder == null) {
recorder = new MediaRecorder();
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setOutputFile(filename);
// MediaRecorder에 미리보기 화면을 보여줄 객체 설정
recorder.setPreviewDisplay(holder.getSurface());
try {
// 녹화 시작
recorder.prepare();
recorder.start();
} catch (Exception e) {
e.printStackTrace();
recorder.release();
recorder = null;
}
}
public void stopRecording() {
if (recorder == null) {
return;
}
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
ContentValues values = new ContentValues(10);
values.put(MediaStore.MediaColumns.TITLE, "RecordedVideo");
values.put(MediaStore.Audio.Media.ALBUM, "Video Album");
values.put(MediaStore.Audio.Media.ARTIST, "Mike");
values.put(MediaStore.Audio.Media.DISPLAY_NAME, "Recorded Video");
values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
values.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
values.put(MediaStore.Audio.Media.DATA, filename);
Uri videoUri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
if (videoUri == null) {
Log.d("SampleVideoRecorder", "Video insert failed.");
return;
}
// 미디어 앨범에 녹화된 동영상 저장, 내용 제공자 사용 필요
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, videoUri));
}
public void startPlay() {
if (player == null) {
player = new MediaPlayer();
}
try {
player.setDataSource(filename);
player.setDisplay(holder);
player.prepare();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopPlay() {
if (player == null) {
return;
}
player.stop();
player.release();
player = null;
}
}
반응형
'안드로이드' 카테고리의 다른 글
GPS로 위치 확인 (0) | 2021.11.08 |
---|---|
유튜브 영상 재생 (0) | 2021.11.06 |
오디오 녹음 후 저장 (0) | 2021.11.05 |
동영상 재생 (0) | 2021.11.05 |
음악 파일 재생 (0) | 2021.11.05 |