如何將本地externalStorageDirectory(不是從互聯網鏈接)的所有視頻路徑嵌入到RecyclerView中,並在單擊playButton時播放它。 我嘗試過,但沒有顯示一些東西並玩。謝謝大家。 這裏是我的代碼:播放本地視頻Android
@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;
}
}
讓我們看看您的代碼 –
@SurajMakhija:我發佈了關於AdapterRecyclerView的代碼。 –