通过 Android 2.2 版本从相机录制视频

发布于 2024-11-11 16:31:41 字数 4873 浏览 5 评论 0原文

在此处输入代码当我尝试从 Android 2.2 版本的相机录制视频时。它有一些错误。没有人能找到解决方案。 Android MediaRecorder API 是否有任何错误。我该如何解决这个问题。 我遇到了更多错误。您可以在图片中看到其中一些错误。 还有这样的错误:Camera Preview -13 多谢。

https://i.sstatic.net/72lp7.png recorder.prepare() 失败并抛出 Java.lang.illegalexeption 这是代码:

package app.raceway.com;

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

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class VideoCapture extends Activity implements  SurfaceHolder.Callback {
    MediaRecorder recorder;
    SurfaceHolder holder;
    public Camera camera;
    File video;
    String filePath;
    boolean recording = false;
      private static final int FRAME_RATE = 15;

      private static final int CIF_WIDTH = 320;

      private static final int CIF_HEIGHT = 240;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    recorder = new MediaRecorder();

    setContentView(R.layout.main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.cameraView);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    String path=Environment.getExternalStorageDirectory().getAbsolutePath()+
    "/video/videocapture_example.mpg4";
    // make sure the directory we plan to store the recording in exists
    File sampleDir = Environment.getExternalStorageDirectory();
    try { 
        video = new File(sampleDir+"/videofile.3gp");
        sampleDir.createNewFile();
          //video = File.createTempFile("videofile", ".3gp", sampleDir);
      } 
      catch (IOException e)
      {
          Log.e("deneme","sdcard access error");

      }
      filePath=video.getAbsolutePath();

}

private void initRecorder() {

    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

}
private void prepareRecorder() throws IOException{
    recorder.setCamera(camera);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
    recorder.setOutputFile(filePath);
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
try {

} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


}



public void onclickSaveVideo(View v) throws IOException {
    if (recording) {
    Toast t=new Toast(getApplicationContext());
    t.makeText(getApplicationContext(), "Video Recording stopped",Toast.LENGTH_SHORT);
    t.show();
        recorder.stop();
        recording = false;

        // Let's initRecorder so we can record again
        initRecorder();     
    } else {
        try {
            prepareRecorder();
            //recorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recording = true;
        recorder.prepare();
        recorder.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recorder.stop();
        Toast t=new Toast(getApplicationContext());
        t.makeText(getApplicationContext(), "Video Recording started",Toast.LENGTH_SHORT);
        t.show();
    }
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera=Camera.open();

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    camera.startPreview();
    camera.unlock();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();
    camera.release();
}


}

enter code hereWhen I try to recording video from camera at version Android 2.2. It has some errors.No one could find the solution. İs there any bug Android MediaRecorder API. How can I solve this.
I got more errors.You can see some of them in picture.
And an error like that:Camera Preview -13
Thanks a lot.

https://i.sstatic.net/72lp7.png
recorder.prepare() fails and throws Java.lang.illegalexeption
Here is Code:

package app.raceway.com;

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

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class VideoCapture extends Activity implements  SurfaceHolder.Callback {
    MediaRecorder recorder;
    SurfaceHolder holder;
    public Camera camera;
    File video;
    String filePath;
    boolean recording = false;
      private static final int FRAME_RATE = 15;

      private static final int CIF_WIDTH = 320;

      private static final int CIF_HEIGHT = 240;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    recorder = new MediaRecorder();

    setContentView(R.layout.main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.cameraView);
    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    String path=Environment.getExternalStorageDirectory().getAbsolutePath()+
    "/video/videocapture_example.mpg4";
    // make sure the directory we plan to store the recording in exists
    File sampleDir = Environment.getExternalStorageDirectory();
    try { 
        video = new File(sampleDir+"/videofile.3gp");
        sampleDir.createNewFile();
          //video = File.createTempFile("videofile", ".3gp", sampleDir);
      } 
      catch (IOException e)
      {
          Log.e("deneme","sdcard access error");

      }
      filePath=video.getAbsolutePath();

}

private void initRecorder() {

    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

}
private void prepareRecorder() throws IOException{
    recorder.setCamera(camera);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
    recorder.setOutputFile(filePath);
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
try {

} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


}



public void onclickSaveVideo(View v) throws IOException {
    if (recording) {
    Toast t=new Toast(getApplicationContext());
    t.makeText(getApplicationContext(), "Video Recording stopped",Toast.LENGTH_SHORT);
    t.show();
        recorder.stop();
        recording = false;

        // Let's initRecorder so we can record again
        initRecorder();     
    } else {
        try {
            prepareRecorder();
            //recorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recording = true;
        recorder.prepare();
        recorder.start();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        recorder.stop();
        Toast t=new Toast(getApplicationContext());
        t.makeText(getApplicationContext(), "Video Recording started",Toast.LENGTH_SHORT);
        t.show();
    }
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera=Camera.open();

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    camera.startPreview();
    camera.unlock();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (recording) {
        recorder.stop();
        recording = false;
    }
    recorder.release();
    finish();
    camera.release();
}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-11-18 16:31:41

Emin,

根据您提供的 logcat 输出图像,崩溃发生在 start() 方法。您将从 start 方法的文档中看到 prepare() 必须先调用,否则会抛出IllegalStateException。在您的代码中,所有 prepare() 调用都被注释掉了吗?

编辑:我们在我的答案下面的评论中整理了内容。他在模拟器上运行此代码,但模拟器不支持 MediaRecorder

Emin,

Based on the image of the logcat output you provide the crash is occuring with the start() method. You will see from the documentation for the start method that prepare() must be called first or else it will throw the IllegalStateException. In your code all the calls the prepare() are commented out?

EDIT: We sorted things out in the comments below my answer. He was running this code on an emulator and MediaRecorder is not supported by the emulator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文