Stopping video in TextureView

百般思念 提交于 2019-12-12 02:49:07

问题


I inserted textureview in my recyclerview,this guide I found in StackOverflow https://stackoverflow.com/a/17450763/6241501 but when I try to i,plement this in my recycler view I saw that video are all playing,but I need to all video be stopped when I starting my app.How can I do that? TextureView class:

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) {}
}

adapter:

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(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);
        }
    }
}

来源:https://stackoverflow.com/questions/36798946/stopping-video-in-textureview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!