{"id":687,"date":"2017-01-18T11:19:31","date_gmt":"2017-01-18T11:19:31","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=434"},"modified":"2022-09-13T15:54:40","modified_gmt":"2022-09-13T14:54:40","slug":"not-playing-from-a-specific-page","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/audio-uwp-programming-in-c\/not-playing-from-a-specific-page","title":{"rendered":"Not Playing From A Specific Page"},"content":{"rendered":"<p>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&#8217;t have to be tied to a page.<\/p>\n<pre><code>\n\t\/\/Declare the players, for example 2 seperate players here\n\tprivate static Windows.Media.Playback.MediaPlayer BackgroundMediaPlayer1 = new Windows.Media.Playback.MediaPlayer();\n\tprivate static Windows.Media.Playback.MediaPlayer BackgroundMediaPlayer2 = new Windows.Media.Playback.MediaPlayer();\n\n\t\/\/SET FUNCTION TO CALL WHEN PLAYBACK ENDS\n\tBackgroundMediaPlayer1.MediaEnded += MediaPlayer_MediaEnded;\n\tBackgroundMediaPlayer2.MediaEnded += MediaPlayer_MediaEnded;\n\n\n\t\/\/In your methods:\n\tMediaPlayerSelectNewFileAsync(BackgroundMediaPlayer1, \"audio_song.mp3\", 0.15f, true);\n\tMediaPlayerSelectNewFileAsync(BackgroundMediaPlayer2, \"audio1.mp3\", 1.0f, false);\n\n\tBackgroundMediaPlayer2.Play();\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 Task MediaPlayerSelectNewFileAsync(Windows.Media.Playback.MediaPlayer 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\n\t\t\t\/\/----- SET THE PLAYER FILE LOCATION -----\n\t\t\tWindows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@\"Assets\");\n\t\t\tWindows.Storage.StorageFile file = await folder.GetFileAsync(FilePath);\n\t\t\tMediaPlayer1.AutoPlay = false;\n\t\t\tMediaPlayer1.Source = Windows.Media.Core.MediaSource.CreateFromStorageFile(file);\n\n\t\t\tMediaPlayer1.IsLoopingEnabled = 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}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"MediaPlayerSelectNewFileAsync Exception: \" + ex.Message);\n\t\t}\n\n\t}\n\n\t\/\/*********************************************\n\t\/\/*********************************************\n\t\/\/********** MEDIA PLAYERS - PLAY IT **********\n\t\/\/*********************************************\n\t\/\/*********************************************\n\tprivate static void MediaPlayer_Play(Windows.Media.Playback.MediaPlayer sender, object args)\n\t{\n\t\ttry\n\t\t{\n\t\t\tsender.Play();\n\t\t}\n\t\tcatch (Exception )\n\t\t{\n\t\t}\n\t}\n\n\t\t\n\n\t\/\/****************************************************\n\t\/\/****************************************************\n\t\/\/********** MEDIA PLAYERS - PLAYBACK ENDED **********\n\t\/\/****************************************************\n\t\/\/****************************************************\n\tprivate static void MediaPlayer_MediaEnded(Windows.Media.Playback.MediaPlayer sender, object args)\n\t{\n\t\ttry\n\t\t{\n\t\t\tif (sender == BackgroundMediaPlayer1)\n\t\t\t{\n\t\t\t}\n\n\t\t\tif (sender == BackgroundMediaPlayer2)\n\t\t\t{\n\t\t\t}\n\n\t\t}\n\t\tcatch (Exception)\n\t\t{\n\t\t}\n\t}\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[146],"tags":[],"class_list":["post-687","post","type-post","status-publish","format-standard","hentry","category-audio-uwp-programming-in-c"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/687","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=687"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":1204,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/687\/revisions\/1204"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}