安卓录制视频

发布于 2024-11-09 09:34:43 字数 4007 浏览 0 评论 0原文

我在让我的 adnroid 程序运行时遇到这个问题。我将此作为我的 record.java 代码

import java.io.IOException;



import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class record extends Activity{

    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        recorder = new MediaRecorder();
        initRecorder();
        setContentView(R.layout.view);

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

        cameraView.setClickable(true);
        cameraView.setOnClickListener((OnClickListener) this);
    }

    private void initRecorder() {
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);
        recorder.setOutputFile("/sdcard/videocapture_example.mpg");
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setMaxDuration(60000); // 60 seconds
        recorder.setMaxFileSize(100000000);// Approximately 10 megabytes
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);


    }


    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;

            // Let's initRecorder so we can record again
            initRecorder();
            prepareRecorder();
        } else {
            recording = true;
            recorder.start();
        }
    }

    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();

    }

我的 android 清单也看起来像这样

        <activity android:name=".record"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.apapa.ttt.RECORD" />

        </intent-filter>
        </activity>

</application>


<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.record_audio"/>

然而,当我运行这段代码时,它总是会出现错误“应用程序已意外停止。请重试”我真的很困惑我在这里做错了什么,因为我认为我的每一步都是正确的。任何帮助将不胜感激。

I have this problem with getting my adnroid program to work. I have this as my record.java code

import java.io.IOException;



import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class record extends Activity{

    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording = false;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        recorder = new MediaRecorder();
        initRecorder();
        setContentView(R.layout.view);

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

        cameraView.setClickable(true);
        cameraView.setOnClickListener((OnClickListener) this);
    }

    private void initRecorder() {
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);
        recorder.setOutputFile("/sdcard/videocapture_example.mpg");
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setMaxDuration(60000); // 60 seconds
        recorder.setMaxFileSize(100000000);// Approximately 10 megabytes
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);


    }


    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;

            // Let's initRecorder so we can record again
            initRecorder();
            prepareRecorder();
        } else {
            recording = true;
            recorder.start();
        }
    }

    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();

    }

My android manifest looks like this as well

        <activity android:name=".record"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="com.apapa.ttt.RECORD" />

        </intent-filter>
        </activity>

</application>


<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.record_audio"/>

How ever, when I run this code it always brings up the error "The application has stopped unexpectedly. Please Try again" I'm really confused what I am doing wrong here because I think I have every step right. Any help will be much appreciated.

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

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

发布评论

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

评论(1

醉酒的小男人 2024-11-16 09:34:43

android.permission.RECORD_VIDEO 它不存在。

检查文件是否创建?或者单独创建文件。

android.permission.RECORD_VIDEO its not there.

Check whether file is created? Or create file separately.

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