所有视频都通过TextureView在Recycler \ view中播放

时间:2016-04-23 04:37:59

标签: android android-recyclerview textureview

我需要在我的RecyclerView中暂停所有视频。我正在使用TextureView播放视频现在,我开始我的应用程序所有视频播放和RecyclerView有很大的滞后。我需要看视频,但不能播放。播放视频点击查看item.How播放视频当我点击了RecyclerView我知道,但现在我必须在stoped状态下实现所有这些视频。但是当我在TextureView类中更改一些代码时,即onSurfaceTextureAvailable方法。我将方法start()更改为方法stop()(我试过暂停())我没看到视频,所有的TextureView都是空的。 我的TextureView:

public class MyVideoView extends TextureView implements TextureView.SurfaceTextureListener {
    private MediaPlayer mMediaPlayer;
    private Uri mSource;
    private MediaPlayer.OnCompletionListener mCompletionListener;
    private boolean isLooping = false;


    public MyVideoView(Context context) {
        this(context, null, 0);
    }

    public MyVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSurfaceTextureListener(this);
    }

    public void setSource(Uri source) {
        mSource = source;
    }

    public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
        mCompletionListener = listener;
    }

    public void setLooping(boolean looping) {
        isLooping = looping;
    }

    @Override
    protected void onDetachedFromWindow() {
        // release resources on detach
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        super.onDetachedFromWindow();
    }

    /*
     * TextureView.SurfaceTextureListener
     */
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        Surface surface = new Surface(surfaceTexture);
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnCompletionListener(mCompletionListener);
//            mMediaPlayer.setOnBufferingUpdateListener((MediaPlayer.OnBufferingUpdateListener) this);
//            mMediaPlayer.setOnErrorListener((MediaPlayer.OnErrorListener) this);
            mMediaPlayer.setLooping(isLooping);
            mMediaPlayer.setDataSource(getContext(), mSource);
            mMediaPlayer.setSurface(surface);
           mMediaPlayer.prepare();

          mMediaPlayer.start();

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            mMediaPlayer.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        surface.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
}

适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.VideoViewHolder> {
    List<Video> call;
    RecyclerViewAdapter(List<Video> call){
        this.call = call;

    }
    @Override
    public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row,parent,false);
        VideoViewHolder videoViewHolder = new VideoViewHolder(v);
        return videoViewHolder;
    }

    @Override
    public void onBindViewHolder(VideoViewHolder holder, int position) {
        String video_url = call.get(position).getClipUrl();
        Log.d("Recycler Adapter", "videourl " + video_url);
//holder.videoView.setVideoURI(Uri.parse(video_url));
        Log.d("Recycler Adapter", "title " + call.get(position).getTitle());
        Log.d("Recycler Adapter", "video_like " + call.get(position).getScore());
holder.my_video_view.setSource(Uri.parse(video_url));
        holder.video_name.setText(call.get(position).getTitle());
        holder.video_like.setText("Likes :"+String.valueOf(call.get(position).getScore()));

    }

    @Override
    public int getItemCount() {
        return call.size();
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        VideoView videoView;
        TextView video_name;
        TextView video_like;
        MyVideoView my_video_view;
        public VideoViewHolder(View itemView) {
            super(itemView);
            my_video_view = (MyVideoView) itemView.findViewById(R.id.my_video_view);
            cardView = (CardView) itemView.findViewById(R.id.card_view);
           // videoView = (VideoView)itemView.findViewById(R.id.videoview);
            video_name = (TextView) itemView.findViewById(R.id.Videoname_textView);
            video_like = (TextView) itemView.findViewById(R.id.like_textview);
        }
    }
}

0 个答案:

没有答案