最近因项目需求,需要在滚动列表中播放视频。查了半天资料,发现其实现没有想象中的那么难。
原理
众所周知,由于SurfaceView没有UI同步缓冲区,就导致了在列表滚动的时候,正在播放的视频跟不上滚动的步伐;而在SDK 15的时候,谷歌官方提供了新的具有UI同步缓存的视频播放api TextureView。
本文真是基于该新Api实现。
代码实现
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:focusable="true"
>
<TextureView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="@+id/pb"