我正在做一个录制视频的应用程序。我有很多样本正在工作但问题是录制相机没有在纵向模式下打开,它正在打开另一种模式 这是我使用的样本,包括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.视频录制应在PORTRAIT模式下打开 2.在Android中可以进行视频裁剪
请帮帮我。提前致谢
答案 0 :(得分:1)
答案 1 :(得分:0)
伙计,你需要一些修复,因为这个bug已经(并且将会一直)(我认为这是bug)你需要将这样的代码添加到surfaceChanged()
if(getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT)
{
camera.setDisplayOrientation(90);
p.setRotation(90);
}
else
{
p.setRotation(0);
camera.setDisplayOrientation(0);
}
简单的代码...也许是愚蠢的,但它解决了这个问题。 附:从2.2我觉得它的工作。如果&lt;比代码没有帮助你