How To Play Audio In Xamarin Android App Using Visual Studio 2015

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.

Android

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.

Android

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.

Android


Android

Step 4
 
Next, delete the Linear Layout and default "hello world" button by going to the source panel and removing the button code.
 
Also, delete the C# button action code by going to the MainActivity.cs page.

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.

Android

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").

Android

Step 7

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

Android

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <Button android:id="@+id/playButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/playAudioText" />  
  3. </LinearLayout>  
Step 8
 
Open the String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.

Android

Step 9

Write the following xml code in it.

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="playAudioText">Play Audio</string>  
  4.     <string name="app_name">PlayAudio</string>  
  5. </resources>  
Android

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).

Android

Step 11

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

Android

Step 12

Select the audio file and click Add button.

Android

Step 13

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

Android

MainActivity.cs
  1. Add a using directive.  
  2. using Android.Media;  
  3. At the top of the class, declare a WebView object  
  4. MediaPlayer _player;  
Step 14

Write the following code from OnCreate() method.

Android

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     _player = MediaPlayer.Create(this, Resource.Raw.test);  
  6.     var playButton = FindViewById < Button > (Resource.Id.playButton);  
  7.     playButton.Click += delegate {  
  8.         _player.Start();  
  9.     };  
  10. }  
Step 15

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.

Android

Output

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


Android

Summary

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

Up Next
    Ebook Download
    View all
    Learn
    View all