Android: Play Local Video Using Media Player

I am trying to play a video that I have saved in my project. I downloaded this (.mp4 test video) then created a folder in my project called vid in the project root. Then I 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 the method I used in the onclick event on the button. but when i click the button nothing happens :(

+3


source to share


3 answers


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



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

      

+8


source


The problem may be a defect in the Android OS that prevents you from accessing files larger than 1 MB normally Download files larger than 1 M from the file folder

You probably need to split the video file into 1MB chunks. Then combine these parts into one file on SD card and play it back.



For example, I divided it big_buck_bunny.mp4

into 5 parts big_buck_bunny.mp4.part0

, big_buck_bunny.mp4.part1

and so on. To combine 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();
}

      

+1


source


Try this code ....

1st make folder name raw in res directory, Copy video to that folder and try 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();

      

0


source







All Articles