2014-10-31 44 views
0

我在我的應用程序中使用自定義相機,這工作正常在果凍豆。現在三星Galaxy Tab OS更新後,自定義相機壞了。我從我的一位朋友那裏得到了這個報告,我沒有看到那臺平板電腦。我從崩潰報告表Carshylytics最新的(4.4.2)三星/操作系統更新後視頻攝像機崩潰

我的日誌報告:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.x.y/com.x.y.tools.EnregistrementVideoStackActivity}: java.lang.RuntimeException: Unable to initialize media recorder 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2053) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154) 
     at android.app.ActivityThread.access$700(ActivityThread.java:146) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4949) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810) 
     at dalvik.system.NativeStart.main(NativeStart.java) 
Caused by: java.lang.RuntimeException: Unable to initialize media recorder 
     at android.media.MediaRecorder.native_setup(MediaRecorder.java) 
     at android.media.MediaRecorder.<init>(MediaRecorder.java:121) 
     at com.x.y.tools.EnregistrementVideoStackActivity.<init>(EnregistrementVideoStackActivity.java:38) 
     at java.lang.Class.newInstanceImpl(Class.java) 
     at java.lang.Class.newInstance(Class.java:1319) 
     at android.app.Instrumentation.newActivity(Instrumentation.java:1068) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154) 
     at android.app.ActivityThread.access$700(ActivityThread.java:146) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4949) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

而且這個錯誤行

public MediaRecorder mrec = new MediaRecorder(); 

爲什麼它是發生有什麼想法?我該如何解決這個問題。我希望不需要相機代碼。但任何人都希望看到我可以在這裏發佈的代碼。

編輯:

我EnregistrementVideoStackActivity類:

public class EnregistrementVideoStackActivity extends Activity implements 
     SurfaceHolder.Callback { 
    private SurfaceHolder surfaceHolder; 
    private SurfaceView surfaceView; 
    public MediaRecorder mrec = new MediaRecorder(); 
    private Button startRecording = null; 
    private Button stopRecording = null; 
    File video; 
    private Camera mCamera; 
    private String output_path; 

    boolean isActivityRestarting = false; 

    Chronometer myChronometer; 
    Boolean recording = false; 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      if (recording == false) { 
       finish(); 
      } else { 
       stopRecording(); 
      } 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    public void onRestart() { 
     super.onRestart(); 
     isActivityRestarting = true; 
     finish(); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     if (isActivityRestarting) { 
      return; 
     } 

     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     /*getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
       WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);*/ 

     setContentView(R.layout.camera_surface); 
     Intent intent = getIntent(); 

     output_path = intent.getStringExtra("path"); 

     startRecording = (Button) findViewById(R.id.buttonstart); 
     stopRecording = (Button) findViewById(R.id.buttonstop); 
     try { 
      // This case can actually happen if the user opens and closes the 
      // camera too frequently. 
      // The problem is that we cannot really prevent this from happening 
      // as the user can easily 
      // get into a chain of activites and tries to escape using the back 
      // button. 
      // The most sensible solution would be to quit the entire EPostcard 
      // flow once the picture is sent. 
      mCamera = Camera.open(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), 
        "Please restart device, camera error", Toast.LENGTH_LONG) 
        .show(); 
      finish(); 
      return; 
     } 
     surfaceView = (SurfaceView) findViewById(R.id.surface_camera); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(this); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     myChronometer = (Chronometer) findViewById(R.id.chronometer1); 

     startRecording.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       try { 
        startRecording(); 
        myChronometer.setBase(SystemClock.elapsedRealtime()); 
        myChronometer.start(); 

        startRecording.setClickable(false); 
       } catch (Exception e) { 
        e.getMessage(); 
        e.printStackTrace(); 
        mrec.release(); 
       } 

      } 
     }); 
     stopRecording.setOnClickListener(new OnClickListener() { 

      public void onClick(View arg0) { 
       try { 
        stopRecording(); 
        myChronometer.stop(); 
        Intent intent = getIntent(); 
        intent.putExtra("gallery", "viewed"); 
        setResult(RESULT_OK, intent); 
       } catch (Exception e) { 
        e.getMessage(); 
       } 

      } 
     }); 

     myChronometer 
       .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { 

        public void onChronometerTick(Chronometer chronometer) { 
         long myElapsedMillis = SystemClock.elapsedRealtime() 
           - myChronometer.getBase(); 
         if (myElapsedMillis >= 120000) { 
          Toast.makeText(getApplicationContext(), 
            "Maximum Video limit reached", 
            Toast.LENGTH_LONG).show(); 
          stopRecording(); 
         } 
        } 
       }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add(0, 0, 0, "StartRecording"); 
     menu.add(0, 1, 0, "StopRecording"); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case 0: 
      try { 
       startRecording(); 
      } catch (Exception e) { 
       String message = e.getMessage(); 
       Log.e(null, "Problem Start" + message); 
       mrec.release(); 
      } 
      break; 

     case 1: // GoToAllNotes 
      mrec.stop(); 
      mrec.release(); 
      mrec = null; 
      break; 

     default: 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    protected void startRecording() throws IOException { 
     recording = true; 
     mrec = new MediaRecorder(); // Works well 
     mCamera.unlock(); 
     mrec.setCamera(mCamera); 
     mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
     mrec.setAudioSource(MediaRecorder.AudioSource.MIC); 

     mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 

     mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
     mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
     int width = 320; 
     int height = 240; 
     try { 
      // Check what resolutions are supported by your camera 
      List<Size> sizes = params.getSupportedPictureSizes(); 

      for (Size size : sizes) { 

       Log.e("TAG", "Available resolution: " + size.width + " " 
         + size.height); 
       width = size.width; 
       height = size.height; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     mrec.setVideoFrameRate(30); 
     mrec.setVideoSize(width, height); 
     mrec.setVideoEncodingBitRate(1700000); 
     mrec.setPreviewDisplay(surfaceHolder.getSurface()); 
     mrec.setOutputFile(output_path); 
     mrec.prepare(); 
     mrec.start(); 
    } 

    protected void stopRecording() { 
     try { 
      Log.e("stop recording", "Stop recording"); 
      recording = false; 
      mrec.stop(); 
      mrec.release(); 
      mCamera.release(); 
      // go out 

      Toast.makeText(getApplicationContext(), 
        "New Video Observation Saved", Toast.LENGTH_LONG).show(); 
     } catch (Exception npe) { 
      npe.printStackTrace(); 
     } 
     Intent intent = getIntent(); 
     setResult(RESULT_OK, intent); 
     finish(); 

    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // 
    } 

    Parameters params; 

    public void surfaceCreated(SurfaceHolder holder) { 
     if (!isActivityRestarting) { 
      if (mCamera != null) { 
       params = mCamera.getParameters(); 
       mCamera.setParameters(params); 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "Camera not available!", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     try { 
      if (mCamera != null) { 
       mCamera.stopPreview(); 
       mCamera.release(); 
       mCamera.setPreviewCallback(null); 
       mCamera = null; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     if (recording == false) { 
      finish(); 
     } else { 
      stopRecording(); 
     } 
    } 
} 
+0

的EnregistrementVideoStackActivity班上其他同學請發表 – 2014-10-31 09:58:48

+0

@CarlosRobles請檢查我的編輯 – Amsheer 2014-10-31 10:09:09

+0

我不知道這是否會解決任何問題,但你應該初始化'OnCreate'媒體記錄器。像這樣:'public MediaRecorder mrec; @Override public void onCreate(Bundle savedInstanceState){ if(isActivityRestarting){ return; } super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); mrec = new MediaRecorder(); // ..' – 2014-10-31 10:28:39

回答

0

我因爲視頻尺寸的誤差未設置。我解決這個

int width = 320; 
      int height = 240; 
      try { 
       //get the available sizes of the video 
       List<Size> tmpList = getSupportedVideoSizes(); 

       final List<Size> sizeList = new Vector<Size>(); 

       // compare the apsect ratio of the candidate sizes against the 
       // real ratio 
       Double aspectRatio = (Double.valueOf(getWindowManager() 
         .getDefaultDisplay().getHeight())/getWindowManager() 
         .getDefaultDisplay().getWidth()); 
       for (int i = tmpList.size() - 1; i > 0; i--) { 
        Double tmpRatio = Double.valueOf(tmpList.get(i).height) 
          /tmpList.get(i).width; 

        if (Math.abs(aspectRatio - tmpRatio) < .15) { 
         width = tmpList.get(i).width; 
         height = tmpList.get(i).height; 
         sizeList.add(tmpList.get(i)); 
        } 
       } 
       if (EnableLog.LOG_TAG) { 
        Log.e("tmpList", tmpList + "*"); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

mrec.setVideoSize(width, height); 

public List<Size> getSupportedVideoSizes() { 
     if (params.getSupportedVideoSizes() != null) { 
      return params.getSupportedVideoSizes(); 
     } else { 
      // Video sizes may be null, which indicates that all the supported 
      // preview sizes are supported for video recording. 
      return params.getSupportedPreviewSizes(); 
     } 
    } 
相關問題