在android上启动活动时,我看不到相机的预览

时间:2013-12-02 11:55:08

标签: android camera video-capture preview

更新: 我可以解决旋转问题(我稍后会通过答案编写修改后的代码)。但是在开始活动时我看不到相机的预览。 我在下面看到链接,但没有帮助我。 Can't create Camera preview in onCreate? [surfaceview2中的Android相机预览]

如果您知道任何回复,请修改我的代码。

// -------------------------------------------

我有一个Android应用程序,下面有捕获视频代码。除了2件事以外,每件事都能正常工 首先是:在拍摄之前我无法预览相机。我想在启动VideoRecorderActivity1时通过我的相机进行预览。 第二:当我按下ToggleButton的开头时,那么每件东西都会旋转90度,这是非常糟糕的。但输出文件具有正确的显示度。您可以使用下面的代码并查看结果。 请帮助解决我的应用问题。谢谢。 这是我的Classess。

import java.io.File;
import java.io.IOException;
import android.hardware.Camera.Parameters;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class VideoRecorderActivity1 extends Activity 
implements SurfaceHolder.Callback 
{

private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec= new MediaRecorder();

private Camera mCamera;
private ToggleButton mToggleButton=null;
private String Videname="";
private String Videopath="";
private VideoRecorder myVideo=null;
private static Boolean isRecording=false;
String  filePath= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator
+"My Audios"+File.separator+"video2camera5.3gpp";
private Boolean ExistVideo(String path1)
{
 File   file = new File(SDcard.Dir_recordedVideos );
   File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {           
        if(list[i].getPath().equals(path1))
            return true;
    }
    return false;
  }

  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recorder_video);
    InitializeUI();
    mToggleButton.setOnClickListener(new OnClickListener() 
    {
        @Override

        public void onClick(View v) 
        {
              if (((ToggleButton) v).isChecked())

                  try 
                  {    myVideo=new VideoRecorder();
                    do{
                        myVideo.setName();
                        Videname=myVideo.getName();
                        myVideo.setPath();
                        Videopath=myVideo.getPath();
                      }while(ExistVideo(Videopath));

                     startRecording();
                    } 
                  catch (Exception e) 
                    {
                        String message = e.getMessage();
                        Log.i(null, "Problem Start"+message);
                        if(mrec!= null)
                        mrec.release();
                    }

                else 
                    stopRecording();
        }
    });
}

 protected void startRecording() throws IOException 
 {
    String state = android.os.Environment.getExternalStorageState();
    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) 
    {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(this.Videopath).getParentFile();

    if (!directory.exists() && !directory.mkdirs()) 
    {
        throw new IOException("Path to file could not be created.");
    }

   mrec = new MediaRecorder();  // Works well
    mCamera.unlock();

    mrec.setCamera(mCamera);
    mrec.setPreviewDisplay(surfaceHolder.getSurface());   
    mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
    mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mrec.setOutputFile(Videopath);

    mrec.prepare();
    isRecording=true;
    mrec.start();
}
protected void stopRecording() 
{
    if(mrec !=null)
        mrec.stop();
    releaseMediaRecorder();
    isRecording=false;
}
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)
{
    // TODO Auto-generated method stub  
}
//------------------------------------------------------------

@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();
      VideoRecorderActivity1.this.finish();
    }
}
//------------------------------------------------------------

@Override
public void surfaceDestroyed(SurfaceHolder holder) 
{

}
//------------------------------------------------------------
 public boolean onKeyDown(int keyCode, KeyEvent event) 
 {
      if (keyCode == KeyEvent.KEYCODE_BACK ) 
      {              
          if(isRecording)
              mCamera.stopPreview();
           mCamera.release();

        VideoRecorderActivity1.this.finish();
      }

      return super.onKeyDown(keyCode, event);
  }
//------------------------------------------------------------
 private void InitializeUI()
 {
       mCamera = Camera.open();
        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mrec.setPreviewDisplay(surfaceHolder.getSurface()); 
        mToggleButton = (ToggleButton) findViewById(R.id.toggleRecordingButton);
 }

}

导入此类:

public class VideoRecorder 
{
private String name="";
private static int id=0;
private String path=null;

public VideoRecorder()
{

}
public String getName() 
{
    return(this.name);
}   
public void setName() 
{
    id++;
    this.name="video "+id;
} 
    public String getPath()
    {
        return this.path;
    }

    public void setPath() 
    {
        if (!this.name.contains(".")) 
        {
            this.name += ".3gpp";
        }
       this.path=SDcard.Dir_recordedVideos + this.name;

    }

}

1 个答案:

答案 0 :(得分:1)

你不是在创建预览我认为,在你的表面创建代码中放了这个:

    public void surfaceCreated(SurfaceHolder holder) {

            Log.v(TAG,"in surfaceCreated");                                 //Surface created for video preview and playback
            try{
                    mCamera .setPreviewDisplay(holder);
                    mCamera .startPreview();
            }catch(IOException e)
            {
                    Log.v(TAG,"could not start the preview ");
                    e.printStackTrace();

            }

    }

在您的开始记录功能中,请执行以下操作:

protected void startRecording() throws IOException 
{
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) 
{
    throw new IOException("SD Card is not mounted.  It is " + state + ".");
}

// make sure the directory we plan to store the recording in exists
File directory = new File(this.Videopath).getParentFile();

if (!directory.exists() && !directory.mkdirs()) 
{
    throw new IOException("Path to file could not be created.");
}

  mcamera.stopPreview();
  mcamera.unlock();
  mrec = new MediaRecorder();  // Works well
  mrec.setCamera(mCamera);   
 mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
 mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
 mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
 mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
 mrec.setOutputFile(Videopath);
 mrec.setPreviewDisplay(surfaceHolder.getSurface());   
 mrec.prepare();
 isRecording=true;
 mrec.start();
}