gpt4 book ai didi

android - 准备失败 : -1 media recorder in android

转载 作者:行者123 更新时间:2023-11-30 04:24:05 25 4
gpt4 key购买 nike

当我尝试从相机录制视频时似乎出现错误准备失败:-1。

我搜索了很多,但我无法从任何地方得到任何可接受的答案。下面是我的代码。

我不知道参数?并通过解锁相机。谁能做到,将不胜感激。

package com.camara.activity;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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.CAMERA);
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(10);
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();



}

}

下面是logcat的输出

01-10 17:33:00.450: I/MediaRecorderJNI(14046): prepare: surface=0x243398 (id=1)
01-10 17:33:00.500: E/MediaRecorder(14046): prepare failed: -1
01-10 17:33:00.500: E/IOException(14046): java.io.IOException: prepare failed.

提前致谢。

最佳答案

好吧,我真的发现了我的错误。因为当然有错误。实际上我想同时进行预览和录制,并认为我必须为此使用对象 Camera。但实际上 MediaRecorder 自己使用 SetDisplayPreview 和 .... MediaRecord.Prepare :)

我心情很好,所以这就是所有类(class);)

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;

public class ModuleVideo implements SurfaceHolder.Callback, OnInfoListener
{
public static final int STATE_NONE = -1;
public static final int STATE_STOPPED = 0;
public static final int STATE_PREVIEW = 1;
public static final int STATE_CAPTURE = 2;
public static final int STATE_RECORDING = 3;

private SurfaceHolder mHolder;
private MediaRecorder mRecorder;
private SurfaceView mCameraView;
private Context mContext;
private FrameLayout mParent;
private int mState;
private boolean mRecording;


public ModuleVideo(Context context, FrameLayout parent)
{
//Initiate the Surface Holder properly

mParent = parent;
mContext = context;
mRecorder = null;
mState = STATE_NONE;
mRecording = false;
}


private void Init()
{
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mRecorder.setProfile(camcorderProfile_HQ);
mRecorder.setOutputFile("/sdcard/MY_VIDEO.mp4");
}

private void Prepare()
{
try
{
mRecorder.setPreviewDisplay(mHolder.getSurface());
mRecorder.prepare();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}


public void UI_StartPreview()
{
if(mState == STATE_STOPPED || mState == STATE_NONE)
{
mRecorder= new MediaRecorder();

Init();

mCameraView= new SurfaceView(mContext);
mParent.addView(mCameraView);

this.mHolder = mCameraView.getHolder();
this.mHolder.addCallback(this);
this.mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}


public void UI_StopPreview()
{
if(mState == STATE_PREVIEW)
{
if(mRecording)
{
UI_StopRecord();
}

if(mRecorder != null)
{
mRecorder.release();
mRecorder= null;
}

mParent.removeView(mCameraView);
//mCameraView= null;
}
}

public boolean UI_StartRecord()
{
if(mState != STATE_PREVIEW )
{
return false;
}

String path= "/sdcard/PLUS_VIDEO.mp4";

String state = android.os.Environment.getExternalStorageState();

if(!state.equals(Environment.MEDIA_MOUNTED))
{
return false;
}
File directory = new File(path).getParentFile();
if(!directory.exists() && !directory.mkdirs())
{
return false;
}

mRecorder.start();
mRecording= true;

mState= STATE_RECORDING;

return true;
}


public void UI_StopRecord()
{
if(mRecorder != null)
{
mRecorder.stop();
mRecorder.reset();

Init();
Prepare();

mRecording= false;

mState= STATE_PREVIEW;
}
}

public boolean UI_IsRecording()
{
return mRecording;
}



@Override
public void onInfo(MediaRecorder mr, int what, int extra)
{
// TODO Auto-generated method stub
Log.i(null, "onInfo");
}


@Override
public void surfaceCreated(SurfaceHolder holder)
{
Prepare();
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
mState= STATE_PREVIEW;
}


@Override
public void surfaceDestroyed(SurfaceHolder holder)
{

mState= STATE_STOPPED;
}
}

关于android - 准备失败 : -1 media recorder in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803100/

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