首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >录像机问题

录像机问题
EN

Stack Overflow用户
提问于 2011-07-27 18:09:25
回答 1查看 1.8K关注 0票数 0

我正在做一个录制视频的应用程序。我得到了许多可以正常工作的示例,但问题是,当录制摄像头没有在纵向模式下打开时,它正在打开另一种模式。这是我使用的示例,包括xml文件:

代码语言:javascript
复制
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文件是:

代码语言:javascript
复制
    <?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中进行吗?

请帮帮我。提前感谢

EN

回答 1

Stack Overflow用户

发布于 2011-07-27 18:50:39

伙计,你需要一些修复,因为这个错误已经(我认为它是错误)了(我认为它是错误),你需要将这样的代码添加到surfaceChanged()

代码语言:javascript
复制
if(getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT)
            {
                camera.setDisplayOrientation(90);
                p.setRotation(90);
            }
            else
            {
                p.setRotation(0);
                camera.setDisplayOrientation(0);
            }

简单的代码...也许很愚蠢,但它已经解决了这个问题。附注:从2.2开始,我认为这是工作。if < than代码对您没有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6842647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档