0

I am developing and testing a simple video player example in Android. Code is given below:

VideoView vid = (VideoView) findViewById(R.id.videoView1);

vid.setVideoPath("/sdcard/myvideo.mp4");
vid.setMediaController(new MediaController(this));
vid.start();
vid.requestFocus();

I am using HTC ONE V. When I run this on my phone I am getting a NullPointerException on the line where I set the path of video (i.e. setVideoPath("/sdcard/myvideo.mp4")). I think there is problem in accessing the memory card.

So can anyone kindly tell that is the way to access memory card on HTC ONE V different from other phones? And if yes then how to access the memory card HTC ONE V.

swdeveloper
  • 908
  • 1
  • 11
  • 33
  • Post your Logcat error . @ which line it shows null pointer exception – Chirag Aug 24 '12 at 11:58
  • FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{my.test.media/my.test.media.MyVideoPlayer}: java.lang.NullPointerException . . . Caused by: java.lang.NullPointerException at my.test.media.MyVideoPlayer.onCreate(MyVideoPlayer.java:40) at android.app.Activity.performCreate(Activity.java:4538) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158) ... 11 more – swdeveloper Aug 24 '12 at 12:02
  • Post MyVideoPlayer.java:40 Line number code . Post your entire activity code and xml file – Chirag Aug 24 '12 at 12:03
  • Problem is solved. Actually I was trying to play video on phone while it was connected to the PC (even when I played directly from card while phone was connected, an error came saying that phone was being used as a USB), when I disconnected it, then it started palying. Thanks to all of you – swdeveloper Aug 24 '12 at 12:49

2 Answers2

3

If you are getting NullpointerException on the line

vid.setVideoPath("/sdcard/myvideo.mp4");

then that indicates vid is null.

So make sure setContentView is done in Oncreate before you call findviewbyid and make sure R.id.videoView1 is present in the Layout that you have set.

As others have pointed do not hard code sdcard path.

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Thank, I was missing setContentView and now I wrote that before this code, so Exception is not coming, but now it gives error on mobile phone says "Cannot play video. Sorry this video cannot be played" – swdeveloper Aug 24 '12 at 12:10
  • are you using /sdcard/myvideo.mp4 or using getExternalstoragedirectory. For now try hard coding the exact path – nandeesh Aug 24 '12 at 12:14
  • I have tried both, /sdcard/myvideo.mp4 and Environment.getExternalStorageDirectory().toString()+"/myvideo.mp4", but it gives same error – swdeveloper Aug 24 '12 at 12:16
  • but when I play that video directly from card, then it is played – swdeveloper Aug 24 '12 at 12:45
  • then you should check the logs. See if it showing anything of importance – nandeesh Aug 24 '12 at 12:47
2

You should use

vid.setVideoPath(Environment.getExternalStorageDirectory().toString()+filename);

instead of

vid.setVideoPath("/sdcard/myvideo.mp4");
Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38