videos on android - stuff what i learned
DESCRIPTION
A journey of how not to play videos, how to play them properly (maybe), and general things to be aware of when adding video to your Android app.TRANSCRIPT
![Page 1: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/1.jpg)
Videos on AndroidStuff What I Learned
![Page 2: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/2.jpg)
![Page 3: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/3.jpg)
![Page 4: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/4.jpg)
In the beginning…
![Page 5: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/5.jpg)
>200 Videos
![Page 6: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/6.jpg)
1080p H.264
![Page 7: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/7.jpg)
~10MB
![Page 8: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/8.jpg)
2GB
![Page 9: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/9.jpg)
50MB-100MB / routine
![Page 10: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/10.jpg)
0.5TB/month
![Page 11: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/11.jpg)
![Page 12: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/12.jpg)
1GB/month
![Page 13: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/13.jpg)
10MB → <1MB
![Page 14: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/14.jpg)
![Page 15: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/15.jpg)
1GB/month → <10MB total
![Page 16: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/16.jpg)
{ "id": 117, "name": "Hamstring Hanging Stretch", "description": "Position yourself lying on your front on a flat, raised surface. Allow the lower part of your leg to hang off the edge. Let the weight of your foot straighten your knee.", "sets": 3, "reps": 8, "hold_duration": 10, "rest": 30, "created": "2013-12-02 22:03:21", "modified": "2013-12-23 16:59:02", "video": { "id": 132, "url": "/ddaf3d7d59b4dd09fae6d34e760236d9/320p-16to9.mp4", "hold_time_seconds": 4.3, "loop_time_start_seconds": 0, "loop_time_end_seconds": 0, "etag": "b38f39cebe21487dd2ed123d16065d7b" } }
![Page 17: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/17.jpg)
Let’s Code!
![Page 18: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/18.jpg)
Both.
![Page 19: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/19.jpg)
I hate this device. It’s great!
![Page 20: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/20.jpg)
developer.android.com/guide/appendix/media-formats.html
![Page 21: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/21.jpg)
![Page 22: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/22.jpg)
Video ViewSurfaceView
MediaPlayer
start()pause()seekTo()
getDuration()
setVideoURI()setVideoPath()
setOnPreparedListener()setOnCompletionListener()
vv.vv.vv.
vv.
vv.vv.
vv.vv.
![Page 23: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/23.jpg)
VideoView vv = (VideoView) findViewById(R.id.videoView);vv.setVideoPath(path);!
vv.setOnPreparedListener (new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setLooping(true); }});!
vv.start();
![Page 24: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/24.jpg)
![Page 25: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/25.jpg)
![Page 26: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/26.jpg)
Disclaimer: This isn’t meant to happen.
![Page 27: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/27.jpg)
–Android API Docs
“SurfaceView punches a hole in its window to allow its surface to be displayed.”
![Page 28: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/28.jpg)
TextureView
• Android 4.0 • Behaves as a regular view • OpenGL, Video etc
![Page 29: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/29.jpg)
implement TextureView.SurfaceTextureListener
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)!
!
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)!
!
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)!
!
public void onSurfaceTextureUpdated(SurfaceTexture surface)
![Page 30: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/30.jpg)
public void onSurfaceTextureAvailable(SurfaceTexture surfaceT, int width, int height) { Surface surface = new Surface(surfaceT); try { mediaPlayer.setDataSource(path); } catch (Exception e) { }!
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setLooping(true); mp.start(); } });!
mediaPlayer.setSurface(surface);!
mediaPlayer.prepareAsync();}
![Page 31: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/31.jpg)
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceT) { mediaPlayer.release(); return true;}
![Page 32: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/32.jpg)
![Page 33: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/33.jpg)
Hey Mark, what about YouTube?
![Page 34: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/34.jpg)
YouTubePlayer
![Page 35: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/35.jpg)
MediaController• Just a view containing media controls • Implement all callbacks with setMediaPlayer(); • start(), pause(), seekTo() etc callbacks • Hook these up to your MediaPlayer :
public void start() { mediaPlayer.start();}!public void pause() { mediaPlayer.pause();}!public void seekTo(int i) { mediaPlayer.seekTo(i);}
![Page 36: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/36.jpg)
MediaMetadataRetriever
• Get a frame as a Bitmap • Useful when waiting for video to load or when using VideoView
metadataRetriever.setDataSource(ctx, path);!Bitmap bitmap = metadataRetriever.getFrameAtTime(0L, MediaMetadataRetriever.OPTION_CLOSEST);!if (bitmap != null) { BitmapDrawable previewBitmap = new BitmapDrawable(this.getResources(), bitmap); imageView.setImageDrawable(previewBitmap);}
![Page 37: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/37.jpg)
Playing Audio?
!setVolumeControlStream(AudioManager.STREAM_MUSIC);
![Page 38: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/38.jpg)
Y U NO USE INTENT?!
![Page 39: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/39.jpg)
In Summary
• Think about your content • “I need shiny 1080p” • Resolution/user dependent content? • Be nice, cache (and cache hard) if possible • Assume nothing and be prepared • TextureView when 4.0 • VideoView when <4.0 • Test • Test • Test • Test • Test • Test
![Page 40: Videos on Android - Stuff What I Learned](https://reader038.vdocument.in/reader038/viewer/2022110302/54b42d564a7959d7178b463e/html5/thumbnails/40.jpg)
Fin.
@mhemmings