{"id":915,"date":"2019-07-18T08:54:01","date_gmt":"2019-07-18T07:54:01","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=915"},"modified":"2019-07-18T08:54:01","modified_gmt":"2019-07-18T07:54:01","slug":"playing-video-using-mediaelement","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/video\/playing-video-using-mediaelement","title":{"rendered":"Playing video using MediaElement"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">.xaml File<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;MediaElement x:Name=\"MediaPlayer1\" AutoPlay=\"False\" Source = \"\" Stretch=\"Uniform\" Width=\"{Binding Path=ActualWidth, ElementName=container}\" Height=\"{Binding Path=ActualHeight, ElementName=container}\" \/&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">.cs File<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/CONSTRUCTOR:\n\tMediaPlayer1.MediaEnded += MediaPlayer_MediaEnded;\n\n\/\/PLAY A FILE\n\tMediaPlayerSelectNewFileAsync(MediaPlayer1, \"MyVideo.mp4\", 100, true);\n\n\n\t\/\/***********************************************************\n\t\/\/***********************************************************\n\t\/\/********** MEDIA PLAYERS - SET NEW PLAYBACK FILE **********\n\t\/\/***********************************************************\n\t\/\/***********************************************************\n\t\/\/Volume\t0.0f(silence) to 1.0f(full volume relative to the current device volume).\n\tpublic static async void MediaPlayerSelectNewFileAsync(MediaElement MediaPlayer1, string FilePath, double Volume, bool PlayImmediately)\n\t{\n\n\t\ttry\n\t\t{\n\t\t\t\/\/----- STOP PLAYBACK IF NECESSARY -----\n\t\t\ttry\n\t\t\t{\n\t\t\t\tMediaPlayer1.Pause();\n\t\t\t}\n\t\t\tcatch (Exception )\n\t\t\t{\n\t\t\t}\n\t\t\t\t\n\n\t\t\tMediaPlayer1.AutoPlay = false;\n\n\t\t\t\/\/----- SET THE PLAYER FILE LOCATION -----\n\t\t\tMediaPlayer1.Source = new Uri(\"ms-appdata:\/\/\/local\/\" + ApMain.LOCAL_FOLDER_GAME_FILES_DIRECTORY + \"\/\" + FilePath);\n\n\n\t\t\tMediaPlayer1.IsLooping = false;\n\n\t\t\tMediaPlayer1.Volume = Volume;\t\t\/\/0.0f(silence) to 1.0f(full volume relative to the current device volume)\n\n\t\t\t\/\/----- PLAY THE FILE NOW IF SELECTED -----\n\t\t\tif (PlayImmediately)\n\t\t\t\tMediaPlayer1.MediaOpened += MediaPlayer_Play;\n\n\t\t\t\/\/----- SET FUNCTION TO CALL WHEN PLAYBACK ENDS -----\n\t\t\t\/\/MediaPlayer1.MediaEnded += MediaPlayer_MediaEnded;\t\t\/\/Can't do here otherwise we create multiple calls to the MediaPlayer_MediaEnded method every time we add this again!\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"MediaPlayerSelectNewFileAsync Exception: \" + ex.Message);\n\t\t}\n\t}\n\n\t\/\/*********************************************\n\t\/\/*********************************************\n\t\/\/********** MEDIA PLAYERS - PLAY IT **********\n\t\/\/*********************************************\n\t\/\/*********************************************\n\tprivate static void MediaPlayer_Play(object sender, RoutedEventArgs e)\n\t{\n\t\ttry\n\t\t{\n\t\t\tMediaElement CallingMediaPlayer = (MediaElement)sender;\n\t\t\tCallingMediaPlayer.Play();\n\t\t}\n\t\tcatch (Exception )\n\t\t{\n\t\t}\n\t}\n\n\n\n\t\/\/***************************************************\n\t\/\/***************************************************\n\t\/\/********** MEDIA PLAYER - PLAYBACK ENDED **********\n\t\/\/***************************************************\n\t\/\/***************************************************\n\t\/\/private static void MediaPlayer_MediaEnded(Windows.Media.Playback.MediaPlayer sender, object args)\n\tprivate void MediaPlayer_MediaEnded(object sender, RoutedEventArgs e)\n\t{\n\t\ttry\n\t\t{\n\t\t\tMediaPlayer1.Stop();\n\t\t}\n\t\tcatch (Exception)\n\t\t{\n\t\t}\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>.xaml File .cs File<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109],"tags":[],"class_list":["post-915","post","type-post","status-publish","format-standard","hentry","category-video"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=915"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/915\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}