0

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?

Mike
  • 4,550
  • 4
  • 33
  • 47
MarshallLee
  • 1,290
  • 4
  • 23
  • 42
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 08 '15 at 18:07
  • 3
    Can you add the Exception Stack Trace in your question ? We can't help you much without it =) – Kapcash Dec 08 '15 at 18:11
  • @Kapcash It says `Exception caught: java.lang.NoSuchFieldException: uri` – MarshallLee Dec 08 '15 at 18:18

1 Answers1

0

your error says "uri" is not a field of VideoView class, check VideoView class and correct the name of the field you are trying to access.

AAnkit
  • 27,299
  • 12
  • 60
  • 71