问题描述
在 Android TextureView 的文档中,它说你可以使用一个 TextureView 来播放视频:但我似乎无法找到如何做到这一点的任何例子.有人知道吗?
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?
我需要使用 textureView
因为我想为视频制作动画.我想播放 .3gp/.mp4 格式的视频,而不是来自相机的视频 :)
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..
更新:
解决方案作为社区维基答案发布
Solution is posted as a community wiki answer
推荐答案
您可以这样做:(问题作者的解决方案,他在问题中作为更新发布)
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();
}
}
动画效果非常好.
这篇关于在 TextureView 上播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!