Android	
  Audio	
  and	
  Video	
  

          Jussi	
  Pohjolainen	
  
Audio	
  and	
  Video	
  
‱  Supports	
  variety	
  of	
  common	
  media	
  types	
  
    –  See	
  Media	
  Formats	
  
    –  http://developer.android.com/guide/appendix/
       media-formats.html
‱  Can	
  play	
  raw	
  resources,	
  standalone	
 Â ïŹles	
  and	
  
   streams	
  
‱  MediaPlayer	
  for	
  playing	
  and	
  MediaCapture	
  for	
  
   capturing	
  
‱  Emulator	
  does	
  not	
  have	
  the	
  capability	
  to	
  record	
  
   audio	
  or	
  video	
  
MediaPlayer	
  
Audio	
  and	
  Video	
  Playback	
  
‱  From	
  raw	
  (res/raw	
  folder)	
  
    –  MediaPlayer mp = MediaPlayer.create(context,
       R.raw.sound_file_1);
    –  mp.start();
‱  To	
  replay	
  the	
  media,	
  call	
  reset()	
  and	
  
     prepare()
‱  To	
  pause,	
  use	
  pause()	
  and	
  to	
  stop	
  use	
  
     stop()
	
  
Playing	
  from	
  File	
  or	
  Stream	
  
‱  Use	
  setDataSource()	
  –	
  method	
  
   –  MediaPlayer mp = new MediaPlayer();
   –  mp.setDataSource(PATH_TO_FILE OR URL);
   –  mp.prepare();
   –  mp.start();
Playing	
  Videos:	
  VideoView	
  Widget	
  
‱  If	
  using	
  MediaPlayer	
  for	
  videos,	
  you	
  must	
  set	
  
   the	
  SurfaceView	
  for	
  the	
  video	
  
‱  Easier	
  way:	
  use	
  VideoView	
  Widget	
  
‱  VideoView	
  wraps	
  MediaPlayer	
  and	
  
   SurfaceView	
  
‱  How?	
  
    –    VideoView videoview = (VideoView) findById..
    –    videoview.setVideoPath(
);
    –    videoview.setMediaController(new MediaController(this));
    –    videoview.requestFocus();
MediaRecorder:	
  Record	
  Audio	
  and	
  Video	
  
Audio	
  Capture	
  
‱  Create	
  instance	
  of	
  MediaRecorder	
  
‱  Create	
  a	
 Â ïŹle	
  path	
  for	
  the	
  data	
  to	
  go	
  
‱  Set	
  audio	
  source	
  to	
  
   MediaRecorder.AudioSource.MIC	
  
‱  Set	
  output	
  format	
  
‱  Set	
  audio	
  encoder	
  
‱  Call	
  prepare,	
  start	
  and	
  stop	
  
‱  App	
  must	
  have	
  permission	
  to	
  make	
  audio	
  
   captures!	
  
Example	
  of	
  Audio	
  Capture	
  
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

// 3GPP
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

// AMR (Narrowband) audio codec
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start();   // Recording is now started
...
recorder.stop();
Couple	
  Things	
  to	
  Remember	
  
‱  Remember	
  to	
  set	
  permissions:	
  
    –  android.permission.RECORD_AUDIO	
  
    –  android.permission.WRITE_EXTERNAL_STORAGE	
  
‱  To	
  get	
  the	
  full	
  path	
  to	
  external	
  storage,	
  use:	
  
    –  	
  String
               path =
       Environment.getExternalStorageDirectory().getAbsolutePath
       ();
Video	
  Capture	
  
‱  MediaRecorder	
  is	
  used	
  also	
  to	
  capture	
  Video	
  
‱  You	
  can	
  preview	
  the	
  the	
  captured	
  video	
  on	
  
   any	
  SurfaceView	
  (for	
  example	
  VideoWidget):	
  
    – 
         recordMyVideo.setPreviewDisplay(	
  videoView.get
         Holder().getSurface()	
  );	
  	
  
Example	
  of	
  Video	
  Capture	
  
 MediaRecorder recordMyVideo = new MediaRecorder();
recordMyVideo.setAudioSource(MediaRecorder.AudioSource.MIC);
recordMyVideo.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recordMyVideo.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recordMyVideo.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recordMyVideo.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recordMyVideo.setMaxDuration(5000);
recordMyVideo.setVideoSize(320, 240);
recordMyVideo.setVideoFrameRate(15);
// get full path to sd-card
recordMyVideo.setOutputFile( getMyVideoSourcePath() );
// use videoWidget as preview
recordMyVideo.setPreviewDisplay( videoView.getHolder().getSurface() );

Android Multimedia Support

  • 1.
    Android  Audio  and  Video   Jussi  Pohjolainen  
  • 2.
    Audio  and  Video   ‱  Supports  variety  of  common  media  types   –  See  Media  Formats   –  http://developer.android.com/guide/appendix/ media-formats.html ‱  Can  play  raw  resources,  standalone Â ïŹles  and   streams   ‱  MediaPlayer  for  playing  and  MediaCapture  for   capturing   ‱  Emulator  does  not  have  the  capability  to  record   audio  or  video  
  • 3.
  • 4.
    Audio  and  Video  Playback   ‱  From  raw  (res/raw  folder)   –  MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); –  mp.start(); ‱  To  replay  the  media,  call  reset()  and   prepare() ‱  To  pause,  use  pause()  and  to  stop  use   stop()  
  • 5.
    Playing  from  File  or  Stream   ‱  Use  setDataSource()  –  method   –  MediaPlayer mp = new MediaPlayer(); –  mp.setDataSource(PATH_TO_FILE OR URL); –  mp.prepare(); –  mp.start();
  • 6.
    Playing  Videos:  VideoView  Widget   ‱  If  using  MediaPlayer  for  videos,  you  must  set   the  SurfaceView  for  the  video   ‱  Easier  way:  use  VideoView  Widget   ‱  VideoView  wraps  MediaPlayer  and   SurfaceView   ‱  How?   –  VideoView videoview = (VideoView) findById.. –  videoview.setVideoPath(
); –  videoview.setMediaController(new MediaController(this)); –  videoview.requestFocus();
  • 7.
  • 8.
    Audio  Capture   ‱ Create  instance  of  MediaRecorder   ‱  Create  a Â ïŹle  path  for  the  data  to  go   ‱  Set  audio  source  to   MediaRecorder.AudioSource.MIC   ‱  Set  output  format   ‱  Set  audio  encoder   ‱  Call  prepare,  start  and  stop   ‱  App  must  have  permission  to  make  audio   captures!  
  • 9.
    Example  of  Audio  Capture   MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 3GPP recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // AMR (Narrowband) audio codec recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(PATH_NAME); recorder.prepare(); recorder.start(); // Recording is now started ... recorder.stop();
  • 10.
    Couple  Things  to  Remember   ‱  Remember  to  set  permissions:   –  android.permission.RECORD_AUDIO   –  android.permission.WRITE_EXTERNAL_STORAGE   ‱  To  get  the  full  path  to  external  storage,  use:   –   String path = Environment.getExternalStorageDirectory().getAbsolutePath ();
  • 11.
    Video  Capture   ‱ MediaRecorder  is  used  also  to  capture  Video   ‱  You  can  preview  the  the  captured  video  on   any  SurfaceView  (for  example  VideoWidget):   –  recordMyVideo.setPreviewDisplay(  videoView.get Holder().getSurface()  );    
  • 12.
    Example  of  Video  Capture   MediaRecorder recordMyVideo = new MediaRecorder(); recordMyVideo.setAudioSource(MediaRecorder.AudioSource.MIC); recordMyVideo.setVideoSource(MediaRecorder.VideoSource.CAMERA); recordMyVideo.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recordMyVideo.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recordMyVideo.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); recordMyVideo.setMaxDuration(5000); recordMyVideo.setVideoSize(320, 240); recordMyVideo.setVideoFrameRate(15); // get full path to sd-card recordMyVideo.setOutputFile( getMyVideoSourcePath() ); // use videoWidget as preview recordMyVideo.setPreviewDisplay( videoView.getHolder().getSurface() );