Videos on Android - Stuff What I Learned

40
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 of Videos on Android - Stuff What I Learned

Page 1: Videos on Android - Stuff What I Learned

Videos on AndroidStuff What I Learned

Page 2: Videos on Android - Stuff What I Learned
Page 3: Videos on Android - Stuff What I Learned
Page 4: Videos on Android - Stuff What I Learned

In the beginning…

Page 5: Videos on Android - Stuff What I Learned

>200 Videos

Page 6: Videos on Android - Stuff What I Learned

1080p H.264

Page 7: Videos on Android - Stuff What I Learned

~10MB

Page 8: Videos on Android - Stuff What I Learned

2GB

Page 9: Videos on Android - Stuff What I Learned

50MB-100MB / routine

Page 10: Videos on Android - Stuff What I Learned

0.5TB/month

Page 11: Videos on Android - Stuff What I Learned
Page 12: Videos on Android - Stuff What I Learned

1GB/month

Page 13: Videos on Android - Stuff What I Learned

10MB → <1MB

Page 14: Videos on Android - Stuff What I Learned
Page 15: Videos on Android - Stuff What I Learned

1GB/month → <10MB total

Page 16: Videos on Android - Stuff What I Learned

{ "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

Let’s Code!

Page 18: Videos on Android - Stuff What I Learned

Both.

Page 19: Videos on Android - Stuff What I Learned

I hate this device. It’s great!

Page 20: Videos on Android - Stuff What I Learned

developer.android.com/guide/appendix/media-formats.html

Page 21: Videos on Android - Stuff What I Learned
Page 22: Videos on Android - Stuff What I Learned

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

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
Page 25: Videos on Android - Stuff What I Learned
Page 26: Videos on Android - Stuff What I Learned

Disclaimer: This isn’t meant to happen.

Page 27: Videos on Android - Stuff What I Learned

–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

TextureView

• Android 4.0 • Behaves as a regular view • OpenGL, Video etc

Page 29: Videos on Android - Stuff What I Learned

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

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

public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceT) { mediaPlayer.release(); return true;}

Page 32: Videos on Android - Stuff What I Learned
Page 33: Videos on Android - Stuff What I Learned

Hey Mark, what about YouTube?

Page 34: Videos on Android - Stuff What I Learned

YouTubePlayer

Page 35: Videos on Android - Stuff What I Learned

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

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

Playing Audio?

!setVolumeControlStream(AudioManager.STREAM_MUSIC);

Page 38: Videos on Android - Stuff What I Learned

Y U NO USE INTENT?!

Page 39: Videos on Android - Stuff What I Learned

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

Fin.

@mhemmings