![](/img/trans.png)
[英]Android Force Close issue of Video Recorder implementation in Custom Camera app
[英]Issue with video recorder
我正在做一個錄制視頻的應用程序。我有許多可以正常工作的示例,但問題是錄制相機未以縱向模式打開時,它正在打開另一種模式,這是我使用過的示例,包括xml文件:
package com.example.RecordVideo;
import java.io.File;
import android.app.Activity;
import android.graphics.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
private MediaRecorder recorder = null;
private static final String OUTPUT_FILE = "/sdcard/videooutput.mp4";
private static final String TAG = "RecordVideo";
private VideoView videoView = null;
private Button startBtn = null;
Button endBtn;
Button playRecordingBtn;
Button stpPlayingRecordingBtn;
SurfaceHolder mholder;
File outFile ;
Camera camera;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordvideo);
startBtn = (Button) findViewById(R.id.bgnBtn);
startBtn.setOnClickListener(this);
endBtn = (Button) findViewById(R.id.stpBtn);
endBtn.setOnClickListener(this);
endBtn.setEnabled(false);
playRecordingBtn = (Button) findViewById(R.id.playRecordingBtn);
playRecordingBtn.setOnClickListener(this);
stpPlayingRecordingBtn =(Button) findViewById(R.id.stpPlayingRecordingBtn);
stpPlayingRecordingBtn.setOnClickListener(this);
videoView = (VideoView)this.findViewById(R.id.videoView);
playRecordingBtn.setEnabled(false);
stpPlayingRecordingBtn.setEnabled(false);
mholder = videoView.getHolder();
mholder.addCallback(this);
mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// @Override
public void surfaceCreated(SurfaceHolder holder) {
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startBtn.setEnabled(true);
}
// @Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
// @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.v(TAG, "Width x Height = " + width + "x" + height);
}
private void playRecording() {
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoPath(OUTPUT_FILE);
videoView.start();
}
private void stopPlayingRecording() {
videoView.stopPlayback();
}
private void stopRecording() throws Exception {
if (recorder != null) {
recorder.stop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (recorder != null) {
recorder.release();
}
}
private void beginRecording(SurfaceHolder holder) throws Exception {
if(recorder!=null)
{
recorder.stop();
recorder.release();
}
outFile = new File(OUTPUT_FILE);
if(outFile.exists())
{
outFile.delete();
}
try {
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(480, 580);
recorder.setVideoFrameRate(15);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setMaxDuration(60000*10); // limit to 10 minutes
recorder.setPreviewDisplay(mholder.getSurface());
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
catch(Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
public Surface getSurface()
{
return mholder.getSurface();
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(startBtn))
{
try {
endBtn.setEnabled(true);
startBtn.setEnabled(false);
playRecordingBtn.setEnabled(false);
stpPlayingRecordingBtn.setEnabled(false);
beginRecording(mholder);
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
if(v.equals(endBtn))
{
try {
startBtn.setEnabled(false);
endBtn.setEnabled(false);
playRecordingBtn.setEnabled(true);
stpPlayingRecordingBtn.setEnabled(false);
stopRecording();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
if(v.equals(playRecordingBtn))
{
try {
startBtn.setEnabled(false);
endBtn.setEnabled(false);
playRecordingBtn.setEnabled(false);
stpPlayingRecordingBtn.setEnabled(true);
playRecording();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
if(v.equals(stpPlayingRecordingBtn))
{
try {
startBtn.setEnabled(false);
endBtn.setEnabled(false);
playRecordingBtn.setEnabled(true);
stpPlayingRecordingBtn.setEnabled(false);
stopPlayingRecording();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
}
}
XML文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<VideoView
android:id="@+id/videoView"
android:layout_width="480px"
android:layout_height="580px"
android:keepScreenOn="true"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="80px"
>
<Button
android:id="@+id/bgnBtn"
android:layout_width="150px"
android:layout_marginLeft="80px"
android:layout_height="wrap_content"
android:text="Start"
/>
<Button
android:id="@+id/stpBtn"
android:layout_width="150px"
android:layout_marginLeft="40px"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="80px"
>
<Button
android:id="@+id/playRecordingBtn"
android:layout_width="150px"
android:layout_marginLeft="80px"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="@+id/stpPlayingRecordingBtn"
android:layout_width="150px"
android:layout_marginLeft="40px"
android:layout_height="wrap_content"
android:text="Stop"
/>
</LinearLayout>
</LinearLayout>
我也添加權限,我想要的是1.應該在肖像模式下打開視頻錄制2.在Android中可以進行視頻裁剪
請幫我。 提前致謝
伙計,您需要一些修復,因為此錯誤已經(並且將一直存在)(我認為是錯誤),您需要向surfaceChanged()添加類似的代碼
if(getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT)
{
camera.setDisplayOrientation(90);
p.setRotation(90);
}
else
{
p.setRotation(0);
camera.setDisplayOrientation(0);
}
簡單的代碼...可能會感到震驚,但這已解決了這個問題。 PS從2.2我認為這是工作。 如果<比代碼無濟於事
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.