2014-06-14 12 views
6

sto provando a registrare video e voglio mostrare i secondi della registrazione in corso.C'è un modo per ottenere la durata della riproduzione in corso

come devo fare?

pubblico startRecording void (Visualizza v) {

flipCamera.setVisibility(View.GONE); 
    captureImage.setVisibility(View.GONE); 
    String deviceMan = android.os.Build.MANUFACTURER; 
    this.mediaRecorder = new MediaRecorder(); 
    this.mediaRecorder.setCamera(this.camera); 

    camera.unlock(); 
    this.mediaRecorder.setCamera(camera); 
    this.mediaRecorder.setOrientationHint(90); 

    this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
    this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
    CamcorderProfile camcorderProfile_HQ = CamcorderProfile 
      .get(CamcorderProfile.QUALITY_480P); 
    this.mediaRecorder.setProfile(camcorderProfile_HQ); 
    this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath()); 
    this.mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec. 
    this.mediaRecorder.setMaxFileSize(5000000); 
    this.mediaRecorder.setPreviewDisplay(this.cameraPreview.getHolder() 
      .getSurface()); 

    this.mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 

    try { 
     this.mediaRecorder.prepare(); 
     // start the actual recording 
     // throws IllegalStateException if not prepared 
     this.mediaRecorder.start(); 
     Toast.makeText(this, R.string.recording, Toast.LENGTH_SHORT).show(); 

     this.toggleButtons(true); 

    } catch (Exception e) { 
     Log.wtf(TAG, "Failed to prepare MediaRecorder", e); 
     Toast.makeText(this, R.string.cannot_record, Toast.LENGTH_SHORT) 
       .show(); 
     this.releaseMediaRecorder(); 
    } 
} 

io sono abbastanza nuovo per android quindi per favore se qualcuno può aiutare.

risposta

10

È possibile utilizzare un timer e un gestore per ottenerlo. Nell'esempio seguente viene utilizzata una visualizzazione testuale per visualizzare la durata nel formato 00min: 00sec. Lo uso nel servizio in background ma puoi anche utilizzarlo in un'attività.

public TextView timerTextView; 
private long startHTime = 0L; 
private Handler customHandler = new Handler(); 
long timeInMilliseconds = 0L; 
long timeSwapBuff = 0L; 
long updatedTime = 0L; 


private Runnable updateTimerThread = new Runnable() { 

      public void run() { 

       timeInMilliseconds = SystemClock.uptimeMillis() - startHTime; 

       updatedTime = timeSwapBuff + timeInMilliseconds; 

       int secs = (int) (updatedTime/1000); 
       int mins = secs/60; 
       secs = secs % 60; 
       if (timerTextView != null) 
       timerTextView.setText("" + String.format("%02d", mins) + ":" 
         + String.format("%02d", secs)); 
       customHandler.postDelayed(this, 0); 
      } 

     }; 

Dove si inizia a registrare:

 ...... 
      this.mediaRecorder.start() 
      startHTime = SystemClock.uptimeMillis(); 
      customHandler.postDelayed(updateTimerThread, 0); 

Dove si arresta la registrazione:

 mediaRecorder.stop() 
    timeSwapBuff += timeInMilliseconds; 
customHandler.removeCallbacks(updateTimerThread); 
+0

Heyy @Nana Ghartey la soluzione funziona .. Ma la cosa è .. ogni volta che registrare un il video viene aggiunto l'ora. Ad esempio, ho registrato il primo video per 5 secondi e il secondo video per 3 secondi, quindi mostra 00:00 - 00:05 per il primo video e 00:00 - 00:08 per il secondo video invece dovrebbe mostrare 00:00 - 00:05 per il primo video e 00:00 - 00:03 per il secondo video. Puoi per favore aiutarmi qui. Ogni aiuto è apprezzato Grazie in anticipo;) :) – MashukKhan

+0

Basta fermare/avviare il timer dopo che ogni fase video è stata registrata –