.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)
		{
		}
	}
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 *