user987723
user987723

Reputation: 965

Android: Play local video with mediaplayer

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 :(

Upvotes: 11

Views: 26678

Answers (3)

Rohit
Rohit

Reputation: 2681

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();

Upvotes: 1

vasart
vasart

Reputation: 6702

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();
}

Upvotes: 1

lukjar
lukjar

Reputation: 7425

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);

Upvotes: 13

Related Questions