播放本地视频Android

时间:2017-04-24 06:18:15

标签: android android-recyclerview android-videoview

如何将本地externalStorageDirectory(不是从互联网链接)的所有视频路径嵌入到RecyclerView中,并在单击playButton时播放它。 我试过但没有展示一些东西和玩。谢谢大家。 这是我的代码:



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

    @Override
    public void onBindViewHolder(VideoViewHolder holder, int position) {
        holder.videoUrl = videos.get(position);
        holder.imageLoaderProgressBar.setVisibility(View.INVISIBLE);
        holder.videoImageView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onViewRecycled(VideoViewHolder holder) {
        if (holder == currentVideoViewHolder) {
            holder.stopVideo();
        }
        holder.videoView.stopPlayback();
        super.onViewRecycled(holder);

    }

    @Override
    public int getItemCount() {
        return videos.size();
    }
    VideoViewHolder currentVideoViewHolder;
    public void onScrolled(RecyclerView recyclerView) {
        if (currentVideoViewHolder != null) {
            currentVideoViewHolder.onScrolled(recyclerView);
        }
    }

    

    class VideoViewHolder extends RecyclerView.ViewHolder {
       //find R.id.controls for each controls. : button, progressbar, videoview.
        String videopath;

        public String getVideoUrl() {
            return videopath;
        }

        public VideoViewHolder(View view) {
            super(view);
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(final MediaPlayer mp) {
                    int width = mp.getVideoWidth();
                    int height = mp.getVideoHeight();
                    videoView.setIsPrepared(true);
                    UIUtils.resizeView(videoView, UIUtils.getScreenWidth(getActivity()), UIUtils.getScreenWidth(getActivity()) * height / width);
                    if (currentVideoViewHolder == VideoViewHolder.this) {
                        videoImageView.setVisibility(View.GONE);
                        imageLoaderProgressBar.setVisibility(View.INVISIBLE);
                        videoView.setVisibility(View.VISIBLE);
                        videoView.seekTo(0);
                        videoView.start();
                    }
                }
            });
            videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    return false;
                }
            });
            
            videoPlayImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentVideoViewHolder != null ) {
                        currentVideoViewHolder.videoView.pause();

                        if (currentVideoViewHolder.videoView.getVisibility() == View.VISIBLE)                           currentVideoViewHolder.videoView.setVisibility(View.INVISIBLE);

                        currentVideoViewHolder = null;
                    }
                    currentVideoViewHolder = VideoViewHolder.this;
                    if (!getVideoUrl().equals(videoView.getVideoPath())) {
                        videoView.setIsPrepared(false);
                        videoView.setVideoPath(getVideoUrl());
                        videoView.requestFocus();
                    } else {
                        if (videoView.isPrepared()) {
                            imageLoaderProgressBar.setVisibility(View.INVISIBLE);
                        } else {
                            imageLoaderProgressBar.setVisibility(View.VISIBLE);
                        }
                        videoView.requestFocus();
                        videoView.seekTo(0);
                        videoView.start();
                    }
                }
            });

        }

        public void stopVideo() {
            videoView.pause();
        }

        public void onScrolled(RecyclerView recyclerView) {
            if (isViewNotVisible(videoPlayImageButton, recyclerView)) {
                stopVideo();
            }
        }

        public boolean isViewNotVisible(View view, RecyclerView recyclerView) {
            Rect scrollBounds = new Rect();
            recyclerView.getHitRect(scrollBounds);
            return view.getVisibility() == View.VISIBLE;
        }
    }




1 个答案:

答案 0 :(得分:0)

以下是从android的内部存储器播放mp4视频的完整意图代码:

File mydir = activity.getDir("Videos", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, headingsList.get(mPosition));
fileWithinMyDir.setReadable(true, false);
String videoResource = fileWithinMyDir.getPath();
Uri intentUri = Uri.fromFile(new File(videoResource));

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);

我希望它会对你有所帮助。