Reputation: 1
I'm using the following code to record the camera in Android that uses the basic Intent
. How can I change it to use MediaRecorder
API instead of this Intent-based approach?
private Uri fileUri;
//...
private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}
// ...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
// play the video given the global fileUri
}
Upvotes: 0
Views: 1312
Reputation: 15936
I don't use the Android SDK (my Java skills are on Windows only), but from a quick research...
See if this tutorial helps you (more info further beow)...
Just replace their audio setup (3GP) with the H264 & AAC setup from the other Question.
Also read the section about "Recording Video" on this article to understand the needed parts.
Also declare permissions in your Manifest.xml. An example of such manifest and related code is here on Github (that one allows RECORD_AUDIO
but you need also a RECORD_VIDEO
so set as):
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
Back to the tutorial, I cannot test their code but something like the below edit should get you started:
public class MainActivity extends Activity
{
MediaRecorder recorder;
File outputfile = null;
static final String TAG = "MediaRecording";
Then inside that class you add useful functions (initialise Recorder, start rec, stop rec, save file)...
public void initRecording(View view) throws IOException
{
//Creating file
File dir = Environment.getExternalStorageDirectory();
try { outputfile = File.createTempFile("video_test_android", ".mp4", dir); }
catch (IOException e)
{
Log.e(TAG, "external storage access error");
return;
}
//# Create a new instance of MediaRecorder
recorder = new MediaRecorder(); //create MediaRecorder object
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//# Video settings
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //contained inside MP4
recorder.setVideoSize(640, 480); //width 640, height 480
recorder.setVideoFrameRate(30); //30 FPS
recorder.setVideoEncodingBitRate(3000000); //adjust this for picture quality
//# Audio settings
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //must always be AAC
recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(outputfile.getAbsolutePath());
recorder.prepare();
}
public void startRecording(View view)
{
recorder.start();
}
public void stopRecording(View view)
{
recorder.stop(); recorder.release();
//# After stopping the recorder...
//# Create the video file and add it to the Media Library.
addRecordingToMediaLibrary();
}
protected void addRecordingToMediaLibrary()
{
//# used to store a set of values that the ContentResolver can process
ContentValues values = new ContentValues(4); //# 4 items (title, date, etc)
long current = System.currentTimeMillis(); //# get recording time (date)
//# size of 4 for values, all in String
values.put(MediaStore.Audio.Media.TITLE, "Android Tina J - " + outputfile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "video/mp4"); //# set MIME type
values.put(MediaStore.Audio.Media.DATA, outputfile.getAbsolutePath()); // data stream for the file
//# provides applications access to the content model
ContentResolver contentResolver = getContentResolver();
//# The content:// style URI for the "primary" external storage volume.
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
//# Request the media scanner to scan a file and add it to the media database
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
//# if you need a notification...
//Toast.makeText(this, "Created Media File : " + newUri, Toast.LENGTH_LONG).show();
}
So to begin a recording, just inititalise the media recorder and do a recorder.start();
to begin recording (camera and mic are used automatically according to your MediaRecorder settings).
Upvotes: 0