一、什么是TextureView
TextureView是Android 4.0(API level 14)引入的一个新的控件,它可以在UI线程之外维护一个可渲染的surface,这允许我们可以将播放视频这样复杂的任务放到子线程中去执行,从而提高程序的性能。相较于SurfaceView,在TextureView中我们可以使用TextureView.setTransform()方法来对视频进行旋转、移动等的变换操作。但是使用TextureView也有一些限制,比如只有在硬件加速开启的情况下才有效,并且在TextureView上不能覆盖其他View。
二、使用TextureView播放视频
我们还需要使用MediaPlayer来进行视频的播放。在UI布局文件中,我们可以如下声明TextureView和一个Button控件:
<TextureView android:id="@+id/textureView" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/startBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始播放" />
在Java代码中,我们可以通过如下代码来初始化MediaPlayer和TextureView:
private MediaPlayer mediaPlayer; private TextureView textureView; private Button startBtn; private void initView() { textureView = findViewById(R.id.textureView); startBtn = findViewById(R.id.startBtn); // 初始化MediaPlayer并设置监听器 mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); // 开始播放视频 } }); // 设置TextureView的监听器 textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { mediaPlayer.setSurface(new Surface(surface)); try { mediaPlayer.setDataSource("video.mp4"); // 视频文件的路径 mediaPlayer.prepareAsync(); // 异步加载视频 } catch (IOException e) { e.printStackTrace(); } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { mediaPlayer.stop(); // 销毁TextureView时停止播放视频 mediaPlayer.release(); return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); startBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mediaPlayer.start(); // 点击按钮开始播放 } }); }
三、将TextureView与RecyclerView联动
在某些场景下,我们需要将TextureView与RecyclerView联动,实现类似于社交软件中的视频滚动浏览效果。
在我们的RecyclerView的Adapter中,需要为每个TextureView设置一个SurfaceTextureListener。在onBindViewHolder()方法中,我们可以创建一个新的MediaPlayer,为它设置数据源和回调监听,以及为TextureView设置SurfaceTextureListener。在onViewRecycled()方法中,我们需要释放MediaPlayer所占用的资源。
public class VideoAdapter extends RecyclerView.Adapter<VideoViewHolder> { private List<String> videoList; private MediaPlayer mediaPlayer; private TextureView.SurfaceTextureListener surfaceTextureListener; public VideoAdapter(List<String> videoList) { this.videoList = videoList; mediaPlayer = new MediaPlayer(); surfaceTextureListener = createSurfaceTextureListener(); } @NonNull @Override public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // 创建ViewHolder } @Override public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) { TextureView textureView = holder.getTextureView(); textureView.setSurfaceTextureListener(surfaceTextureListener); String videoUrl = videoList.get(position); try { mediaPlayer.setDataSource(videoUrl); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); } catch (IOException e) { e.printStackTrace(); } } @Override public void onViewRecycled(@NonNull VideoViewHolder holder) { TextureView textureView = holder.getTextureView(); mediaPlayer.stop(); mediaPlayer.reset(); textureView.setSurfaceTextureListener(null); } @Override public int getItemCount() { return videoList.size(); } private TextureView.SurfaceTextureListener createSurfaceTextureListener() { return new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { mediaPlayer.setSurface(new Surface(surface)); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }; } }