从前置摄像头 Android 录制

Posted

技术标签:

【中文标题】从前置摄像头 Android 录制【英文标题】:Record from front camera Android 【发布时间】:2018-02-14 15:27:51 【问题描述】:

我使用此代码从前置摄像头录制,但是当摄像头开始录制时,Recorder 更改为后置摄像头(表面视图显示前置摄像头预览),这是我的代码:

public class MatchActivity extends Activity implements SurfaceHolder.Callback 

    private static final String LOG_TAG = MainActivity.class.getCanonicalName();
    View myButton;
    MediaRecorder mediaRecorder;
    SurfaceHolder surfaceHolder;
    boolean recording;
    private TextView mTimer;
    private Camera mCamera;
    TextView prepareid;
    CountDownTimer countDownTimer;
    String pathOfVideo;
    TextView textmode;
    ImageView imagemode;
    boolean isRunning = false;
    String PIC;
    int ID;

    operations op = new operations();

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_match);
        SurfaceView myVideoView = (SurfaceView) findViewById(R.id.surface);
        op.Localize();
        recording = false;
        mCamera = Camera.open(1);
        mCamera.setDisplayOrientation(90);
        mCamera.unlock();
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setCamera(mCamera);

        pathOfVideo=initMediaRecorder();
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        myButton = (View) findViewById(R.id.Prepare);
        myButton.setOnClickListener(StartVideo);

        ImageView CorecctAnswer = (ImageView) findViewById(R.id.correctanswer);
        CorecctAnswer.setOnClickListener(CorrectAnswer);


        prepareid = (TextView) findViewById(R.id.prepareid);
        mTimer = (TextView) findViewById(R.id.timer);
        if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) 
            ActivityCompat.requestPermissions(MatchActivity.this, new String[]android.Manifest.permission.CAMERA, 50);
        

    
    private Button.OnClickListener StartVideo
            = new Button.OnClickListener() 
        @Override
        public void onClick(final View arg0) 
            myButton.setVisibility(View.GONE);
            prepareid.setVisibility(View.GONE);

            countDownTimer = new CountDownTimer(60 * 1000, 1000) 

                @Override
                public void onTick(long millisUntilFinished) 
                    mTimer.setText(String.valueOf(millisUntilFinished / 1000)+ " ثانیه ");
                    isRunning = true;
                
                @Override
                public void onFinish() 
                    isRunning= false;
                    if(camerastate)
                        stopvideo();
                    Intent intent = new Intent(MatchActivity.this, ResultPage.class);
                    Bundle b = new Bundle();
                    b.putString("PIC",PIC);
                    b.putString("PATH", pathOfVideo);
                    b.putString("Word", textmode.getText().toString());
                    b.putInt("Answer",0);
                    b.putInt("ID",ID);
                    intent.putExtras(b);
                    startActivity(intent);
                    finish();
                
            ;
            startvideo();
        
    ;
    public void stopvideo()
    
        recording=false;
        try 

            mediaRecorder.stop();
            mediaRecorder.reset();

            finish();
         catch (Exception e) 
            Log.e(LOG_TAG, "Failed to stop recorder", e);
        
        mediaRecorder.release();
        mediaRecorder = null;
        if (mCamera != null) 
            mCamera.release();
            mCamera = null;
        
    
    public void startvideo()
    
        mCamera.release();
        countDownTimer.start();
        mediaRecorder.start();
        recording = true;
    

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 

    

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
        if (mCamera != null) 
            Camera.Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);
            try 
                mCamera.setPreviewDisplay(holder);
             catch (IOException e) 
                e.printStackTrace();
            
            mCamera.startPreview();
        
        prepareMediaRecorder();
    

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) 

    
    private String initMediaRecorder() 

        mediaRecorder.setCamera(mCamera);
        mediaRecorder.setAudiosource(MediaRecorder.AudioSource.DEFAULT);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);  //get your own profile
        Camera.Parameters parameters = null;
        if (mCamera.getParameters()!=null) 
            parameters = mCamera.getParameters();
        
        parameters.setPreviewSize(camcorderProfile.videoFrameWidth,camcorderProfile.videoFrameHeight);
        mCamera.setParameters(parameters);
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES),"hadskalme" + System.currentTimeMillis()+ ".mp4");
        if (file.exists()) 
            file.delete();
        
        mediaRecorder.setOutputFile(file.getAbsolutePath());

        mediaRecorder.setMaxDuration(90000); // Set max duration 90 sec.
        mediaRecorder.setMaxFileSize(15000000); // Set max file size 15M

        return String.valueOf(file.getAbsolutePath());
    

    private void prepareMediaRecorder() 

        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

        try 
            mediaRecorder.prepare();
         catch (IllegalStateException | IOException e) 
            Log.e(LOG_TAG, "  to prepare recorder", e);
        
    

这就是我收到的错误:

W/AudioSystem:AudioFlinger 服务器死了! W/IMediaDeathNotifier:媒体服务器死机 E/MediaPlayer: 错误 (100, 0) W/MediaMetadataRetriever:MediaMetadataRetriever 服务器死机! W/CameraBase:相机服务死了!

【问题讨论】:

【参考方案1】:
public static Camera openFrontalCamera() 
   Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
   for (int i = 0; i < Camera.getNumberOfCameras(); i++) 
       Camera.getCameraInfo(i, cameraInfo);
       if (Camera.CameraInfo.CAMERA_FACING_FRONT == cameraInfo.facing) 
           return Camera.open(i);
       
   
   return null;

代码中的用法

mCamera = openFrontalCamera();
if (mCamera != null) 
    //continue flow
 else 
    //no frontal camera available

【讨论】:

感谢您的回答,在这种情况下相机挂起并且不记录任何内容(表面视图太挂起)(相机服务已失效)

以上是关于从前置摄像头 Android 录制的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 中,是不是可以更改相机(从前到后)并仍然继续录制?

在 Android 中,是不是可以更改相机(从前到后)并仍然继续录制?

从前置摄像头拍摄图像后如何消除镜面效果? [安卓]

iOS 摄像头应用,从前置摄像头切换到后置摄像头时闪烁

Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19

切换相机后音频/视频不同步