On my app I wanted to include an introductory video loaded directly ino the app instead of pulled form an internet source.

Once the app was created I used a resource called handbrake which compressed my video into just a few megs.

 

The I applied the following code…

 

My main activity has two class object.

FrameLayout frameLayout;
<pre>ImageView im;</pre>

 

My method calls lead to a splashPlayer() which loads the video. The sequence is @Override:onCreate() -> createStart()this loads all my startup stuff -> splashPlayer() this loads the video

The frame layout below is linked to my xml layout as usual. Here  we set up our video. Then call another method addPlayButton()

</pre>
<pre>public void splashPlayer()
{

    try {</pre>
<pre>final VideoView videoHolder = new VideoView(this);

frameLayout.addView(videoHolder);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound);
videoHolder.setVideoURI(video);</pre>
<pre>addPlayButton(true);</pre>
<pre>

The addplay button shows a play button over your black square so people understand its a movie.

</pre>
<pre>public void addPlayButton(boolean show)
{

    if (show){
        im = new ImageView(this);
        im.setImageResource(R.drawable.playicon1);
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = 50;
        params.height = 50;

        frameLayout.addView(im, params);
    }else
    {
        frameLayout.removeView(im);
    }


}</pre>
<pre>

The where back in splashplayer and setting our controls.

</pre>
<pre>
        videoHolder.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch ( motionEvent.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        Log.i(TAG, "onTouch: touched");
                        if (videoHolder.isPlaying()) {
                            Log.i(TAG, "onTouch: 1");
                            videoHolder.pause();
                            addPlayButton(true);

                        } else {
                            Log.i(TAG, "onTouch: 2");
                            videoHolder.start();
                            addPlayButton(false);
                        }
                        break;

                }

                return true;
            }
        });

    }catch (Exception e){
        Log.i(TAG, "splashPlayer: try/catch fail");
    }

}</pre>
<pre>

Total code as follows

</pre>
<pre>public void splashPlayer()
{

    try {

        Log.i(TAG, "splashPlayer: trying...");

        final VideoView videoHolder = new VideoView(this);

        frameLayout.addView(videoHolder);
        Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound);
        videoHolder.setVideoURI(video);

        addPlayButton(true);

        //videoholder.setoncompletelistener
        videoHolder.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch ( motionEvent.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        Log.i(TAG, "onTouch: touched");
                        if (videoHolder.isPlaying()) {
                            Log.i(TAG, "onTouch: 1");
                            videoHolder.pause();
                            addPlayButton(true);

                        } else {
                            Log.i(TAG, "onTouch: 2");
                            videoHolder.start();
                            addPlayButton(false);
                        }
                        break;

                }

                return true;
            }
        });

    }catch (Exception e){
        Log.i(TAG, "splashPlayer: try/catch fail");
    }

}</pre>
<pre>

The post Adding a splash video player on Android appeared first on SignalHillTechnology.

Powered by WPeMatico