【问题标题】:Playing video on TextureView在 TextureView 上播放视频
【发布时间】:2012-05-30 23:30:45
【问题描述】:

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

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

任何帮助将不胜感激..

更新:

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

【问题讨论】:

  • 你在activity上开启了硬件acel吗?
  • 我只是在 Manifest 中设置了 :)
  • 您知道为什么 onSurfaceTextureAvailable 从未调用过吗?
  • 还有。您可以随时查看netmite.com/android/mydroid/frameworks/base/core/java/android/…,了解 Google 是如何处理常规 VideoView 的。
  • @Zelleriation 您应该发布您的解决方案作为答案。对我来说效果很好。

标签: java android video video-streaming


【解决方案1】:

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

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

动画效果非常好。

【讨论】:

  • 使用 TextureView 代替 SurfaceView 解决了视频播放时的闪烁问题。谢谢!
  • onCreate 中的 R.id.surface & extras 是什么?
  • @PranoyC TextureView,显然。
  • 别忘了释放表面
  • 谢谢@JonDunn。请注意,@Override 注释不是必需的。尽管如此,它有助于防止错误,因为如果标有 @Override 的方法未能正确覆盖其超类之一中的方法,编译器会生成错误,这就是为什么它是一种良好的风格实践。我会编辑帖子
【解决方案2】:

我遇到了同样的问题,并通过TextureView 解决了它。我发现 setScaleXsetScaleY 非常有用,如果这对任何人都有帮助的话。 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);

应该这样做:)

【讨论】:

  • 你真是懒得帮忙
【解决方案3】:

您的setContentView(mPreview); 需要在

之前调用
mPreview = (TextureView) findViewById(R.id.surface);
mPreview.setSurfaceTextureListener(this);

【讨论】:

  • 在这里你回答问题不是为了纠正答案。使用评论来纠正它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2015-10-11
  • 2014-12-21
  • 2015-10-12
  • 2016-01-22
  • 2015-12-08
相关资源
最近更新 更多