Playing a movie

As the final recipe of this chapter, we will see how to play a movie with your Android application. Playing video, unlike playing audio, involves some special View elements for displaying it to the users.

Getting ready

For the last time, we will reuse the same project, and more specifically, we will play a video of Mario under the button for playing the Mario theme seen in the previous recipe.

How to do it...

  1. Add the following code to your Main.axml file under the Layout file:
    <VideoView android:id="@+id/myVideoView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    </VideoView>
  2. As a result, the content of your Main.axml file should look as follows:
    How to do it...
  3. Add the following code to the MainActivity.cs class in the OnCreate() method:
    var videoView = FindViewById<VideoView> (Resource.Id.SampleVideoView);
    
    var uri = Android.Net.Uri.Parse ("url of your video");
    
    videoView.SetVideoURI (uri);
  4. Finally, invoke the videoView.Start(); method.

Note

Note that playing an Internet-based video, even short one, will take a very long time as the video needs to be fully loaded while using this technique.

How it works...

As discussed in the introduction to this recipe, playing a video should involve a special view to display it to users. This special view is named the VideoView element and should be used in the same way as the simple TextView element that we saw earlier in this book:

<VideoView android:id="@+id/myVideoView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
</VideoView>

As you can see in the previous code sample, you can apply the same parameters to the VideoView element as the TextView element, such as layout-based options.

The VideoView element, like the MediaPlayer class for audio, has a method to set the video URI, named SetVideoURI, and another one to start the video, named Start().

See also

Chapter 10, Taking Advantage of the Android Platform, will expand on what you learned here about playing a video, notably by demonstrating how to use the camera and record video.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.98.108