21.04.2014 Views

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CURSO ANDROID<br />

59<br />

public void surfaceDestroyed(SurfaceHol<strong>de</strong>r hol<strong>de</strong>r) {<br />

mediaRecor<strong>de</strong>r.release();<br />

mediaPlayer.release();<br />

}<br />

Verificamos si las variables son nulas (para ejecutar este código sólo una vez) y luego <strong>de</strong> inicializarlas se<br />

coloca el SurfaceHol<strong>de</strong>r como display para la vista previa <strong>de</strong> la grabación y para la vista <strong>de</strong> la reproducción:<br />

@Overri<strong>de</strong><br />

public void surfaceCreated(SurfaceHol<strong>de</strong>r hol<strong>de</strong>r) {<br />

if (mediaRecor<strong>de</strong>r == null) {<br />

mediaRecor<strong>de</strong>r = new MediaRecor<strong>de</strong>r();<br />

mediaRecor<strong>de</strong>r.setPreviewDisplay(hol<strong>de</strong>r.getSurface());<br />

}<br />

if (mediaPlayer == null) {<br />

mediaPlayer = new MediaPlayer();<br />

mediaPlayer.setDisplay(hol<strong>de</strong>r);<br />

}}<br />

Adicionalmente, se agrega un método para preparar la grabación configurando los atributos <strong>de</strong> la fuente<br />

para audio y ví<strong>de</strong>o, el formado y el codificador.<br />

public void prepareRecor<strong>de</strong>r(){<br />

mediaRecor<strong>de</strong>r.setAudioSource(MediaRecor<strong>de</strong>r.AudioSource.MIC);<br />

mediaRecor<strong>de</strong>r.setVi<strong>de</strong>oSource(MediaRecor<strong>de</strong>r.Vi<strong>de</strong>oSource.CAMERA);<br />

mediaRecor<strong>de</strong>r.setOutputFormat(MediaRecor<strong>de</strong>r.OutputFormat.MPEG_4);<br />

mediaRecor<strong>de</strong>r.setAudioEnco<strong>de</strong>r(MediaRecor<strong>de</strong>r.AudioEnco<strong>de</strong>r.DEFAULT);<br />

mediaRecor<strong>de</strong>r.setVi<strong>de</strong>oEnco<strong>de</strong>r(MediaRecor<strong>de</strong>r.Vi<strong>de</strong>oEnco<strong>de</strong>r.MPEG_4_SP);<br />

}<br />

Dentro <strong>de</strong>l método onCreate realizamos algunas inicializaciones, primero para la variable <strong>de</strong>l nombre<br />

<strong>de</strong>l archivo:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!