0

Iam trying to animate a video rendered using texture view, the animation works fine , but when i scale the texture to a different size the media player does not scale to fit the size of the texture,the video is always playing full screen in the background and i can only see part of the video when i scale the texture
my code works fine on some android devices running android 4.0 but does not work on devices with later versions of android . heres my code for the animation kindly tell me where the problem is ..

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener{
AnimationSet animset;
TextureView myTexture;
MediaPlayer mMediaPlayer;
Surface s;
int height;
static final String LogFileName = "Log";
int width;
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  myTexture = new TextureView(this);
  myTexture.setSurfaceTextureListener(this);
  setContentView(myTexture);    
   }

   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
   int arg2) {


   String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
s = new Surface(arg0);

   try {
       mMediaPlayer= new MediaPlayer();
       mMediaPlayer.setSurface(s);
       mMediaPlayer.setDataSource(mediaStorageDir+"/AirIndia.mp4");
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
       mMediaPlayer.prepareAsync();
       mMediaPlayer.setLooping(true);
       mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

                mMediaPlayer.start();

            }
        });   


// to animate the texture   

     animset =new  AnimationSet(true);
     animset.addAnimation(scaling);
     animset.addAnimation(translate);
     animset.setStartOffset(20000);

  Animation fulscaling = new ScaleAnimation(1.0f, 1.3333f, 1.0f, 1.3333f);
     TranslateAnimation fulltranslate = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, -0.3333f,        0, 0.0f, 0, 0.0f);
          fulltranslate.setStartOffset(10000);

        animset.setDuration(1000);
        animset.setFillAfter(true);
        animset.setFillEnabled(true);
        fulscaling.setStartOffset(10000);

        animset.addAnimation(fulscaling);
    animset.addAnimation(fulltranslate);  

    myTexture.setAnimation(animset); 


    animset.setAnimationListener(new AnimationListener() {

          @Override
          public void onAnimationEnd(Animation arg0) {

              myTexture.startAnimation(animset);
          }

          @Override
          public void onAnimationRepeat(Animation arg0) {
              // TODO Auto-generated method stub

          }

          @Override
          public void onAnimationStart(Animation arg0) {
              // TODO Auto-generated method stub

          }

      });








} catch (IllegalArgumentException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} catch (SecurityException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} catch (IllegalStateException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} catch (IOException e) {
    showToast(e.getMessage());
    e.printStackTrace();
} 

}

user2949215
  • 493
  • 1
  • 6
  • 11

1 Answers1

5

If you want to scale the TextureView's contents, use setTransform(). For an example, see adjustAspectRatio() in Grafika's PlayMovieActivity class, which changes the size of the TextureView contents to match the aspect ratio of the video being played.

fadden
  • 48,613
  • 5
  • 104
  • 152
  • thank you for the response, I've tried setTranform it also doesnot scale the video to fit the textureview . even when iam changing the layout parameters of the texture the video doesnot scale, but using a customised videoview by overriding onmeasure iam getting a scaled video , but iam unable to apply scale and translate animations on videoview . – user2949215 Nov 28 '14 at 04:33
  • It doesn't scale the *video* to fit the TextureView. The video is sent to a surface, the surface is converted to a GLES texture, and the texture is rendered. The transformation matrix determines how the texture is rendered. For the Grafika video player, the TextureView window is always the same size, but the transform matrix renders the video in only part of the window (to maintain the proper aspect ratio). VideoView is different. For a SurfaceView you need to wrap it in a custom frame layout; see AspectFrameLayout and how it's used in PlayMovieSurfaceActivity.java in Grafika. – fadden Nov 28 '14 at 05:58
  • thankyou for helping .the problem remains.My code is working fine on the mobile and tabs but its not working for the android tv box. i guess its a device issue . Do i need to do any settings ? in the device ? – user2949215 Nov 28 '14 at 09:44
  • I haven't worked with an Android TV box, but I wouldn't have expected this to behave differently. I seem to recall that the TV boxes have a special layer for live TV that isn't composited by SurfaceFlinger, but that shouldn't affect playback of a video by an app. – fadden Nov 28 '14 at 16:57
  • finally i could get the texture view video scaling in my android tv box . your grafika project has helped me a lot to clarify lots of things . thank you very much . I have more one question to ask , In the PlayMovieActivity what changes are needed to make it play the complete video files i.e.., including the audio not just the video stream . thank you in advance – user2949215 Dec 05 '14 at 17:56
  • You'd need a second MediaCodec to decode the audio stream, and then coordinate the audio and video streams to get proper lip sync. I don't recommend that. You're better off with MediaPlayer, which takes care of decoding both streams. – fadden Dec 05 '14 at 18:27
  • thankyou .. when i do it with a media player it works well with other devices like nexus . but its not working in android tv box – user2949215 Dec 06 '14 at 04:05