58

In the documentation of Android TextureView it says that you can use a TextureView to play video: But I cant seem to find any example of how to do this. Does anyone know?

I need to use a textureView because I want to animate the video. I want to play a video in .3gp/.mp4 format, not video from the Camera :)

Any help would be appreciated..

UPDATE:

Solution is posted as a community wiki answer

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Zelleriation
  • 2,834
  • 1
  • 23
  • 23
  • did you turn on hardware acel on the activity? – Ron May 24 '12 at 11:55
  • I just set it in the Manifest :) – Zelleriation May 25 '12 at 21:50
  • Would you have any idea why onSurfaceTextureAvailable never called? – George Taskos Dec 10 '12 at 22:26
  • Also. you can always check http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/widget/VideoView.java for how Google did the regular VideoView. – Edison Apr 17 '13 at 18:45
  • @Edison and if you actually looked at the source you would see they use a SurfaceView, not a TextureView. Question asks for TextureView. – dcow Oct 01 '13 at 19:45
  • @David, you can simply switch out the callbacks. Vine on Android right now uses the VideoView that inherits from TextureView. (I may open source it at one point soon) – Edison Oct 01 '13 at 20:48
  • @Edison provided you know to make a new Surface over the TextureView -- which the VideoView doesn't show how to do. – dcow Oct 01 '13 at 22:20
  • @Edison I trying play auto video in list view like vine. I created a custom texture view, which is working fine if i use it as a single view in an activity. However if i use it as an item view in list view - SurfaceTextureListener never gets called and surface texture is always null. Therefore no video are played. Can you shed some light, on how can i fix this. https://raw.github.com/ash-gupta/MyTestVideoView/master/MyTestVideoView – aNoviceGuy Oct 09 '13 at 02:54
  • @aNoviceGuy is the view visible? Your view looks fine (though I did not test it). Auto play just need you to do start in onPrepared. – Edison Oct 09 '13 at 16:01
  • @Edison - Like i said - it works when i use it as a single view in an activity. But When i am using it in a list item - it is not showing anything.I set the listview item background to white , and that is what comes up, items with with white color only, no video or image. I checked that my view is visible but still nothing shows up :( Mediaplayer on prepare is calling start. In my adapter get view - i am just doing holder.videoView.setVideoPath("any mp4 url"); holder.videoView.start(); – aNoviceGuy Oct 09 '13 at 23:18
  • 1
    @Zelleriation you should post your solution as an answer. It worked well for me. – Jens Zalzala Oct 10 '13 at 22:13

3 Answers3

55

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.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
6

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:)

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Maximosaic
  • 604
  • 6
  • 14
-2

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

mPreview = (TextureView) findViewById(R.id.surface);
mPreview.setSurfaceTextureListener(this);
Ami
  • 387
  • 3
  • 11
  • here you answer the question not to correct the answer.. use comment for correcting the it. – MBH Mar 22 '16 at 13:14