There are an infinite number of occasions in which you would want your applications to play sound. In this section, you will learn how to play a song from our application in a user-interactive way (by means of clicking on a button) or programmative way.
Now, we will see how to play a song :
using
attribute to your MainActivity.cs
file:using Android.Media;
_myPlayer
in the MediaPlayer
class:MediaPlayer _myPlayer;
raw
under the Resources
folder.We will use the Mario theme, free of rights for noncommercial use, downloaded from http://mp3skull.com.
OnCreate()
method of your MainActivity
class:_myPlayer = MediaPlayer.Create (this, Resource.Raw.mario); Button button = FindViewById<Button> (Resource.Id.myButton); button.Click += delegate { _myPlayer.Start(); };
In the previous code sample, the first line creates an instance of the MediaPlayer
using this
as context and Resource.Raw.mario
as the file to play with this MediaPlayer
. The rest of the code is simple, we just acquired a reference to the button and created a behavior for the OnClick
event of the button. In this event, we call the Start()
method of the _myPlayer()
variable.
You should hear the Mario theme playing right after you clicked the button, and this will happen even if you running the application on the emulator.
Playing sound (and video) is an activity handled by the MediaPlayer
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—through the MediaPlayer
class—a support for playing a very large variety of media, such MP3s, from the filesystem or from the Internet. Also, you can only play a song on the current sound device, which can be the phone's 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 set by the user is the one where your sound will be played. However, you cannot play sound during a call.
There are two ways to play sound, which are discussed in the following sections.
You may want to play sounds that are not stored locally, that is, in the raw
folder, but anywhere else on the phone, such as on an SD card. To do this, 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 target file to be played. The next three lines set the StreamType
and the Uri
parameter and prepare the MediaPlayer
class. The Prepare()
method is the method that prepares the player for playback in a synchronous manner, meaning that this instruction blocks the program until the player is ready to play, that is, until the player has loaded the file. You can also call the PrepareAsync()
method, 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 can play sounds from the Internet.
Basically, we just have to replace the Uri
attribute with an HTTP address, as follows:
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 a <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>
18.222.121.170