Android: Play local video with mediaplayer

24,439

Solution 1

Just paste the file into res/raw/big_buck_bunny.mp4 instead vid folder and change your videoPath to:

video.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.big_buck_bunny);

Solution 2

Try this code....

1st make folder name raw in res directory, Copy your video in that folder and try out this code...

    video1=(VideoView)findViewById(R.id.myvideoview);
    video1.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/"+R.raw.YOUR_VIDEO_FILE_NAME));
    video1.setMediaController(new MediaController(this));
    video1.requestFocus();
    video1.start();

Solution 3

The problem may be in Android OS defect, which doesn't let you access normally files more than 1Mb size Load files bigger than 1M from assets folder

You probably need to split your video file into 1Mb sized parts. Then merge this parts into one file on sdcard and play it.

For example, I've splited big_buck_bunny.mp4 into 5 parts big_buck_bunny.mp4.part0, big_buck_bunny.mp4.part1 and so on. To merge them you can use this method

private void copyVideoFromAssets(String inFilePrefix, String outFileName) throws IOException {
    // Get list of files in assets and sort them
    final String[] assetsFiles = getAssets().list("");
    Arrays.sort(assetsFiles);

    // Open the empty file as the output stream
    final OutputStream output = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024 * 128];

    for (String file: assetsFiles) {
        if (file.startsWith(inFilePrefix)) {
            // Open part of file stored in assets as the input stream
            final InputStream input = getAssets().open(file);

            // Transfer bytes from the input file to the output file
            int length = input.read(buffer);
            while (length > 0) {
                output.write(buffer, 0, length);
                length = input.read(buffer);
            }
            input.close();
        }
    }

    // Close the streams
    output.flush();
    output.close();
}

public void PlayLocalVideo(View view)
    try {
        copyVideoFromAssets("big_buck_bunny.mp4.part", "/mnt/sdcard/big_buck_bunny.mp4");
    } catch (IOException e) {
        e.printStackTrace();
    }

    VideoView video=(VideoView) findViewById(R.id.video);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(video);
    video.setMediaController(mediaController);
    video.setKeepScreenOn(true);
    video.setVideoPath("/mnt/sdcard/big_buck_bunny.mp4");
    video.start();
    video.requestFocus();
}
Share:
24,439
user987723
Author by

user987723

Updated on July 25, 2022

Comments

  • user987723
    user987723 almost 2 years

    I am trying to play a video i have saved in my project. I have download this (an .mp4 test video) then created a folder within my project called vid on the root of the project. I have then used this code:

    public void PlayLocalVideo(View view)
        {
        VideoView video=(VideoView) findViewById(R.id.video1);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(video);
        video.setMediaController(mediaController);
        video.setKeepScreenOn(true);
        video.setVideoPath("android.resource://uk.co.SplashActivity/vid/big_buck_bunny.mp4");
        video.start();
        video.requestFocus();
    }
    

    my xml looks like this:

    <VideoView
        android:id="@+id/video1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    PlayLocalVideo is a method i have then used on the onclick event on a button. but when i press play nothing happens :(

  • user987723
    user987723 over 11 years
    no different. i should i be able to see a videoview even if the video was broken? i placed the button above the videoview in the xml but nothing appears to be below it.
  • Indrek Kõue
    Indrek Kõue over 8 years
    this problem doesn't occur when the data is already compressed (.mp4 for example)