/
VideoRecorderActivity.java
executable file
·237 lines (195 loc) · 6.44 KB
/
VideoRecorderActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.nus.edu.cs5248.videocapture;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import com.nus.edu.cs5248.pipeline.Storage;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
public class VideoRecorderActivity extends Activity {
private static final int MEDIA_TYPE_VIDEO = 2;
private static final String TAG = "VideoRecorderActivity";
private static final String FILE_PREFIX = "cs5248";
private Camera myCamera;
private SurfaceView surfaceView;
private MediaRecorder mediaRecorder;
boolean recording;
private TextView statusView;
private Button startButton;
private Handler recordingTimerHandler;
private long recordingLength;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
recording = false;
setContentView(R.layout.activity_main);
statusView = (TextView) findViewById(R.id.textViewStatus);
// Get Camera for preview
myCamera = getCameraInstance();
if (myCamera == null) {
statusView.setText("Error getting Camera");
}
surfaceView = new CameraSurfaceView(this, myCamera);
surfaceView.setKeepScreenOn(true);
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.videoView);
frameLayout.addView(surfaceView);
startButton = (Button) findViewById(R.id.buttonStrat);
this.recordingTimerHandler = new Handler();
}
@Override
protected void onPause() {
super.onPause();
if(recording){
stopRecording();
}else{
statusView.setText("OnPause");
releaseMediaRecorder(); // if you are using MediaRecorder, release it
// release the camera immediately on pause event
startButton.setText("Record");
}
releaseCamera();
}
@Override
protected void onResume(){
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void startButtonClicked(View view) {
if (!recording) {
releaseCamera();
if (!prepareMediaRecorder()) {
Log.d(TAG, "fail in prepareMediaRecorder()");
finish();
}
mediaRecorder.start();
startRecordingTimer();
recording = true;
statusView.setText("Starting recording...");
startButton.setText("Stop");
} else {
stopRecording();
}
}
public void PreViewButtonClicked(View view){
Intent i = new Intent(VideoRecorderActivity.this, VideoPreview.class);
startActivity(i);
}
private void stopRecording(){
if (recording) {
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
this.recordingTimerHandler.removeCallbacks(this.recordingTimerUpdater);
recording = false;
statusView.setText("Recording Stopped");
startButton.setText("Record");
}
}
private Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
System.out.println(e);
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private boolean prepareMediaRecorder() {
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
Log.e(TAG, e.getMessage());
return false;
} catch (IOException e) {
releaseMediaRecorder();
Log.e(TAG, e.getMessage());
return false;
}
return true;
}
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
myCamera.lock(); // lock camera for later use
}
}
private void releaseCamera() {
if (myCamera != null) {
myCamera.release(); // release the camera for other applications
myCamera = null;
}
}
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = Storage.getMediaFolder(true);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
if (type == MEDIA_TYPE_VIDEO) {
return new File(mediaStorageDir.getPath() + File.separator
+ FILE_PREFIX + "_" + timeStamp + ".mp4");
}
return null;
}
private void startRecordingTimer() {
this.recordingLength = 0;
this.recordingTimerUpdater.run();
}
private void onRecordingTimerTick() {
final long h = TimeUnit.SECONDS.toHours(this.recordingLength);
final long m = TimeUnit.SECONDS.toMinutes(this.recordingLength) - (TimeUnit.SECONDS.toHours(this.recordingLength)* 60);
final long s = TimeUnit.SECONDS.toSeconds(this.recordingLength) - (TimeUnit.SECONDS.toMinutes(this.recordingLength) *60);
++this.recordingLength;
updateRecordingLength(h, m, s);
}
//UI THREAD ONLY
private void updateRecordingLength(final long h, final long m, final long s) {
if (h > 0) {
this.statusView.setText(String.format("Recording %02d:%02d:%02d", h, m, s));
} else {
this.statusView.setText(String.format("Recording %02d:%02d", m, s));
}
}
Runnable recordingTimerUpdater = new Runnable() {
public void run() {
onRecordingTimerTick();
recordingTimerHandler.postDelayed(recordingTimerUpdater, 1000);
}
};
}