With this method below, I play the videos on the list.
videoItem.setVideoPath(filePaths.get(position));
videoItem.setClickable(true);
videoItem.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
player.playVideo(videoItem);
return true;
}
});
The videoItem
variable is an instance of the VideoView
class, and the player
variable and its method is from the custom VideoPlayer
class.
Here is the VideoPlayer
class.
public class VideoPlayer {
private Context context;
private Uri uri;
private boolean isVideoPlaying = false;
public VideoPlayer(Context context) {
this.context = context;
}
public void getVideoUri(VideoView video) {
try {
Field field = VideoView.class.getDeclaredField("uri");
field.setAccessible(true);
uri = (Uri) field.get(video);
} catch (Exception e) {
Toast.makeText(context, "Exception caught: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void playVideo(VideoView video) {
getVideoUri(video);
MediaController mediaController = new MediaController(context);
mediaController.setAnchorView(video);
mediaController.setMediaPlayer(video);
video.setMediaController(mediaController);
video.setVideoURI(uri);
video.start();
isVideoPlaying = true;
}
}
When I tap the video views, it starts to play. That's not a problem. But at the same time the toast message that is thrown when an exception is caught is also shown. Why is the exception happening in the code?