androidandroid-videoviewassets

how to play video from sub folder of assets folder in android


I am trying to play video from sub-folder's of Assets folder.

I have created one folder xyz in assets folder.

this is my code:

String uriPath = "android.resource://"+ getPackageName() + "/assets/xyz/"+"age.mp4";
    videoView.setVideoURI(Uri.parse(uri));
    // videoView.setMediaController(new MediaController(this));
    videoView.setMediaController(null);
    videoView.requestFocus();
    videoView.start();

But it is giving me error E/MediaPlayer(11068): error (1, -2147483648)

also tried this code:

public void Play(String fileName) {
        AssetFileDescriptor descriptor = null;
        try {

            String[] array = getApplicationContext().getAssets().list("long_a");
            Toast.makeText(MainActivity.this,
                    "file is exists  " + array.length, Toast.LENGTH_LONG)
                    .show();

            descriptor = getApplicationContext().getAssets().openFd(
                    "xyz/say.mp4");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long start = descriptor.getStartOffset();
        long end = descriptor.getLength();
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start,
                    end);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mediaPlayer.start();
    }

Thanks for help in advance :)


Solution

  • Replace this:

    String uriPath = "android.resource://"+ getPackageName() + "/assets/xyz/"+"age.mp4";
       
    

    with this:

    AssetFileDescriptor afd = getAssets().openFd("xyz/age.mp4");
    player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
    

    and make sure you get ID of video view from layout like:

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

    Create MediaController like:

    MediaController mediaController = new MediaController(this);  
    mediaController.setAnchorView(videoView);  
    

    Set MediaController like:

    videoView.setMediaController(mediaController);