2017-04-24 215 views
0

如何將本地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; 
 
     } 
 
    }

+0

讓我們看看您的代碼 –

+0

@SurajMakhija:我發佈了關於AdapterRecyclerView的代碼。 –

回答

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

我希望它會幫助你。

+1

非常感謝。但是,如果我想列出所有視頻在Android的外部存儲,我如何將它導入到RecyclerView和滾動,控制暫停的狀態,通過使用TextureView停止或我必須使用別的東西? –

+0

你可以按照這個鏈接:http://stackoverflow.com/questions/30978030/playing-video-using-textureview-in-recyclerview –

+0

哦,男人。非常感謝你。祝你最好。 –