2

我在我的 Android 应用程序中使用视频录制。它在Android2.2Android2.3设备中运行良好,但应用程序在Android4.0.

这是我的代码:

  class VideoPreview extends SurfaceView implements SurfaceHolder.Callback
  {
  MediaRecorder recorder;
  SurfaceHolder holder = null; 
  FileOutputStream fileout; 
 public VideoPreview(Context context,MediaRecorder temprecorder) {
 super(context);
    recorder= temprecorder;
    holder= getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   }
  public void surfaceCreated(SurfaceHolder holder){
    }
  public void surfaceDestroyed(SurfaceHolder holder)
  {
      if(recorder!=null)
      {
    recorder.release();
    Log.d("Video Preview", "Exception1");
    recorder = null;
      }
  }

  //surfaceChanged : This method is called after the surface is created.
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
  {
  }
  public void  stop(){

      if(recorder!=null)
      {
      recorder.release();
      recorder = null;
      }        
  }
    @Override 
    public boolean onKeyDown (int keyCode, KeyEvent event){ 

         return false; 

    } 

  public boolean start()  {     
      String state = android.os.Environment.getExternalStorageState(); 
      String path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/myvideo.mp4";
      File fname = new File(path);
      if (!state.equals(Environment.MEDIA_MOUNTED)) return false;
      File directory = new File(path).getParentFile(); 
      if(!directory.exists() && !directory.mkdirs())   return false; 

          if (recorder == null)    
            recorder = new MediaRecorder();         

           int out_format = MediaRecorder.OutputFormat.MPEG_4;
           //THREE_GPP;
          recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);    
          recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
            recorder.setOutputFormat(out_format); 
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
            //recorder.setMaxDuration(20000); 
            //recorder.setVideoSize(352,288);   // 352, 288 m_recorder.setVideoSize(320, 240); 176,144
            recorder.setVideoFrameRate(15);
            recorder.setOutputFile(fname.getPath());                
           /* videoview = (VideoView) findViewById(R.id.videosurface);
            SurfaceHolder holder = videoview.getHolder();
            mediarecorder.setPreviewDisplay(holder.getSurface());*/             
            recorder.setPreviewDisplay(holder.getSurface());
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (!Recorder_Prepare())
            return false;
            else
            return true;
      }    

private boolean Recorder_Prepare()
    {

        try {
            if (recorder != null){
            recorder.prepare();
            Log.d("Video Preview", "Exception2");
            recorder.start();     
            Log.d("Video Preview", "Exception3");
            }
        }
            catch (IllegalStateException e) {
                return false;
            }
            catch (IOException e) { 
                return false;
            }
            return true;            
    }             
  }

这是我的logcat抛出异常,recorder.start();如下所示:

      06-11 13:54:54.926: E/MediaRecorder(22582): start failed: -19
      06-11 13:54:54.961: E/AndroidRuntime(22582): FATAL EXCEPTION: Thread-2201
      06-11 13:54:54.961: E/AndroidRuntime(22582): java.lang.RuntimeException: start failed.
      06-11 13:54:54.961: E/AndroidRuntime(22582):  at android.media.MediaRecorder.start(Native Method)
      06-11 13:54:54.961: E/AndroidRuntime(22582):  at  com.Myapp.VideoPreview.Recorder_Prepare(VideoPreview.java:106)
      06-11 13:54:54.961: E/AndroidRuntime(22582):  at com.Myapp.VideoPreview.start(VideoPreview.java:94)
      06-11 13:54:54.961: E/AndroidRuntime(22582):  at com.Myapp.videoRecord$1.run(videoRecord.java:94)
      06-11 13:54:54.961: E/AndroidRuntime(22582):  at java.lang.Thread.run(Thread.java:856)

我在Samsung Nexus S中验证了该应用程序,但它没有SDCard支持。我用谷歌搜索了 4 个小时来解决问题,但找不到解决方案。上述程序中是否有任何不推荐使用的方法?请帮我解决问题。

4

1 回答 1

1

Android 4.0 引入了新的运行时检查,以确保您不会做一些愚蠢的事情,比如在主线程上访问网络。

您是否在做一些愚蠢的事情,例如在主线程上访问网络?您的媒体是否通过网络流式传输,您VideoPreview.start()从哪里调用您的方法?

于 2012-06-11T10:37:19.380 回答