Audio is normally played from a Page, and if you change page it will stop. However you can play audio from anywhere in an app, the player doesn’t have to be tied to a page.


	//Declare the players, for example 2 seperate players here
	private static Windows.Media.Playback.MediaPlayer BackgroundMediaPlayer1 = new Windows.Media.Playback.MediaPlayer();
	private static Windows.Media.Playback.MediaPlayer BackgroundMediaPlayer2 = new Windows.Media.Playback.MediaPlayer();

	//SET FUNCTION TO CALL WHEN PLAYBACK ENDS
	BackgroundMediaPlayer1.MediaEnded += MediaPlayer_MediaEnded;
	BackgroundMediaPlayer2.MediaEnded += MediaPlayer_MediaEnded;


	//In your methods:
	MediaPlayerSelectNewFileAsync(BackgroundMediaPlayer1, "audio_song.mp3", 0.15f, true);
	MediaPlayerSelectNewFileAsync(BackgroundMediaPlayer2, "audio1.mp3", 1.0f, false);

	BackgroundMediaPlayer2.Play();


	//***********************************************************
	//***********************************************************
	//********** MEDIA PLAYERS - SET NEW PLAYBACK FILE **********
	//***********************************************************
	//***********************************************************
	//Volume	0.0f(silence) to 1.0f(full volume relative to the current device volume).
	public static async Task MediaPlayerSelectNewFileAsync(Windows.Media.Playback.MediaPlayer MediaPlayer1, string FilePath, double Volume, bool PlayImmediately)
	{

		try
		{
			//----- STOP PLAYBACK IF NECESSARY -----
			try
			{
				MediaPlayer1.Pause();
			}
			catch (Exception )
			{
			}

			//----- SET THE PLAYER FILE LOCATION -----
			Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
			Windows.Storage.StorageFile file = await folder.GetFileAsync(FilePath);
			MediaPlayer1.AutoPlay = false;
			MediaPlayer1.Source = Windows.Media.Core.MediaSource.CreateFromStorageFile(file);

			MediaPlayer1.IsLoopingEnabled = false;

			MediaPlayer1.Volume = Volume;		//0.0f(silence) to 1.0f(full volume relative to the current device volume)

			//----- PLAY THE FILE NOW IF SELECTED -----
			if (PlayImmediately)
				MediaPlayer1.MediaOpened += MediaPlayer_Play;

		}
		catch (Exception ex)
		{
			System.Diagnostics.Debug.WriteLine("MediaPlayerSelectNewFileAsync Exception: " + ex.Message);
		}

	}

	//*********************************************
	//*********************************************
	//********** MEDIA PLAYERS - PLAY IT **********
	//*********************************************
	//*********************************************
	private static void MediaPlayer_Play(Windows.Media.Playback.MediaPlayer sender, object args)
	{
		try
		{
			sender.Play();
		}
		catch (Exception )
		{
		}
	}

		

	//****************************************************
	//****************************************************
	//********** MEDIA PLAYERS - PLAYBACK ENDED **********
	//****************************************************
	//****************************************************
	private static void MediaPlayer_MediaEnded(Windows.Media.Playback.MediaPlayer sender, object args)
	{
		try
		{
			if (sender == BackgroundMediaPlayer1)
			{
			}

			if (sender == BackgroundMediaPlayer2)
			{
			}

		}
		catch (Exception)
		{
		}
	}

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *