Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 357
user987723 Avatar asked Feb 05 '13 14:02

user987723


People also ask

Does Android have a MediaPlayer?

VLC for Android VLC is one of the most popular Android media players. It is famous for its vast codec support and you can even watch DVD ISOs with this app along with online streams, most music codecs, and all kinds of other media files. MediaMonkey is a music player with the capacity to watch video.


3 Answers

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);
like image 167
lukjar Avatar answered Oct 21 '22 09:10

lukjar


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();
}
like image 34
vasart Avatar answered Oct 21 '22 08:10

vasart


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();
like image 33
Rohit Avatar answered Oct 21 '22 07:10

Rohit