In this article written by Mathieu Nayrolles, author of the book Xamarin Studio for Android Programming: A C# Cookbook, wants us to learn of how to play sound and how to play a movie by using an user-interactive—press on button in a programmative way.
(For more resources related to this topic, see here.)
There are an infinite number of occasions in which you want your applications to play sound. In this section, we will learn how to play a song from our application in a user-interactive—press on a button—or programmative way.
For using this you need to create a project on your own:
using Android.Media;
MediaPlayer _myPlayer;
_myPlayer = MediaPlayer.Create (this, Resource.Raw.mario);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
_myPlayer.Start();
};
In the preceding code sample, the first line creates an instance of the MediaPlayer using the this statement as Context and Resource.Raw.mario as the file to play with this MediaPlayer. The rest of the code is trivial, we just acquired a reference to the button and create a behavior for the OnClick() event of the button. In this event, we call the Start() method of the _myPlayer() variable.
Run your application and press on the button as shown on the following screenshot:
You should hear the Mario theme playing right after you pressed the button, even if you are running the application on the emulator.
Playing sound (and video) is an activity handled by the Media Player class of the Android platform. This class involves some serious implementations and a multitude of states in the same way as activities. However, as an Android Applications Developer and not as an Android Platform Developer we only require a little background on this.
The Android multimedia framework includes—thought the Media Player class—a support for playing a very large variety of media such MP3 from the filesystem or from the Internet. Also, you can only play a song on the current sound device which could be the phone speakers, headset or even a Bluetooth enabled speaker. In other words, even if there are many sound outputs available on the phone, the current default settled by the user is the one where your sound will be played. Finally, you cannot play a sound during a call.
Obviously, you may want to play sounds that are not stored locally—in the raw folder—but anywhere else on the phone, like SDcard or so. To do it, you have to use the following code sample:
Uri myUri = new Uri ("uriString");
_myPlayer = new MediaPlayer();
_myPlayer.SetAudioStreamType (AudioManager.UseDefaultStreamType);
_myPlayer.SetDataSource(myUri);
_myPlayer.Prepare();
The first line defines a Uri for the targeting file to play. The three following lines set the StreamType, the Uri prepares the MediaPlayer. The Prepare() is a method which prepares the player for playback in a synchrone manner. Meaning that this instruction blocks the program until the player is ready to play, until the player has loaded the file. You could also call the PrepareAsync() which returns immediately and performs the loading in an asynchronous way.
Using a code very similar to the one required to play sounds stored somewhere on the phone, we could play sound from the Internet.
Basically, we just have to replace the Uri parameter by some HTTP address just like the following:
String url = "http://myWebsite/mario.mp3";
_myPlayer = new MediaPlayer();
_myPlayer.SetAudioStreamType (AudioManager.UseDefaultStreamType);
_myPlayer.SetDataSource(url);
_myPlayer.Prepare();
Also, you must request the permission to access the Internet with your application. This is done in the manifest by adding an <uses-permission> tag for your application as shown by the following code sample:
<application android_icon="@drawable/Icon" android_label="Splash">
<uses-permission android_name="android.permission.INTERNET" />
</application>
As a final recipe in this article, we will see how to play a movie with your Android application. Playing video, unlike playing audio, involves some special views for displaying it to the users.
For the last time, we will reuse the same project, and more specifically, we will play a Mario video under the button for playing the Mario theme seen in the previous recipe.
<VideoView android_id="@+id/myVideoView"
android_layout_width="fill_parent"
android_layout_height="fill_parent">
</VideoView>
var videoView = FindViewById<VideoView> (Resource.Id.SampleVideoView);
var uri = Android.Net.Uri.Parse ("url of your video");
videoView.SetVideoURI (uri);
Note that playing Internet based video, even short one, will take a very long time as the video need to be fully loaded while using this technique.
Playing video should involve some special view to display it to users. This special view is named the VideoView tag and should be used as same as simple TextView tag.
<VideoView android_id="@+id/myVideoView"
android_layout_width="fill_parent"
android_layout_height="fill_parent">
</VideoView>
As you can see in the preceding code sample, you can apply the same parameters to VideoView tag as TextView tag such as layout based options.
The VideoView tag, like the MediaPlayer for audio, have a method to set the video URI named SetVideoURI and another one to start the video named Start();.
In this article, we've learned how to play a sound clip as well as video on the Android application which we've developed.
Further resources on this subject: