2013-06-10 36 views
6

Kullanıcının ekrana dokunarak videoları hızlı bir şekilde almasına olanak tanıyan temel bir video kamera oluşturmaya çalışıyorum, ancak bu en başta gelen temel sorun Android MediaRecorder'tı. hızlı başlatma ve kilitlenme olmadan yeniden başlatmaya izin vermez, kullanıcı çeker ve sonra durur ve daha sonra tekrar çekim yapmaya tekrar devam eder her zaman çöker. Bunu doğrudan düzeltmenin bir yolu olup olmadığından emin değilim, bu yüzden bunu başarmak için başka bir yöntem geliştirdim tek bir video kaydetmek, ancak mediaRecorder aslında dosyaya yazdığında bazı kontroller elde etmeye çalışmak. ancak bu tamamen FileDescriptor kullanarak çalıştıramazdım, aşağıda orijinal kodumu yapıştırır ve yazımı kontrol eden ikinci denemede kullandığım metodum, daha sonra duraklatma işlevini sürdürmek için herhangi bir kodu ayarlamak için herhangi bir yol var mı?MediaRecorder start(), çok hızlı çağrılırsa başarısız olur

İşte
public class MainActivity extends Activity implements SurfaceHolder.Callback { 

public static final String LOGTAG = "VIDEOCAPTURE"; 

private MediaRecorder recorder; 
private SurfaceHolder holder; 
private CamcorderProfile camcorderProfile; 
private Camera camera; 

boolean recording = false; 
boolean usecamera = true; 
boolean previewRunning = false; 

double timer = 0; 
ProgressBar pb; 
boolean neverEnd; 

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

    camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW); 

    setContentView(R.layout.activity_main); 

    pb = (ProgressBar) findViewById(R.id.progressBar1); 
    pb.setProgress(0); 

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

    cameraView.setClickable(true); 
    cameraView.setOnTouchListener(new OnTouchListener() { 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 

       recording = true; 
       new recordVideo().execute(); 
       Log.v(LOGTAG, "Recording Started"); 

       return true; 
      } else if (event.getAction() == MotionEvent.ACTION_UP) { 

       recording = false; 

       return true; 
      } 
      return false; 
     } 
    }); 
} 

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

    if (usecamera) { 
     camera.unlock(); 
     recorder.setCamera(camera); 
    } 

    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

    recorder.setProfile(camcorderProfile); 


    Calendar calendarTime = Calendar.getInstance(); 

//initial attempt using a file path with setoutputfile 

    File file = new File(Environment.getExternalStorageDirectory(), 
      String.valueOf(calendarTime.getTimeInMillis()) + ".mp4"); 

    if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.THREE_GPP) { 

     recorder.setOutputFile(file.getAbsolutePath()); 
    } else if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) { 

     recorder.setOutputFile(file.getAbsolutePath()); 
    } else { 

     recorder.setOutputFile(file.getAbsolutePath()); 

    } 
    // recorder.setMaxDuration(50000); // 50 seconds 
    // recorder.setMaxFileSize(5000000); // Approximately 5 megabytes 

    boolean initialized = false; 

    while (!initialized) { 
     try { 
      recorder.prepare(); 
      initialized = true; 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
      // finish(); 
      initialized = false; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      // finish(); 
      initialized = false; 
     } 
    } 

} 

public void surfaceCreated(SurfaceHolder holder) { 
    Log.v(LOGTAG, "surfaceCreated"); 

    if (usecamera) { 
     camera = Camera.open(); 

     try { 
      camera.setPreviewDisplay(holder); 
      camera.startPreview(); 
      previewRunning = true; 
     } catch (IOException e) { 
      Log.e(LOGTAG, e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 

} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    Log.v(LOGTAG, "surfaceChanged"); 

    if (!recording && usecamera) { 
     if (previewRunning) { 
      camera.stopPreview(); 
     } 

     try { 
      Camera.Parameters p = camera.getParameters(); 

      p.setPreviewSize(camcorderProfile.videoFrameWidth, 
        camcorderProfile.videoFrameHeight); 
      p.setPreviewFrameRate(camcorderProfile.videoFrameRate); 

      camera.setParameters(p); 

      camera.setPreviewDisplay(holder); 
      camera.startPreview(); 
      previewRunning = true; 
     } catch (IOException e) { 
      Log.e(LOGTAG, e.getMessage()); 
      e.printStackTrace(); 
     } 

     prepareRecorder(); 
    } 
} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    Log.v(LOGTAG, "surfaceDestroyed"); 
    if (recording) { 
     recorder.stop(); 
     recording = false; 
    } 
    recorder.release(); 
    if (usecamera) { 
     previewRunning = false; 
     // camera.lock(); 
     camera.release(); 
    } 
    finish(); 
} 

private class recordVideo extends AsyncTask<Void, Integer, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 

     try { 

      recorder.start(); 
      while (recording) { 

       Thread.sleep(100); 

       publishProgress(); 
      } 
      recorder.stop(); 
      recorder.release(); 
      recorder = null; 

      // recorder.release(); 

      Log.v(LOGTAG, "Recording Stopped"); 
      // Let's prepareRecorder so we can record again 
      prepareRecorder(); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 

     if (recording) { 
      timer += 0.1; 
      pb.setProgress((int) (timer * 10)); 
     } 

    } 

} 
} 

Filedescriptor kullanarak yöntemi bu işe yaramadı, sadece bir yarattı: herhangi bir yardım uzun bir yol setOutPutFile() bir dosya ayar ortak bir yol kullanarak Benim ilk girişimi

sayesinde

gidecek dosya ama hiç yazma kendisine: prepareRecorder (sonra

//Pass it into setOutputFile() like this 
recorder.setOutputFile(getStreamFd()); 

    private FileDescriptor getStreamFd() { 
    ParcelFileDescriptor pipe = null; 
    try { 

     Calendar calendarTime = Calendar.getInstance(); 

     File file = new File(Environment.getExternalStorageDirectory(), 
       String.valueOf(calendarTime.getTimeInMillis()) + ".mp4"); 

     pipe = ParcelFileDescriptor.open(file, 
       ParcelFileDescriptor.MODE_CREATE 
         | ParcelFileDescriptor.MODE_APPEND 
         | ParcelFileDescriptor.MODE_WORLD_WRITEABLE); 

     byte[] buf = new byte[1024]; 
     int len; 

     FileOutputStream out = new FileOutputStream(FileDescriptor.out); 

     InputStream is = new FileInputStream(FileDescriptor.in); 

     while (usecamera) { 
         if(recordng){ 
         out.write(buf, 0, len); 
         } 

     } 

     is.close(); 
     out.close(); 

    } catch (IOException e) { 
     Log.e(getClass().getSimpleName(), "Exception opening pipe", e); 
    } 

    return pipe.getFileDescriptor(); 
} 
+1

AsynkTask üzerine) (çağrı prepareRecorder önce (yanlış) cameraView.setClickable çağrı Bir şey yapmayı başardın mı? Aynı problemim var ... tekrar tekrar durup medya kaydediciyi başlatıyorum, başlangıçta bir süre sonra() "start başarısız -2147483648" hata kodu ... – Hitman

cevap

0

çağrı cameraView.setClickable (true)) ve

İlgili konular