自定义摄像机无法启动 - android

时间:2014-12-01 08:29:00

标签: android video

我的自定义摄像机无法启动。按下按钮时,代码始终会捕获异常。 我想要做的是录制低质量的视频,以便我可以将其转换为base64并将其发送到服务器。 我尝试使用android的原生摄像机使用意图并设置intent.putExtra("EXTRA_VIDEO_QUALITY", 0);,但质量没有变化。

所以我尝试制作自定义视频。

这是代码:

package holosoft.wasfty;

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

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MyVideoActivity extends Activity implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Button startRecording = null;
// private Button stopRecording = null;
File video;
private Camera mCamera;
private Boolean recording = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_video);
    Log.i(null, "Video starting");
    startRecording = (Button) findViewById(R.id.buttonstart);
    mCamera = Camera.open();
    surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

    startRecording.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!recording)
            {
                try {
                    startRecording();
                    startRecording.setText("stop");
                    recording   = true;
                } catch (Exception e) {
                    String message = e.getMessage();
                    Log.i(null, "Problem Start" + message);
                    mrec.release();
                }
            }
            else
            {
                stopRecording();
                startRecording.setText("Record");
            }
        }
    });
}

protected void startRecording() throws IOException {
    mrec = new MediaRecorder(); // Works well
    mCamera.unlock();

    mrec.setCamera(mCamera);

    mrec.setPreviewDisplay(surfaceHolder.getSurface());
    mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mrec.setAudioSource(MediaRecorder.AudioSource.MIC);

    mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    mrec.setPreviewDisplay(surfaceHolder.getSurface());
    File mediasro=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),"MyCameraAPP");
    mrec.setOutputFile(mediasro.getPath()+File.separator+"VID_"+"HELLO"+".mp4");
    mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mrec.prepare();
    mrec.start();

    Intent returnIntent = new Intent();
    returnIntent.putExtra("data",mediasro.getAbsolutePath());
    setResult(RESULT_OK, returnIntent);        
    finish();
}

protected void stopRecording() {
    /*mrec.stop();
    mrec.release();
    mCamera.release();*/
    releaseMediaRecorder();
    releaseCamera();
}

private void releaseMediaRecorder() {
    if (mrec != null) {
        mrec.reset(); // clear recorder configuration
        mrec.release(); // release the recorder object
        mrec = null;
        mCamera.lock(); // lock camera for later use
    }
}

private void releaseCamera() {
    if (mCamera != null) {
        mCamera.release(); // release the camera for other applications
        mCamera = null;
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    if ( mCamera == null )
    {
        mCamera = Camera.open();
    }
    if ( mCamera != null )
    {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (mCamera != null) {
        Parameters params = mCamera.getParameters();
        mCamera.setParameters(params);
    } else {
        Toast.makeText(getApplicationContext(), "Camera not available!",
                Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();
    mCamera.release();
}

}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyVideoActivity" >

<SurfaceView
    android:id="@+id/surface_camera"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" />

<Button
    android:id="@+id/buttonstart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/CameraView"
    android:layout_alignTop="@+id/CameraView"
    android:text="Button" />

</RelativeLayout>

权限:

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

logcat的:

12-01 10:13:55.925: E/ActivityThread(9238): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
12-01 10:13:55.925: E/ActivityThread(9238):     at dalvik.system.VMRuntime.pauseGc(Native Method)
12-01 10:13:55.925: E/ActivityThread(9238):     ... 15 more
12-01 10:13:55.955: I/(9238): Video starting
12-01 10:13:58.305: E/MediaRecorder(9238): setOutputFormat called in an invalid state: 4
12-01 10:13:58.305: I/(9238): Problem Startnull
12-01 10:13:59.020: I/(9238): Problem Startunlock failed
12-01 10:14:04.425: I/(9238): Problem Startunlock failed
12-01 10:14:07.000: I/(9238): Problem Startunlock failed
12-01 10:14:07.875: I/(9238): Problem Startunlock failed
12-01 10:14:08.460: I/(9238): Problem Startunlock failed
12-01 10:14:08.820: I/(9238): Problem Startunlock failed
12-01 10:14:09.095: I/(9238): Problem Startunlock failed
12-01 10:14:09.685: I/(9238): Problem Startunlock failed
12-01 10:14:10.385: I/(9238): Problem Startunlock failed
12-01 10:14:10.775: I/(9238): Problem Startunlock failed
12-01 10:14:11.095: I/(9238): Problem Startunlock failed
12-01 10:14:11.370: I/(9238): Problem Startunlock failed
12-01 10:14:11.660: I/(9238): Problem Startunlock failed
12-01 10:14:11.880: I/(9238): Problem Startunlock failed
12-01 10:14:12.165: I/(9238): Problem Startunlock failed
12-01 10:14:12.345: I/(9238): Problem Startunlock failed
12-01 10:14:12.605: I/(9238): Problem Startunlock failed
12-01 10:14:12.800: I/(9238): Problem Startunlock failed
12-01 10:14:13.510: D/AbsListView(9238): Get MotionRecognitionManager

我会感激任何帮助。提前致谢

1 个答案:

答案 0 :(得分:0)

你已经写了两次

mrec.setPreviewDisplay(surfaceHolder.getSurface());