简体   繁体   中英

Video can not be played by videoView Android

I am developing video player app. I want this app to be appear as option on selecting video from sdcard, album or any folder of the phone . When i select my video player to play video, it takes to app but video is not playing. I have given permission to read and write external storage in manifest.Below is my code :

 Intent in =getIntent();
 file_path = in.getData().getPath();
 System.out.println("file path from sdcard:"+file_path);
  videoView =(VideoView)findViewById(R.id.video);

   MediaController mediaController= new MediaController(this);
    mediaController.setAnchorView(videoView);        
     Uri uri=Uri.parse(file_path);        
     videoView.setVideoURI(uri);        
     videoView.requestFocus();
         videoView.start();

ERROR :

 10-19 10:39:40.917: I/System.out(20430): file path from sdcard:/external/video/media/24363
 10-19 10:39:40.987: E/MediaPlayer(20430): Uri is  <URL suppressed>
 10-19 10:39:40.997: E/MediaPlayer(20430): error (1, -2147483648)
 10-19 10:39:41.017: E/MediaPlayer(20430): Error (1,-2147483648)
 10-19 10:39:41.017: D/VideoView(20430): Error: 1,-2147483648

EDIT: testing phone : Android 4.1 with 32Gb inbuilt memory and does not have Sdcard.

Answer for this is here

 public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Video.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

I had this same problem, went down many different paths to fix it, and what finally ended up working was the following code block:

/* inside onCreate */
VideoView videoView = (VideoView)findViewById(R.id.loginVideoBackground);
videoView.setVideoURI(Uri.parse("android.resource://" + this.getPackageName() + "/raw/lights"));
videoView.start();

I had previously tried:

  • Extending SurfaceView
  • using the answer given by @brinda above (which was resulting in a NullPointerException )
  • and various other permutations of StackOverflow answers

....sometimes simpler is better

/external/video/media/24363 is not correct, please check. local path must be visit..eg /sdcard/video/media/..

videoView.setVideoPath("http://www.ebookfrenzy.com/android_book/movie.mp4");        MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();

To be honest, I've experienced similar problems in the past and for each account, what I've ultimately found to be the best solution was to extend SurfaceView, directly. There is a surprisingly small amount of code to write and can easily (relatively speaking) be tailored efficiently to your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM