Playing video on TextureView Playing video on TextureView android android

Playing video on TextureView


Here is how you can do it:(solution by the question author, that he posted as an update in the question)

Public class MediaPlayerDemo_Video extends Activity implements TextureView.SurfaceTextureListener { private MediaPlayer mMediaPlayer; private TextureView mPreview; @Override public void onCreate(Bundle icicle) {      super.onCreate(icicle);      mPreview = new TextureView(this);      mPreview.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));      mPreview.setSurfaceTextureListener(this);      extras = getIntent().getExtras();      setContentView(mPreview); } @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { Surface s = new Surface(surface); try {       mMediaPlayer= new MediaPlayer();       mMediaPlayer.setDataSource("http://daily3gp.com/vids/747.3gp");       mMediaPlayer.setSurface(s);       mMediaPlayer.prepare();       mMediaPlayer.setOnBufferingUpdateListener(this);       mMediaPlayer.setOnCompletionListener(this);       mMediaPlayer.setOnPreparedListener(this);       mMediaPlayer.setOnVideoSizeChangedListener(this);       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);       mMediaPlayer.start();      } catch (IllegalArgumentException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (SecurityException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IllegalStateException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }   } 

And animating it works really well.


I had the same problem, and solved it with a TextureView. I found setScaleX and setScaleY very useful, if this helps anyone. http://developer.android.com/reference/android/view/View.html#setScaleX%28float%29

However if you are only targeting API 16+:

mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);

should do it:)


Your setContentView(mPreview); needs to be called before the

mPreview = (TextureView) findViewById(R.id.surface);mPreview.setSurfaceTextureListener(this);