2013-10-04 43 views
6

在android中如何在媒體錄像機啓動之前顯示預覽面。 我的應用程序具有視頻錄製功能,當我導航到視頻錄製片段時它顯示黑屏,當我開始錄製時使用開始按鈕相機預覽顯示和錄製開始。在Android媒體錄像機中預覽顯示

如何在錄製前開始預覽。 我已經加入的片段 -

surfaceHolder = mySurfaceView.getHolder(); 
camera = Camera.open(); 
if(camera!=null){ 
    camera.setDisplayOrientation(90); 
    Camera.Parameters param; 
    param = camera.getParameters(); 
    param.setPreviewFrameRate(20); 
    param.setPreviewSize(176, 144); 
    camera.setParameters(param); 
    camera.setPreviewDisplay(surfaceHolder); 
} 

mediaRecorder = new MediaRecorder(); 
camera.unlock(); 
mediaRecorder.setCamera(camera); 
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
CamcorderProfile camcorderProfile_HQ = CamcorderProfile 
      .get(CamcorderProfile.QUALITY_HIGH); 
mediaRecorder.setProfile(camcorderProfile_HQ); 
String filePath = getOutputMediaFile(MEDIA_TYPE_VIDEO).getPath(); 
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 
mediaRecorder.setOutputFile(filePath); 

)代碼i的onCreateView使用(以及我在啓動按鈕使用的碼點進

mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface()); 
mediaRecorder.prepare(); 
mediaRecorder.start(); 

所以我能夠通過如上所述使用重新編碼視頻代碼,但無法在錄製開始前顯示預覽。 請幫助我在哪裏我missing.for黑屏幕顯示之前重新編碼的視頻。

在此先感謝。

+0

我有同樣的問題 - 你怎麼顯示相機預覽沒有實際記錄?!在用戶有機會正確對焦攝像機之前開始錄製是一種糟糕的用戶體驗。這令人印象深刻,沒有人回答這個問題。 – Cat

+0

@貓你有沒有得到你的答案? – Ravi

回答

4
public void surfaceCreated(SurfaceHolder holder) { 
     if (mCamera != null) { 
      Parameters params = mCamera.getParameters(); 
      mCamera.setParameters(params); 
      try { 
       //mCamera.setDisplayOrientation(90); 
       mCamera.setPreviewDisplay(holder); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      mCamera.startPreview(); 
     } 
    } 

如果您實現SurfaceHolder.Callback,請像這樣重寫surfaceCreated方法。這對我有效。

2

我遇到了同樣的問題。我查了一下,這是我的活動。我花了一點努力才使它不會崩潰,所以這是最終結果。它會在用戶單擊REC按鈕之前顯示預覽。 (我也顯示倒計時,但不介意)。還要注意,在這個例子中,在主線程上做了太多的工作(點擊時)。這裏有一些不是最佳實踐的東西,但對於一個工作示例,我認爲它夠好。

import android.app.Activity; 
import android.hardware.Camera; 
import android.media.CamcorderProfile; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.os.Environment; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

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

public class MainActivity extends Activity implements SurfaceHolder.Callback { 

    private static final String LOG_TAG = MainActivity.class.getCanonicalName(); 
    Button myButton; 
    MediaRecorder mediaRecorder; 
    SurfaceHolder surfaceHolder; 
    boolean recording; 
    private TextView mTimer; 
    private Camera mCamera; 

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

     setContentView(R.layout.activity_main); 

     recording = false; 
     mediaRecorder = new MediaRecorder(); 
     mCamera = Camera.open(); 
     initMediaRecorder(); 

     SurfaceView myVideoView = (SurfaceView) findViewById(R.id.videoview); 
     surfaceHolder = myVideoView.getHolder(); 
     surfaceHolder.addCallback(this); 

     myButton = (Button) findViewById(R.id.mybutton); 
     myButton.setOnClickListener(myButtonOnClickListener); 

     mTimer = (TextView) findViewById(R.id.timer); 
    } 

    private Button.OnClickListener myButtonOnClickListener 
      = new Button.OnClickListener() { 

     @Override 
     public void onClick(final View arg0) { 
      CountDownTimer countDownTimer = new CountDownTimer(90 * 1000, 1000) { 
       @Override 
       public void onTick(long millisUntilFinished) { 
        mTimer.setText(String.valueOf(millisUntilFinished/1000)); 
       } 

       @Override 
       public void onFinish() { 
        onClick(arg0); 
       } 
      }; 

      if (recording) { 
       // Stop recording and finish 
       try { 
        mediaRecorder.stop(); 
        mediaRecorder.reset(); 
        mediaRecorder.release(); 
        finish(); 
       } catch (Exception e) { 
        Log.e(LOG_TAG, "Failed to stop recorder", e); 
       } 

      } else { 
       // Release the camera and start recording 
       mCamera.release(); 
       countDownTimer.start(); 
       mediaRecorder.start(); 
       recording = true; 
       myButton.setText("STOP"); 
      } 
     } 
    }; 


    @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 void initMediaRecorder() { 
     mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
     mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
     CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_CIF); 
     mediaRecorder.setProfile(camcorderProfile_HQ); 
     File file = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "myvideo.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 
    } 

    private void prepareMediaRecorder() { 
     mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface()); 

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