.xaml File

<MediaElement x:Name="MediaPlayer1" AutoPlay="False" Source = "" Stretch="Uniform" Width="{Binding Path=ActualWidth, ElementName=container}" Height="{Binding Path=ActualHeight, ElementName=container}" />

.cs File

//CONSTRUCTOR:
	MediaPlayer1.MediaEnded += MediaPlayer_MediaEnded;

//PLAY A FILE
	MediaPlayerSelectNewFileAsync(MediaPlayer1, "MyVideo.mp4", 100, true);


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

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

			MediaPlayer1.AutoPlay = false;

			//----- SET THE PLAYER FILE LOCATION -----
			MediaPlayer1.Source = new Uri("ms-appdata:///local/" + ApMain.LOCAL_FOLDER_GAME_FILES_DIRECTORY + "/" + FilePath);


			MediaPlayer1.IsLooping = 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;

			//----- SET FUNCTION TO CALL WHEN PLAYBACK ENDS -----
			//MediaPlayer1.MediaEnded += MediaPlayer_MediaEnded;		//Can't do here otherwise we create multiple calls to the MediaPlayer_MediaEnded method every time we add this again!
		}
		catch (Exception ex)
		{
			System.Diagnostics.Debug.WriteLine("MediaPlayerSelectNewFileAsync Exception: " + ex.Message);
		}
	}

	//*********************************************
	//*********************************************
	//********** MEDIA PLAYERS - PLAY IT **********
	//*********************************************
	//*********************************************
	private static void MediaPlayer_Play(object sender, RoutedEventArgs e)
	{
		try
		{
			MediaElement CallingMediaPlayer = (MediaElement)sender;
			CallingMediaPlayer.Play();
		}
		catch (Exception )
		{
		}
	}



	//***************************************************
	//***************************************************
	//********** MEDIA PLAYER - PLAYBACK ENDED **********
	//***************************************************
	//***************************************************
	//private static void MediaPlayer_MediaEnded(Windows.Media.Playback.MediaPlayer sender, object args)
	private void MediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
	{
		try
		{
			MediaPlayer1.Stop();
		}
		catch (Exception)
		{
		}
	}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

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