gpt4 book ai didi

android - 在无效状态 : 4 中调用 MediaRecorder 启动

转载 作者:行者123 更新时间:2023-11-29 21:07:04 26 4
gpt4 key购买 nike

我刚刚开始涉足 Android/Glass 开发,我正在尝试添加基本的视频录制功能。目前,Glass 将你限制为 10 秒,除非你再次点击/按下按钮,我想绕过这个,只需要一个开放式录像机。

我跟着两个摄像头API演练:

并引用了另一个 project :

到目前为止,我的代码如下所示:

private void startRecording()
{
try{
camera = Camera.open();
mediaRecorder = new MediaRecorder();
surfaceView = new CamSurfaceView(this, camera);

try {
camera.setPreviewDisplay(surfaceView.getHolder());
} catch (IOException e1) {

}
camera.startPreview();
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(MediaRecorder.OutputFormat.MPEG_4));
mediaRecorder.setOutputFile(getOutputMediaFile().toString());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

try{
mediaRecorder.prepare();
}
catch(IllegalStateException e){

}
catch(IOException e){

}
mediaRecorder.start(); //Code failure occurs here.
}
catch(Exception e)
{
if(mediaRecorder != null)
mediaRecorder.release();
if(camera != null)
camera.release();
}
}
private Uri getOutputMediaFile()
{
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()+File.separator+"DCIM/Camera");

File[] files =mediaStorageDir.listFiles();

if(!mediaStorageDir.exists())
{
if(!mediaStorageDir.mkdirs())
{
Log.d("KarmaCam","Failed to create directory");
return null;
}
}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(mediaStorageDir.getPath() + File.separator+ "VID_"+timeStamp+".mp4");
return Uri.fromFile(file);

}

我的 SurfaceView:

public class KarmaCamSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder mHolder;
private Camera mCamera;

public CamSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight, int height) {

if(mHolder.getSurface() == null)
return;
try
{
mCamera.stopPreview();
}
catch(Exception e) {}

try
{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
}
catch(Exception e){}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try
{
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
catch(Exception e){}

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub

}

我见过其他人有类似的问题/问题,它们通常发生在不正确的输出文件名格式周围。据我所知,我的没问题,但我很好奇目录路径是否有效。这是内置 Glass 视频的存储位置。

最佳答案

将您的位置更改为:

 Environment.getExternalStorageDirectory() + File.separator 
+ Environment.DIRECTORY_DCIM + File.separator + "FILE_NAME";

这对我有用。希望对您有所帮助!

关于android - 在无效状态 : 4 中调用 MediaRecorder 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com