我不使用 Android SDK(我的 Java 技能仅适用于 Windows),但通过快速研究...
看看this tutorial 是否对您有帮助(更多信息请见下文)...
只需使用来自other Question 的 H264 和 AAC 设置替换他们的音频设置 (3GP)。
另请阅读this article 上关于“录制视频”的部分以了解所需部分。
还有 declare permissions 在您的 Manifest.xml 中。这种清单和相关代码的一个示例是here on Github(允许RECORD_AUDIO,但您还需要一个RECORD_VIDEO,因此设置为):
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
回到教程,我无法测试他们的代码,但下面的编辑应该可以帮助你开始:
public class MainActivity extends Activity
{
MediaRecorder recorder;
File outputfile = null;
static final String TAG = "MediaRecording";
然后在该类中添加有用的功能(初始化记录器、启动记录、停止记录、保存文件)...
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();
}
所以要开始录制,只需初始化媒体录制器并发送recorder.start(); 即可开始录制(相机和麦克风会根据您的 MediaRecorder 设置自动使用)。