无法启动的OnCreate记录 [英] Unable to start the recorder in Oncreate

查看:220
本文介绍了无法启动的OnCreate记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了其中的视频开始录制按钮点击一个简单的视频录像机的应用程序。这是我的code:

I made a simple video recorder app in which the video starts recording on Button click. This is my code:

package com.example.videocapture2;

import java.io.IOException;

import android.app.Activity; 
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
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.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements  SurfaceHolder.Callback,android.media.MediaRecorder.OnInfoListener{
    MediaRecorder recorder;
    SurfaceHolder holder;
    Button Rec = null;
    boolean recording = false;
    int count =1;
    @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();
    initRecorder();
    setContentView(R.layout.activity_main);

    SurfaceView cameraView = (SurfaceView) findViewById(R.id.videoview);

    holder = cameraView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    recorder.start();  //This is the line pointed to by the IllegalStateException

    //cameraView.setClickable(true);
   // cameraView.setOnClickListener(this);

    Rec = (Button)findViewById(R.id.mybutton);
    Rec.setOnClickListener(new View.OnClickListener() {

        @Override
         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();
            }
        }
    });
}

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

private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}



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();
    //Toast.makeText(getApplicationContext(), "Video is saved", Toast.LENGTH_LONG).show();
    //finish();
}

@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
    // TODO Auto-generated method stub
    System.out.println("Reached onInfoListener");
    if(what==android.media.MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED)
    {
        Toast.makeText(getApplicationContext(), "Video clip recorded", Toast.LENGTH_LONG).show();
    }
}


}

我应当怎样更改调用

What changes should I make to call the

recorder.start();

从的onCreate,或者更确切地说,后立即启动我的应用,而不是在OnClick方法?此外,如果我尝试直接从OnCreate中调用它,它抛出在 IllegalStateException异常在recorder.start()语句的行。

from onCreate, or rather immediately after I launch my app, instead of in the OnClick method? Moreover, if I try to call it directly from the OnCreate, it throws the IllegalStateException in the line of the recorder.start() statement.

logcat的:

04-03 12:48:51.005: E/Trace(26160): error opening trace file: No such file or directory (2)
04-03 12:48:51.025: V/ActivityThread(26160): Class path:   /data/app/com.example.videocapture2-2.apk, JNI path:  /data/data/com.example.videocapture2/lib
04-03 12:48:51.174: I/System.out(26160): In surface view:false
04-03 12:48:51.174: E/MediaRecorder(26160): start called in an invalid state: 4
04-03 12:48:51.175: D/AndroidRuntime(26160): Shutting down VM
04-03 12:48:51.175: W/dalvikvm(26160): threadid=1: thread exiting with uncaught  exception (group=0x41bd88a8)
04-03 12:48:51.178: E/AndroidRuntime(26160): FATAL EXCEPTION: main
04-03 12:48:51.178: E/AndroidRuntime(26160): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.videocapture2/com.example.videocapture2.MainActivity}:  java.lang.IllegalStateException
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.os.Looper.loop(Looper.java:153)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.ActivityThread.main(ActivityThread.java:5096)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at java.lang.reflect.Method.invoke(Method.java:511)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at dalvik.system.NativeStart.main(Native Method)
04-03 12:48:51.178: E/AndroidRuntime(26160): Caused by: java.lang.IllegalStateException
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.media.MediaRecorder.native_start(Native Method)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.media.MediaRecorder.start(MediaRecorder.java:728)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at com.example.videocapture2.MainActivity.onCreate(MainActivity.java:51)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.Activity.performCreate(Activity.java:5244)
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 
04-03 12:48:51.178: E/AndroidRuntime(26160):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
04-03 12:48:51.178: E/AndroidRuntime(26160):    ... 11 more

在解决这个尝试:

1),如果我把它的onCreate它不工作

1) It does not work if I keep it in onCreate

2),如果我把它在一个从所谓的onCreate新的线程,它不工作。

2) It does not work if I keep it in a new thread called from onCreate.

3)此外,如果我把它在onclick不起作用,并通过code,模拟点击使用

3) It also does not work if I keep it in the onClick, and simulate a click through code, using

myButton.performClick();
所有上述情况给出同样的异常错误。

myButton.performClick(); All above scenarios gives same exception error.

但有趣的是,如果

recorder.start();
提到有保持ButtonClick事件中,它工作正常。

recorder.start(); mentioned there is kept within buttonClick event, it works fine.

我不能够理解为什么呢?任何人都可以帮我解决这个?我需要被记录并且只要活动启动保存视频

I am not able to understand why? Anyone can help me solve this? I need the video to be recorded and saved as soon as the activity is launched.

推荐答案

由于这需要一些时间来记录对象加载到内存中。所以它会显示非法状态异常。

Because It takes some time to load recorder object into memory. so it will show illegal state exception.

如果你想使用的onCreate使用该处理程序作为做一些延迟。

If you want to use this in onCreate use Handler for make some delay.

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        recorder.start();
        }
    }, 1000);

更新:

把recorder.start();在prepareRecorder()方法

put recorder.start(); in prepareRecorder() method

private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());

try {
    recorder.prepare();
    recorder.start();
} catch (IllegalStateException e) {
    e.printStackTrace();
    finish();
} catch (IOException e) {
    e.printStackTrace();
    finish();
}
}

这篇关于无法启动的OnCreate记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆