在TextureView上播放视频

58
在Android TextureView的文档中提到可以使用TextureView播放视频: 但我似乎找不到任何如何实现的示例。有人知道吗?
我需要使用textureView,因为我想要对视频进行动画处理。我希望播放.3gp/.mp4格式的视频,而不是来自相机的视频 :)
任何帮助都将不胜感激。
更新:
解决方案已发布为社区wiki答案。

@Edison 如果你真的看过源代码,你就会发现他们使用的是SurfaceView,而不是TextureView。问题要求使用TextureView。 - dcow
@Edison 提供了一种方法,让你知道如何在 TextureView 上创建新的 Surface,而 VideoView 并没有展示如何实现。 - dcow
@Edison,我正在尝试在列表视图中像Vine一样播放自动视频。我创建了一个自定义纹理视图,如果我将其作为活动中的单个视图使用,则可以正常工作。但是,如果我将其用作列表视图中的项目视图,则SurfaceTextureListener永远不会被调用,并且表面纹理始终为空。因此,没有视频被播放。您能否指点一下,我该如何解决这个问题。https://raw.github.com/ash-gupta/MyTestVideoView/master/MyTestVideoView - aNoviceGuy
@aNoviceGuy 视图可见吗?你的视图看起来很好(虽然我没有测试过)。自动播放只需要在 onPrepared 中调用 start 方法即可。 - Edison
1
@Zelleriation,你应该把你的解决方案发布为答案。它对我很有效。 - Jens Zalzala
显示剩余6条评论
3个回答

55

以下是如何操作的方法:
(这是问题作者在问题更新中发布的解决方案)

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

并且对其进行动画设计效果非常好。


1
在onCreate中,R.id.surface和extras是什么? - sudoExclaimationExclaimation
@PranoyC 显然是TextureView。 - Michael Fulton
1
不要忘记释放表面。 - Vas
1
感谢@JonDunn。请注意,@Override注释不是必需的。尽管如此,它有助于防止错误,因为如果使用@Override标记的方法未能正确覆盖其超类中的方法,则编译器会生成一个错误,这就是为什么它是一种良好的风格实践。我将编辑帖子。 - Carlos Robles
如果需要,实现以下内容: MediaPlayer.OnVideoSizeChangedListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener - Shaktisinh Jadeja
显示剩余2条评论

6

你真的很懒,不愿意帮忙。 - Rafael Lima

-2

你的 setContentView(mPreview); 需要在

之前调用。

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

在这里,你回答问题而不是纠正答案。使用评论来纠正它。 - MBH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接