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 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.
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 8
Open the String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.
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;
Step 14
Write the following code from OnCreate() method.
MainActivity.cs
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- _player = MediaPlayer.Create(this, Resource.Raw.test);
- var playButton = FindViewById < Button > (Resource.Id.playButton);
- playButton.Click += delegate {
- _player.Start();
- };
- }
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.
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.