简体   繁体   English

Android 自定义录像

[英]Android Custom video recording

I'm having some errors with my custom recording application using android.我在使用 android 的自定义录制应用程序时遇到了一些错误。 I saw most of this code from a book called PRO Android Media.我从一本名为 PRO Android Media 的书中看到了大部分代码。 I developed the app using sdk "8" version.我使用 sdk “8” 版本开发了该应用程序。 However, I am testing the phone on my htc evo 4g which just got updated to gingerbread.但是,我正在我的 htc evo 4g 上测试手机,它刚刚更新为姜饼。 So I dont know how much that would have changed.所以我不知道那会改变多少。 Anyways my app still wasnt working when i tested it on 2.2 version either.无论如何,当我在 2.2 版本上测试它时,我的应用程序仍然无法正常工作。

Here is the code below这是下面的代码

package com.apapa.vrsixty;

import java.io.File;
import java.io.IOException;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;

import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
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.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class record extends Activity implements OnClickListener, SurfaceHolder.Callback{

    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording=false;
    public static final String TAG = "VIDEOCAPTURE";
    /** Called when the activity is first created. */
    @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_PORTRAIT);

        recorder = new MediaRecorder();// Instantiate our media recording object
        initRecorder();
        setContentView(R.layout.view);

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

        cameraView.setClickable(true);// make the surface view clickable
        cameraView.setOnClickListener((OnClickListener) this);// onClicklistener to be called when the surface view is clicked
    }

    private void initRecorder() {// this takes care of all the mediarecorder settings
        File OutputFile = new File(Environment.getExternalStorageDirectory().getPath());
        String video= "/DCIM/100MEDIA/Video";
        CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
         recorder.setProfile(cpHigh);        

        //recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        // default microphone to be used for audio
       // recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// default camera to be used for video capture.
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// generally used also includes h264 and best for flash
       // recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //well known video codec used by many including for flash
        //recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// typically amr_nb is the only codec for mobile phones so...

        //recorder.setVideoFrameRate(15);// typically 12-15 best for normal use. For 1080p usually 30fms is used.
       // recorder.setVideoSize(720,480);// best size for resolution.
        //recorder.setMaxFileSize(10000000);
        recorder.setOutputFile(OutputFile.getAbsolutePath()+video+".3gp");
        //recorder.setVideoEncodingBitRate(256000);//
        //recorder.setAudioEncodingBitRate(8000);
       recorder.setMaxDuration(600000);


    }

    /*if(record.setMaxDuration>60000){

        recorder.stop();
        MediaRecorder.OnInfoListener;
        Toast display = Toast.makeText(this, "You have exceeded the record time", Toast.LENGTH_SHORT);// toast shows a display of little sorts
        display.show();
        return true;
    }*/

    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();
            Toast display = Toast.makeText(this, "Stopped Recording", Toast.LENGTH_SHORT);// toast shows a display of little sorts
            display.show();


        } else {

            recorder.start();
            Log.v(TAG,"Recording Started"); 
            recording = true;

        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        initRecorder();
        Log.v(TAG,"surfaceCreated");
        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();

    }

Its bringing up these errors.它提出了这些错误。

06-09 13:37:52.833: ERROR/AndroidRuntime(27979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apapa.vrsixty/com.apapa.vrsixty.record}: java.lang.IllegalStateException
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.os.Looper.loop(Looper.java:143)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.ActivityThread.main(ActivityThread.java:4263)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at java.lang.reflect.Method.invoke(Method.java:507)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at dalvik.system.NativeStart.main(Native Method)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979): Caused by: java.lang.IllegalStateException
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.media.MediaRecorder.setOutputFormat(Native Method)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.media.MediaRecorder.setProfile(MediaRecorder.java:293)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at com.apapa.vrsixty.record.initRecorder(record.java:67)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at com.apapa.vrsixty.record.onCreate(record.java:50)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-09 13:37:52.833: ERROR/AndroidRuntime(27979):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)

Please any help will suffice please.请任何帮助就足够了。 I just cant wait to see an actually recording in stead of "The application has stopped unexpectedly."我迫不及待地想看到一个实际的录音,而不是“应用程序已意外停止”。

Update:更新:

Well thanks, recorder.reset();好吧,谢谢, recorder.reset(); worked for me but I am having an issue when I start the recorder.为我工作,但我在启动录音机时遇到了问题。 I have to click on my phone before it actually shows the preview.我必须先点击我的手机才能真正显示预览。 Also, how do i make the recorder show numbers in a countdown when the video starts recording?另外,当视频开始录制时,如何让录像机在倒计时中显示数字? The video doesn't save to my sd card even though i include即使我包含视频也不会保存到我的 SD 卡

recorder.OutPutFile("/sdcard/video.mp4");

And I have the permission WRITE_EXTERNAL_STORAGE in my manifest file.而且我在清单文件中拥有 WRITE_EXTERNAL_STORAGE 权限。

Another question is how do i save the video in an incrememntal manner.另一个问题是如何以增量方式保存视频。 For example, the first video that i record saves as "video1", the second video is "video2" and so on and so forth.例如,我录制的第一个视频保存为“video1”,第二个视频保存为“video2”,依此类推。 I will appreciate any help that I can get with this issue thank you.我将感谢您在此问题上获得的任何帮助,谢谢。

You didn't know the function setProfile().你不知道 function setProfile()。 The function setProfile() do all of the things below: function setProfile() 执行以下所有操作:

setProfile after setOutputFormat. setOutputFormat 之后的 setProfile。

public void setProfile(CamcorderProfile profile) {
    setOutputFormat(profile.fileFormat); 
    setVideoFrameRate(profile.videoFrameRate); 
    setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
    setVideoEncodingBitRate(profile.videoBitRate);
    setAudioEncodingBitRate(profile.audioBitRate);
    setAudioChannels(profile.audioChannels);
    setAudioSamplingRate(profile.audioSampleRate);
    setVideoEncoder(profile.videoCodec);
    setAudioEncoder(profile.audioCodec);
}

You also need to make sure you can write to the SD Card:您还需要确保可以写入 SD 卡:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Finally, you MUST set the audio and video source.最后,您必须设置音频和视频源。 Try replacing your initRecorder method with the one from the book to see if you get further:尝试用书中的方法替换你的 initRecorder 方法,看看你是否更进一步:

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.mp4");
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}

Do you have the permission set in AndroidManifest.xml?您是否在 AndroidManifest.xml 中设置了权限?

<uses-permission android:name="android.permission.CAMERA"></uses-permission>

Also I have had issues like this before try calling:在尝试调用之前我也遇到过这样的问题:

recorder.reset();记录器.reset();

at the start of your initRecorder() method.在您的 initRecorder() 方法的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM