How to Play Audio using MediaPlayer using Xamarin in Visual Studio

How to Play Audio using MediaPlayer using Xamarin in Visual Studio

In the previous post (Audio Recording using MediaRecorder using Xamarin in Visual Studio),  we have seen how to record an audio. In this post we are going to see how to play audio using Xamarin. The Simplest way to play audio in android is using the inbuilt MediaPlayer class.

Lets create the User interface for the App.

Add a button in the Main.axml file, assign an id (myButton) to the button, so that we can use it in the cs files.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""








android:text="@string/playAudio" />


Now go to MainActivity.cs and do the following.

Create the Click event handler for the button (myButton) and use the MediaPlayer to play audio.

In order to play audio the following steps has to be done in the following order.

  1. Create and instantiate the MediaPlayer
  2. Set the Source for the player by passing the source as parameter for the SetDataSource method.
  3. Initialize the playback by calling the Prepare 
  4. Call the Start method to start playing.

Here is a quick visual representation of the steps that you can give a quick look.


The MediaPlayer  starts Playing the Audio, if the audio has to be paused, resumed or stop, we can  use the following methods.

  1. To pause the Audio use the Pause
  2. To resume the Audio use the Start Which resumes form where the audio has been paused.
  3. To stop the Audio recording use the Stop
  4. Release method can use to release the resources when not needed.

Sample code is provided below.

audio play

when we run the app, we can see the button Play Audio. which plays the audio provided as Data Source.

I hope this post helps in playing the audio using Xamarin.

Arun Kumar

Arun kumar Surya Prakash, is a  Developer  Consultant. He  is a Microsoft Certified Solution  Developer  for Store App Development, and a Azure Solution Developer. His core skills is on developing Windows Store App and Cross Platform App development. You can follow him @arunnov04

One Comment to “How to Play Audio using MediaPlayer using Xamarin in Visual Studio”

Comments are closed.