在TextureView中停止视频

时间:2016-04-22 16:26:50

标签: android android-recyclerview textureview

我在recyclerview中插入了textureview,我在StackOverflow中找到了这个指南 https://stackoverflow.com/a/17450763/6241501但是当我尝试着我时,在我的回收者视图中看到了这一点 视频都在播放,但我需要在启动应用时停止所有视频。我该怎么做? 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(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 个答案:

没有答案