我有recyler列表視圖與TextureView和我設置的URI(這是從原料夾視頻)到紋理視圖和適配器此我後實現TextureView.SurfaceTextureListener inteface我讓下面的方法與textureview recyler列表視圖是緩慢滾動時
- onSurfaceTextureAvailable
- onSurfaceTextureSizeChanged
- onSurfaceTextureDestroyed
在onSurfaceTextureAvailable我正在設置我的MediaPlayer並啓動視頻。但是,當滾動列表時,問題就出現了。(並且加載速度很慢)。
以下是我的代碼。
在此先感謝。
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements TextureView.SurfaceTextureListener {
private List<Model> list = new ArrayList<>();
private Context context;
private MediaPlayer mediaPlayer =null;
Uri uri;
public MyAdapter(Context context, List<Model> l) {
this.list = l;
this.context = context;
uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.malhari1);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_layout, parent, false);
return new MyViewHolder(itemView);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.setIsRecyclable(false);
holder.name.setText(list.get(position).getName());
holder.textureView.setSurfaceTextureListener(this);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
System.out.println("onSurfaceTextureAvailable");
setMediaPlayer(surface, uri);
}
private void setMediaPlayer(SurfaceTexture surface, Uri uri) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(new Surface(surface));
try {
mediaPlayer.setDataSource(context,uri);
mediaPlayer.prepare();
mediaPlayer.start();
// mediaPlayerTemp = mediaPlayer;
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp != null) {
mp.start();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
System.out.println("onSurfaceTextureDestroyed");
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView name;
TextureView textureView;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.tvName);
textureView = (TextureView)
itemView.findViewById(R.id.textureView);
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Model> list = new ArrayList<>();
RecyclerView recyclerView;
MyAdapter adapter;
RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new MyAdapter(MainActivity.this,list);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
setList();
}
private void setList() {
for (int i = 0; i < 15; i++) {
Model model = new Model();
model.setName("Name : "+i);
list.add(model);
}
adapter.notifyDataSetChanged();
}
}
是啊vivek我想播放列表中的視頻.. – Harshad07
然後,看看上面的庫以及ExoPlayer在Android .. –
ExoPlayer是Android的MediaPlayer的替代品。 –