Download Code

Xamarin MediaPlayer Example Android

In this tutorial you will learn how to create Xamarin MediaPlayer example android application using Xamarin IDE.

First let’s start with some basics.

Xamarin MediaPlayer

This class is basically used to control play back of audio/video files and streams.More detailed Link.

MediaPlayer.Create()

This method is basically used to set data Source of audio files and set playback automatically.

Now go ahead and create new xamarin mediaplayer example android application project File > New  > New Solution > Android App.I named it Play.

Then After creating project go to your Main.axml file.Add three buttons like previous,stop,next..

xamarin mediaplayer,xamarin mediaplayer example,xamarin mediaplayer example android,mediaplayer example android

Next go to Resource directory of project Right click,hit Add > New Folder and name it raw.Now add your audio files in this folder just like this.

xamarin mediaplayer,xamarin mediaplayer example,xamarin mediaplayer example android,mediaplayer example android

Now go to your MainActivity.cs class and create variables.

  • Buttons
  • Array for Songs
  • Boolean variable to detect song is playing or not
  • MediaPlayer class variable
  • position

Next create a separate method to load or store songs in an array and call that method in oncreate().Then refer all buttons and apply click events.

namespace play
{
	[Activity (Label = "play", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Button prevBtn,stopBtn,nextBtn;   // previous , stop ,next button variables
		bool isplaying=false;             // boolean variable to detect song is playing or not
		int[] songs = new int[3];         // songs resrouces array
		MediaPlayer song;                 // MediaPlayer class variable
		int position=0;                   // position of song

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// load songs in array from resource > raw folder

			loadSongs (); 

			
			prevBtn = FindViewById< Button > (Resource.Id.button1);   // refering previous button
			stopBtn = FindViewById< Button > (Resource.Id.button2);   // refering stop button
			nextBtn = FindViewById< Button > (Resource.Id.button3);   // refering next button
			// applying click events on all buttons
			prevBtn.Click += PrevBtn_Click;
			stopBtn.Click += StopBtn_Click;
			nextBtn.Click += NextBtn_Click;
		}

		//previous button click method
		void PrevBtn_Click (object sender, System.EventArgs e)
		{
			
		}

		// stop button method
		void StopBtn_Click (object sender, System.EventArgs e)
		{
			
		}
		//next button method
		void NextBtn_Click (object sender, System.EventArgs e)
		{
			
		}
		public void loadSongs(){
			songs [0] = Resource.Raw.bukhaar;
			songs [1] = Resource.Raw.rooh;
			songs [2] = Resource.Raw.shakira;
		}



	}
}

Now next we will check a song is already playing or not.If not then set data source and playback using MediaPlayer.create() method.Start song and set boolean variable to true that it’s playing now.

namespace play
{
	[Activity (Label = "play", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Button prevBtn,stopBtn,nextBtn;   // previous , stop ,next button variables
		bool isplaying=false;             // boolean variable to detect song is playing or not
		int[] songs = new int[3];         // songs resrouces array
		MediaPlayer song;                 // MediaPlayer class variable
		int position=0;                   // position of song

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// load songs in array from resource > raw folder

			loadSongs (); 

			// first detect song is playing or not by default it will play a song

			if (isplaying == false) {
				song = MediaPlayer.Create (this, songs [position]);        // Media player.create method is used to set data source and prepare playback automically
				song.Start ();     // starting song
				isplaying = true;    // setting isplaying to true 
			}
			prevBtn = FindViewById< Button > (Resource.Id.button1);   // refering previous button
			stopBtn = FindViewById< Button > (Resource.Id.button2);   // refering stop button
			nextBtn = FindViewById< Button > (Resource.Id.button3);   // refering next button
			// applying click events on all buttons
			prevBtn.Click += PrevBtn_Click;
			stopBtn.Click += StopBtn_Click;
			nextBtn.Click += NextBtn_Click;
		}

		//previous button click method
		void PrevBtn_Click (object sender, System.EventArgs e)
		{
			
		}

		// stop button method
		void StopBtn_Click (object sender, System.EventArgs e)
		{
			
		}
		//next button method
		void NextBtn_Click (object sender, System.EventArgs e)
		{
			
		}
		public void loadSongs(){
			songs [0] = Resource.Raw.bukhaar;
			songs [1] = Resource.Raw.rooh;
			songs [2] = Resource.Raw.shakira;
		}



	}
}

At last we will setup our button methods to change or stop song using array indexes.Complete MainActivity.cs class.

MainActivity.cs

/* All Rights Reserved By Farhan Rasheed */

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Media;

namespace play
{
	[Activity (Label = "play", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		Button prevBtn,stopBtn,nextBtn;   // previous , stop ,next button variables
		bool isplaying=false;             // boolean variable to detect song is playing or not
		int[] songs = new int[3];         // songs resrouces array
		MediaPlayer song;                 // MediaPlayer class variable
		int position=0;                   // position of song

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// load songs in array from resource > raw folder

			loadSongs (); 

			// first detect song is playing or not by default it will play a song

			if (isplaying == false) {
				song = MediaPlayer.Create (this, songs [position]);        // Media player.create method is used to set data source and prepare playback automically
				song.Start ();     // starting song
				isplaying = true;    // setting isplaying to true 
			}
			prevBtn = FindViewById< Button > (Resource.Id.button1);   // refering previous button
			stopBtn = FindViewById< Button > (Resource.Id.button2);   // refering stop button
			nextBtn = FindViewById< Button > (Resource.Id.button3);   // refering next button
			// applying click events on all buttons
			prevBtn.Click += PrevBtn_Click;
			stopBtn.Click += StopBtn_Click;
			nextBtn.Click += NextBtn_Click;
		}

		//previous button click method
		void PrevBtn_Click (object sender, System.EventArgs e)
		{
			if (isplaying) {
				song.Stop ();
				if (position == 0) {
					position = 2;
					song = MediaPlayer.Create (this, songs [position]);
					song.Start ();
				} else {
					position--;
					song = MediaPlayer.Create (this, songs [position]);
					song.Start ();
				}
			} else {
				song = MediaPlayer.Create (this, songs [position]);
				song.Start ();
			}
		}

		// stop button method
		void StopBtn_Click (object sender, System.EventArgs e)
		{
			if(isplaying)
			song.Stop ();
		}
		//next button method
		void NextBtn_Click (object sender, System.EventArgs e)
		{
			if (isplaying) {
				song.Stop ();
				if (position == 2) {
					position = 0;
					song = MediaPlayer.Create (this, songs [position]);
					song.Start ();
				} else {
					position++;
					song = MediaPlayer.Create (this, songs [position]);
					song.Start ();
				}
			} else {
				song = MediaPlayer.Create (this, songs [position]);
				song.Start ();
			}
		}
		public void loadSongs(){
			songs [0] = Resource.Raw.bukhaar;
			songs [1] = Resource.Raw.rooh;
			songs [2] = Resource.Raw.shakira;
		}



	}
}


Now run your xamarin mediaplayer example android application project.Output should be like this.

xamarin mediaplayer,xamarin mediaplayer example,xamarin mediaplayer example android,mediaplayer example android

that’s it for xamarin mediaplayer example android tutorial.I hope you understand the purpose of MediaPlayer class.

Thanks