Adding a video to an app is really easy!

More or less just a scratchpad of notes for my own future reference, but adding a video to an android app is incredibly easy, as I learned today in my continued studies in the Android Developer Course.

You can download the app here:

http://www.mediafire.com/file/5xfambg19wr0akl/MyVideoApp.apk

If, like myself, you need a small sample video, you can get them in every size, file type, and resolution here:

http://www.sample-videos.com/

And below is the magic behind the curtain.

MainActivity.java:

package com.alaskalinuxuser.videoview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
// Import the widget for video views.
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the video view by id so we can use it.
        VideoView myVideo = (VideoView) findViewById(R.id.videoView);

        // To set the path to the video. Sample.mp4 is in the "raw" folder.
        // You can use http://online.path.to.your.video also.
        myVideo.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.sample);

        // To create the media controller (play, pause, etc.)
        MediaController myController = new MediaController(this);

        // To link the media controller to the video view.
        myController.setAnchorView(myVideo);

        // To link the video view to the media controller.
        myVideo.setMediaController(myController);

        // To autoplay on opening.
        myVideo.start();

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.alaskalinuxuser.videoview.MainActivity">


    <VideoView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/videoView" />

</RelativeLayout>

Pretty simple, but it was great to go through such a well thought out course on how to actually do that and how to use it! I thought that I would have to manually create each button and add the controls myself, it was neat to see a two line command for all the controls!

Linux – keep it simple.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s