Android 将视频录制到 SDCard
我正在尝试将视频录制到 SD 卡中。 但每次我单击按钮进行录制时,它都会显示“意外停止”错误。 不太确定哪里出了问题。我不确定 setOutputFile 的路径是否是我应该指示的方式。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
cam = new cameraview(this);
((FrameLayout) findViewById(R.id.preview)).addView(cam);
// Create A Preview View
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(toogleButtonFlag )
{
startRecording();
toogleButtonFlag = false;
}
else{
stopRecording();
toogleButtonFlag = true;
}
}
});
}
public void startRecording(){
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setOutputFile("/sdcard/.3pg");
try{
recorder.prepare();
}
catch(IOException e)
{
e.printStackTrace();
recorder.reset();
recorder.release();
}
recorder.start(); // Recording is now started
}
public void stopRecording(){
recorder.stop();
recorder.reset();
recorder.release();
}
I am trying to record a video into sdCard.
But everytime I click on the Button to record, it shows "stopped unexpectedly" error.
Not too sure where went wrong. I am not sure if setOutputFile's path is how I should indicate.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
cam = new cameraview(this);
((FrameLayout) findViewById(R.id.preview)).addView(cam);
// Create A Preview View
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(toogleButtonFlag )
{
startRecording();
toogleButtonFlag = false;
}
else{
stopRecording();
toogleButtonFlag = true;
}
}
});
}
public void startRecording(){
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setOutputFile("/sdcard/.3pg");
try{
recorder.prepare();
}
catch(IOException e)
{
e.printStackTrace();
recorder.reset();
recorder.release();
}
recorder.start(); // Recording is now started
}
public void stopRecording(){
recorder.stop();
recorder.reset();
recorder.release();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您拥有
WRITE_EXTERNAL_STORAGE
权限您的输出文件假定 SD 卡位于
/sdcard
,这在某些设备和 Android 版本上不正确 - 请使用Environment.getExternalStorageDirectory()
您的输出文件缺少文件名
您的输出文件的扩展名拼写错误
Make sure you have the
WRITE_EXTERNAL_STORAGE
permissionYour output file assumes the SD card is at
/sdcard
, which is incorrect on some devices and Android versions -- please useEnvironment.getExternalStorageDirectory()
Your output file lacks a file name
Your output file has a mis-spelled extension