views:

842

answers:

4

I am trying to play a video in my app. It has to be embedded.

I went through the "Play Video Files in Android" thread.

I am able to play my video using VideoView as mentioned in this discussion. But there are a few problems.

  1. I need full screen video, how do I stretch VideoView to full screen? Will that stretch the video too?

  2. I don't need the default play/forward/stop buttons at all. Basically I need continuous loop playing of the video.

I tried the MediaPlayer class as in here, but it never worked. What should the string format look like if I have my video file in res/raw directory? I really don't want the video file played from sdcard. How can it be bundled along with the app?

If any of these two approaches work, I will be better off.

This is my code:

videoHolder = new VideoView(this); 
// videoHolder = (VideoView)findViewById(R.id.videoview); 
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
videoHolder.setLayoutParams(params); 
videoHolder.setMediaController(new MediaController(this)); 
setContentView(videoHolder); 
// 
// 
//// I tested and found that it works fine for .wmv, .3gp and .mp4 
//// videoHolder.setVideoURI(Uri.parse("file:///sdcard/video.3gp")); 
videoHolder.setVideoURI(Uri.parse("res/raw/demo.3gp")); 
videoHolder.requestFocus(); 
videoHolder.start(); 

Strangely the commented url works (the one with sdcard). The other one does not work. I have tried many combinations from "file:// res/raw/demo.3gp" to only "demo".

What would be the correct string to access the file?

A: 

I have the same problem could anyone please answer ??

Someone
+1  A: 

I have solved the problem:

1. To remove the continue/pause buttons remove the media controller. For the looping issue put OnCompletionListener so that when the video reaches the end, it starts again:

videoView.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            videoView.start();
        }
        });

2. To resize the video, override method onMeasure() in VideoView like this:

public class MyVideoView extends VideoView {

        public MyVideoView(Context context) {
            super(context);
        }

        @Override
        protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
        {
             setMeasuredDimension(480,800);
        }

    }
Someone
A: 

Hello
To play raw video you need to copy it to Sdcard like this :

    File f=new File(Environment.getExternalStorageDirectory(), FICHIER_BLOW);
    try {
        if (f.exists()){
            MyVideoView videoHolder = new MyVideoView(this); 
            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
            videoHolder.setLayoutParams(params);  
            setContentView(videoHolder);

            video =Uri.fromFile(f);
            videoHolder.setVideoURI(video);
            videoHolder.start();
        }
        else if (f.createNewFile()){
            BufferedOutputStream bufEcrivain = new BufferedOutputStream((new FileOutputStream(f)));
            BufferedInputStream VideoReader = new BufferedInputStream(getResources().openRawResource(R.raw.blow));
            byte[] buff = new byte[32 * 1024];
            int len;
            while( (len = VideoReader.read(buff)) > 0 ){
                bufEcrivain.write(buff,0,len);
            }
            bufEcrivain.flush();
            bufEcrivain.close();

            MyVideoView videoHolder = new MyVideoView(this); 
            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
            videoHolder.setLayoutParams(params);  
            setContentView(videoHolder);

            video =Uri.fromFile(f);
            videoHolder.setVideoURI(video);
            videoHolder.start();
        }
        else{
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
NSchubhan
A: 

I know this thread is a few weeks old, but you don't have to copy the video file to the SD card just to play it. Use the following but insert your package name instead of "com.yourcompany.yourproject":

videoView.setVideoURI(Uri.parse("android.resource://com.yourcompay.yourproject/" + R.raw.yourvideoresource));

Brian