我正在尝试制作一个使用相机应用程序录制视频的应用程序,然后将该视频保存在SD卡上,以便我可以播放它。我有一些代码,但由于我是Android的初学者,我迷失了如何继续。
我的活动:
public class Camcorder extends Activity {
private CamcorderView camcorderView;
private boolean recording = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//irrelevant code
camcorderView = (CamcorderView) findViewById(R.id.camcorder_preview);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
if (recording) {
camcorderView.stopRecording();
finish();
} else {
recording = true;
camcorderView.startRecording();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
CamcorderView类:
public class CamcorderView extends SurfaceView implements
SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
String outputFile = "/sdcard/default.mp4";
public CamcorderView(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
// recorder.setVideoSize(480, 320);
// recorder.setVideoFrameRate(15);
// recorder.setMaxDuration(10000);
}
public void surfaceCreated(SurfaceHolder holder) {
recorder.setOutputFile(outputFile);
recorder.setPreviewDisplay(holder.getSurface());
if (recorder != null) {
try {
recorder.prepare();
} catch (IllegalStateException e) {
Log.e("IllegalStateException", e.toString());
} catch (IOException e) {
Log.e("IOException", e.toString());
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void setOutputFile(String filename)
{
outputFile = filename;
recorder.setOutputFile(filename);
}
public void startRecording()
{
recorder.start();
}
public void stopRecording()
{
recorder.stop();
recorder.release();
}
}
答案 0 :(得分:6)
答案 1 :(得分:3)
使用这个简单的代码
在android中录制视频非常简单首先在按钮上单击简单启动意图
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent,
CAMERA_REQUEST_CODE_VEDIO);
}
比onActivityResult
方法
Uri videoUri = data.getData();
path = Utils.getRealPathFromURI(videoUri, this);
manageVideo(path);///What ever you want to do with your vedio
最后为清单添加权限
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
希望能帮助其他寻求帮助的人:)
由于
答案 2 :(得分:1)
答案 3 :(得分:1)
Uri contentUri="You record video uri";
try {
Date date= new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", date);
String Video_DIRECTORY = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + getString(R.string.app_name) + "/video/";
File storeDirectory = new File(Video_DIRECTORY);
try {
if (storeDirectory.exists() == false) {
storeDirectory.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
File storeDirectory12 = new File(storeDirectory,date+".mp4");
InputStream inputStream = getContentResolver().openInputStream(contentUri);
FileOutputStream fileOutputStream = new FileOutputStream(storeDirectory12);
copyStream(inputStream, fileOutputStream);
fileOutputStream.close();
inputStream.close();
} catch (FileNotFoundException e) {
Log.e("Exception", "" + e);
} catch (IOException e) {
Log.e("Exception", "" + e);
}
答案 4 :(得分:0)
// Show only Brummies
this.myReadData.ApplyFilter(person => person.Address.City == "Birmingham");
// Remove the filter, show everyone again
this.myReadData.RemoveFilter();