57

在 Android TextureView的文档中,它说您可以使用 TextureView 播放视频:但我似乎找不到任何示例来说明如何做到这一点。有人知道吗?

我需要使用 atextureView因为我想为视频制作动画。我想播放 .3gp/.mp4 格式的视频,而不是来自相机的视频 :)

任何帮助,将不胜感激..

更新:

解决方案作为社区 wiki 答案发布

4

3 回答 3

53

您可以这样做:(问题作者的解决方案,他在问题中作为更新发布)

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();
    }   
} 

并且动画效果非常好。

于 2014-10-20T15:48:53.227 回答
6

我有同样的问题,并用TextureView. 如果这对任何人有帮助,我发现setScaleX并且setScaleY非常有用。http://developer.android.com/reference/android/view/View.html#setScaleX%28float%29

但是,如果您只针对 API 16+:

mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);

应该这样做:)

于 2013-11-27T10:27:41.130 回答
-2

setContentView(mPreview);需要在

mPreview = (TextureView) findViewById(R.id.surface);
mPreview.setSurfaceTextureListener(this);
于 2013-05-16T12:12:09.537 回答