Introduction
Xamarin is a platform for developing cross-platform and multi-platform apps (ex. Windows phone, Android, and iOS). In Xamarin, the code sharing concept is used. The Xamarin Studio is available in Visual Studio too.
Audio Play control is used to play the audio files, like mp3 etc. in your Android app.
Prerequisites
- Visual Studio 2015 update 3
The following steps are needed to be followed in order to play the audio files in Android app.
Step 1
Click File--> New --> Project, or click (Ctrl+Shift+N). This will open the list of various types of projects that we can create.

Step 2
Now, select Installed --> Templates --> Visual C#--> Android --> Blank App (Android).
Next, give your android app a name (Ex:sample) and give path to your project. Now, click OK.

Step 3
Go to the Solution Explorer, select Resource --> Layout. Double click to open main.axml page. If you want to write the code, open code view. If you want to design the app using UI, go to the Designer.


Step 5
Next, go to the toolbox window where we have all types of tools and controls. Scroll down until you see all the tools and controls.
Drag and drop the Button.

Step 6
Go to the properties window and edit the Button id value and Text value (Ex: android:id="@+id/playButton" android:text="@string/playAudioText").

Step 7
In this step, go to the Main.axml page Source Panel and note the button id Value.

Main.axml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
- <Button android:id="@+id/playButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/playAudioText" />
- </LinearLayout>

Step 9
Write the following xml code in it.
String.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="playAudioText">Play Audio</string>
- <string name="app_name">PlayAudio</string>
- </resources>

Step 10
Add a folder named "raw".
For this, go to Solution Explorer--> Resource. Right click on it and click Add --> New Folder, and give name (raw).

Step 11
Add the existing audio file by going to Solution Explorer-->Resource-->Raw-->Right Click-->Add-->Existing item.(Shift+Alt+F).

Step 12
Select the audio file and click Add button.

Step 13
In the MainActivity.cs page, write the following code.

MainActivity.cs
- Add a using directive.
- using Android.Media;
- At the top of the class, declare a WebView object
- MediaPlayer _player;
Write the following code from OnCreate() method.

MainActivity.cs
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- _player = MediaPlayer.Create(this, Resource.Raw.test);
- var playButton = FindViewById < Button > (Resource.Id.playButton);
- playButton.Click += delegate {
- _player.Start();
- };
- }
If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.
Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Output
After a few seconds, the app will start running on your phone. Tap the "Play Audio" button.

Summary
So, this was the process of how to play an audio file in Android app, using Visual Studio 2015 update 3.

unalca unalcaPosted Feb 18, 2018, 6:14 AM
Using Android.App;using Android.Widget; using Android.OS; using Android.Media; namespace Nasr_Suresi { [Activity(Label = "Nasr_Suresi", MainLauncher = true,Icon = "@drawable/web_hi_res_512")] public class MainActivity : Activity { MediaPlayer player1, player2, player3, player4, player5,player6; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource imageButton1 SetContentView(Resource.Layout.Main); player1 = MediaPlayer.Create(this, Resource.Raw.besmele); ImageButton imageButton10 = FindViewById<ImageButton>(Resource.Id.imageButton1); imageButton10.Click += ImageButton10_Click; player2 = MediaPlayer.Create(this, Resource.Raw.nasr_1); ImageButton imageButton20 = FindViewById<ImageButton>(Resource.Id.imageButton2); imageButton20.Click += ImageButton20_Click; player3 = MediaPlayer.Create(this, Resource.Raw.nasr_2); ImageButton imageButton30 = FindViewById<ImageButton>(Resource.Id.imageButton3); imageButton30.Click += ImageButton30_Click; player4 = MediaPlayer.Create(this, Resource.Raw.nasr_3); ImageButton imageButton40 = FindViewById<ImageButton>(Resource.Id.imageButton4); imageButton40.Click += ImageButton40_Click; player5 = MediaPlayer.Create(this, Resource.Raw.nasr_4); ImageButton imageButton50 = FindViewById<ImageButton>(Resource.Id.imageButton5); imageButton50.Click += ImageButton50_Click; player6 = MediaPlayer.Create(this, Resource.Raw.NASR_SURESI); Button button1 = FindViewById<Button>(Resource.Id.button1); button1.Click += Button1_Click; } private void Button1_Click(object sender, System.EventArgs e) { player6.Start(); } private void ImageButton10_Click(object sender, System.EventArgs e) { player1.Start(); } private void ImageButton20_Click(object sender, System.EventArgs e) { player2.Start(); } private void ImageButton30_Click(object sender, System.EventArgs e) { player3.Start(); } private void ImageButton40_Click(object sender, System.EventArgs e) { player4.Start(); } private void ImageButton50_Click(object sender, System.EventArgs e) { player5.Start(); } } }
unalca unalcaPosted Feb 18, 2018, 6:13 AM
Hello delpin, i have five button with music 1, music 2.... i wil one action in one time. if i click some time all of the buttons work all musics. Dus while music 1 in playing, play music 2. can you helpen.
Fabio Silva LimaPosted Dec 9, 2016, 12:02 PM
Show thank you! very good!
Prasanna MuraliPosted Oct 22, 2016, 12:07 PM
Nice post..
RajaPosted Oct 21, 2016, 8:28 AM
Good one........
Joe WilsonPosted Oct 19, 2016, 2:21 PM
Thank you.