Android Development – VideoView Working Code

After searching for hours trying to find a decent tutorial for videoview I finally found one I could use to get this working, so now I will explain it simply, so simply that I can come back here for it when needed and still be able to do it.

Here are the basics, will need to add things to all these files:

  1. MainActivity.java – main class file
  2. activity_main.xml – layout file
  3. VideoViewActivity.java – new class file for videoview
  4. videoview_main.xml – new layout file for videoview
  5. strings.xml – just the strings
  6. AndroidManifest.xml – manifest file

so lets begin.

This is the entire MainActivity.java file required, it has been altered to have 2 buttons and 2 videos:

package com.androidbegin.videostreamtutorial;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button button;
Button buttontwo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);

// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);

// Capture button clicks
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
VideoViewActivity.class);
startActivity(myIntent);
}
});

// Locate the button in activity_main.xml THIS IS A COPY OF THE BUTTON
button = (Button) findViewById(R.id.MyButtonTwo);

// Capture button clicks
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
VideoViewActivityTwo.class);
startActivity(myIntent);
}
});

}

}

 

The important bits out of this are:

Button button;

This is declaring a new variable which is the button which will be pressed to play the video, for more buttons declare more of them.

Then this bit:

     // Get the layout from video_main.xml
        setContentView(R.layout.activity_main);
 
        // Locate the button in activity_main.xml
        button = (Button) findViewById(R.id.MyButton);

//the id of the button to be clicked
 
        // Capture button clicks
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
 
                // Start NewActivity.class
                Intent myIntent = new Intent(MainActivity.this,
                        VideoViewActivity.class);
                startActivity(myIntent);
            }
        }

 

The MainActivity.this is referencing the main java class and shouldnt be changed.

The VideoViewActivity is referencing the videoview.class. I have been changing this for each video as the url is hardcoded in but sure theres a way to pass the URL in the button code.

Now in  activity_main.xml we need to create a button that will trigger the video:

<Button
        android:id=”@+id/MyButton
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_centerHorizontal=”true”
        android:layout_centerVertical=”true”
        android:text=”@string/button” />

It needs to have the id that is mentioned in the MainActivity.java, the rest is just formatting

Now we need to create VideoViewActivity.java

In this is the main code for videoview:

package com.androidbegin.videostreamtutorial;
 
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
 
public class VideoViewActivity extends Activity {
 
    // Declare variables
    ProgressDialog pDialog;
    VideoView videoview;
 
    // Insert your Video URL this is for hosted video
    //String VideoURL = “http://www.root.medlington.co.uk/tutorial/qpstest.mp4”;
 
    // try my video coding – this is an internal android resource
    String VideoURL = “android.resource://com.androidbegin.videostreamtutorial/raw/ski”;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the layout from video_main.xml
        setContentView(R.layout.videoview_main);
        // Find your VideoView in your video_main.xml layout
        videoview = (VideoView) findViewById(R.id.VideoView);
        // Execute StreamVideo AsyncTask
 
        // Create a progressbar
        pDialog = new ProgressDialog(VideoViewActivity.this);
        // Set progressbar title
        pDialog.setTitle(“Android Video Streaming Tutorial”);
        // Set progressbar message
        pDialog.setMessage(“Buffering…”);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();
 
        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    VideoViewActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);
 
        } catch (Exception e) {
            Log.e(“Error”, e.getMessage());
            e.printStackTrace();
        }
 
        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();
            }
        });
 
    }
 
}

Now create the layout file to display the videoview videoview_main.xml:

Its just a standard layout file with the videoview comp[onant added:

<VideoView
        android:id=”@+id/VideoView”
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
        android:layout_centerHorizontal=”true”
        android:layout_centerVertical=”true” />

 

then simply text to the buttons and such in strings.xml:

 <string name=”app_name”>Video Stream Tutorial</string>
    <string name=”hello_world”>Hello world!</string>
    <string name=”menu_settings”>Settings</string>
    <string name=”action_settings”>Settings</string>
    <string name=”button”>SKI Vid</string>
    <string name=”buttontwo”>QuickPlayTest</string>

 

Lastly we need to add a few lines to the manifest file AndroidManifest.xml

  <uses-permission android:name=”android.permission.INTERNET” >
    </uses-permission>
// this goes under <uses-sdk>

 

Then add a new activity for each instance of videoview:

        <activity android:name=”.VideoViewActivity”            android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
             />
 //optional line for full screen mode

Open new layout on click Android Development

First we need a button and a second layout.xml page

button in fragment_main.xml:

<Button
        android:id=”@+id/button1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignLeft=”@+id/textView1″
        android:layout_below=”@+id/textView1″
        android:text=”Button” />

First need to add the on-click event to the button so:
<Button
        android:id=”@+id/button1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignLeft=”@+id/textView1″
        android:layout_below=”@+id/textView1″
        android:text=”Button”
    android:onClick=”sendMessage”
 />

 

Now I need to extend the main class to include a sendMessage function.

 

So in:

src > project > mainActivity.java > Main (class)

add:

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
}

Now I need to add an intent which is the apps intent to do something and is used to bind 2 things together, the intent goea inside the function I just added.

Intent intent = new Intent(this, DisplayMessageActivity.class);

DisplayMessageActivity.class is the class which using the intent will start.

*Will need to import the intent class to use this so perss ctrl+shift+o to auto import or add this line:

import android.content.Intent;

Need to add this line into the sendMessage dunction to trigger the intent:

startActivity(intent);

 

Now we need a new activity (another page to go to)

To create a new activity using Eclipse:

  1. Click New in the toolbar.
  2. In the window that appears, open the Android folder and select Android Activity. Click Next.
  3. Select BlankActivity and click Next.
  4. Fill in the activity details:
    • Project: MyFirstApp
    • Activity Name: DisplayMessageActivity
    • Layout Name: activity_display_message
    • Fragment Layout Name: fragment_display_message
    • Title: My Message
    • Hierarchial Parent: com.example.myfirstapp.MainActivity
    • Navigation Type: None

    Click Finish.

All subclasses of Activity must implement the onCreate() method. The system calls this when creating a new instance of the activity. This method is where you must define the activity layout with the setContentView() method and is where you should perform initial setup for the activity components.

Add it to the manifest

All activities must be declared in your manifest file, AndroidManifest.xml, using an <activity> element.

        <activity            android:name=”com.medlington.nextpage.DisplayMessageActivity”
            android:label=”@string/title_activity_display_message” >
        </activity>