嗨我正在開發類似於此鏈接的Android視頻應用程序 http://android-er.blogspot.in/2011/10/video-capture-using-mediarecorder-with.html 其中視頻應用程序以幀佈局顯示。表面視圖顯示黑屏用於視頻預覽
我正在嘗試上面的一個表面視圖和我的xml看起來像下面。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp" >
<SurfaceView
android:id="@+id/CameraView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" />
<ImageView
android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:src="@drawable/record_video" />
</RelativeLayout>
我不知道爲什麼它顯示黑屏而不是相機視圖。我的ViewActivity.java如下。
public class ViewActivity extends Activity implements OnClickListener, SurfaceHolder.Callback
{
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording=false;
private MediaRecorder mediaRecorder;
private File file;
private ImageView recVideo;
private SurfaceView cameraView;
private Camera camera;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.view);
recVideo = (ImageView)findViewById(R.id.mybutton);
cameraView = (SurfaceView) findViewById(R.id.CameraView);
holder = cameraView.getHolder();
holder.addCallback(ViewActivity.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
recVideo.setClickable(true);
recVideo.setOnClickListener(this);
}
private void initRecorder()
{
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
recorder.setOutputFile(this.initFile().getAbsolutePath());
recorder.setMaxDuration(60000); // Set max duration 60 sec.
recorder.setMaxFileSize(5000000); // Set max file size 5M
}
private void prepareRecorder()
{
recorder.setPreviewDisplay(holder.getSurface());
try
{
recorder.prepare();
} catch (IllegalStateException e)
{
e.printStackTrace();
finish();
}
catch (IOException e)
{
e.printStackTrace();
finish();
}
}
public void onClick(View v)
{
if (recording)
{
recorder.stop();
recording = false;
initRecorder();
prepareRecorder();
Toast.makeText(ViewActivity.this,"Video Saved!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(ViewActivity.this,ViewActivity.class));
finish();
}
else
{
recorder.start();
recording = true;
recVideo.setImageDrawable(getResources().getDrawable(R.drawable.stop_recording));
}
}
public void surfaceCreated(SurfaceHolder holder)
{
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
finish();
}
我不知道我哪裏出錯了。如何使表面視圖顯示相機視圖而不是黑屏。
請幫忙。
謝謝!
我不知道到底,但你可以嘗試recorder.setPreviewDisplay(holder.getSurface());在此之後recorder.prepare(); –
試過!沒有變化。仍然顯示黑屏 – sanjana