滚动时,带有textureview的recyler listview很慢

时间:2017-04-17 09:11:49

标签: android android-recyclerview media-player surfaceview

我有 recyler listview TextureView ,我将URI(来自原始文件夹的视频)设置为纹理视图,而Adapter实现 TextureView.SurfaceTextureListener < / strong>接下来我得到以下方法

  1. onSurfaceTextureAvailable
  2. onSurfaceTextureSizeChanged
  3. onSurfaceTextureDestroyed
  4. 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();
    
    }
      }
    

2 个答案:

答案 0 :(得分:0)

我认为您正在尝试在循环播放器视图中播放视频,请查看以下库: 1)Toro

2)VideoPlayerManager

3)ExoPlayer

答案 1 :(得分:0)

首先从网址下载视频,然后使用自定义播放器播放视频,从而实现这一目标。我是这样做的:

1)获取所有网址

2)开始从本地存储中的网址下载视频(在队列中)并在首选项中保留一个标记(视频已经下载或未下载)

3)将网址分配给适配器,其中初始化处理视频播放的视频播放器控制器的对象

4)设置addOnScrollListener以检查当前可见的位置/视频并检查视频是否已经下载,如果是,则播放。