├── .gitignore ├── LICENSE ├── MediaPlayerElementWithHttpClient.sln ├── MediaPlayerElementWithHttpClient ├── App.xaml ├── App.xaml.cs ├── Assets │ ├── LockScreenLogo.scale-200.png │ ├── SplashScreen.scale-200.png │ ├── Square150x150Logo.scale-200.png │ ├── Square44x44Logo.scale-200.png │ ├── Square44x44Logo.targetsize-24_altform-unplated.png │ ├── StoreLogo.png │ └── Wide310x150Logo.scale-200.png ├── HttpRandomAccessStream.cs ├── MainPage.xaml ├── MainPage.xaml.cs ├── MediaPlayerElementWithHttpClient.csproj ├── Package.appxmanifest ├── Properties │ ├── AssemblyInfo.cs │ └── Default.rd.xml └── project.json ├── README.md └── WithoutHtttpClient.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Gilberto Stankiewicz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaPlayerElementWithHttpClient", "MediaPlayerElementWithHttpClient\MediaPlayerElementWithHttpClient.csproj", "{8856A648-D019-43B1-BC82-901B2617DD8A}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|ARM = Debug|ARM 11 | Debug|x64 = Debug|x64 12 | Debug|x86 = Debug|x86 13 | Release|ARM = Release|ARM 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|ARM.ActiveCfg = Debug|ARM 19 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|ARM.Build.0 = Debug|ARM 20 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|ARM.Deploy.0 = Debug|ARM 21 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|x64.ActiveCfg = Debug|x64 22 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|x64.Build.0 = Debug|x64 23 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|x64.Deploy.0 = Debug|x64 24 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|x86.ActiveCfg = Debug|x86 25 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|x86.Build.0 = Debug|x86 26 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Debug|x86.Deploy.0 = Debug|x86 27 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|ARM.ActiveCfg = Release|ARM 28 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|ARM.Build.0 = Release|ARM 29 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|ARM.Deploy.0 = Release|ARM 30 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|x64.ActiveCfg = Release|x64 31 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|x64.Build.0 = Release|x64 32 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|x64.Deploy.0 = Release|x64 33 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|x86.ActiveCfg = Release|x86 34 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|x86.Build.0 = Release|x86 35 | {8856A648-D019-43B1-BC82-901B2617DD8A}.Release|x86.Deploy.0 = Release|x86 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | EndGlobal 41 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/App.xaml: -------------------------------------------------------------------------------- 1 |  7 | 8 | 9 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Runtime.InteropServices.WindowsRuntime; 6 | using Windows.ApplicationModel; 7 | using Windows.ApplicationModel.Activation; 8 | using Windows.Foundation; 9 | using Windows.Foundation.Collections; 10 | using Windows.UI.Xaml; 11 | using Windows.UI.Xaml.Controls; 12 | using Windows.UI.Xaml.Controls.Primitives; 13 | using Windows.UI.Xaml.Data; 14 | using Windows.UI.Xaml.Input; 15 | using Windows.UI.Xaml.Media; 16 | using Windows.UI.Xaml.Navigation; 17 | 18 | namespace MediaPlayerElementWithHttpClient 19 | { 20 | /// 21 | /// Provides application-specific behavior to supplement the default Application class. 22 | /// 23 | sealed partial class App : Application 24 | { 25 | /// 26 | /// Initializes the singleton application object. This is the first line of authored code 27 | /// executed, and as such is the logical equivalent of main() or WinMain(). 28 | /// 29 | public App() 30 | { 31 | this.InitializeComponent(); 32 | this.Suspending += OnSuspending; 33 | } 34 | 35 | /// 36 | /// Invoked when the application is launched normally by the end user. Other entry points 37 | /// will be used such as when the application is launched to open a specific file. 38 | /// 39 | /// Details about the launch request and process. 40 | protected override void OnLaunched(LaunchActivatedEventArgs e) 41 | { 42 | #if DEBUG 43 | if (System.Diagnostics.Debugger.IsAttached) 44 | { 45 | this.DebugSettings.EnableFrameRateCounter = true; 46 | } 47 | #endif 48 | Frame rootFrame = Window.Current.Content as Frame; 49 | 50 | // Do not repeat app initialization when the Window already has content, 51 | // just ensure that the window is active 52 | if (rootFrame == null) 53 | { 54 | // Create a Frame to act as the navigation context and navigate to the first page 55 | rootFrame = new Frame(); 56 | 57 | rootFrame.NavigationFailed += OnNavigationFailed; 58 | 59 | if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) 60 | { 61 | //TODO: Load state from previously suspended application 62 | } 63 | 64 | // Place the frame in the current Window 65 | Window.Current.Content = rootFrame; 66 | } 67 | 68 | if (e.PrelaunchActivated == false) 69 | { 70 | if (rootFrame.Content == null) 71 | { 72 | // When the navigation stack isn't restored navigate to the first page, 73 | // configuring the new page by passing required information as a navigation 74 | // parameter 75 | rootFrame.Navigate(typeof(MainPage), e.Arguments); 76 | } 77 | // Ensure the current window is active 78 | Window.Current.Activate(); 79 | } 80 | } 81 | 82 | /// 83 | /// Invoked when Navigation to a certain page fails 84 | /// 85 | /// The Frame which failed navigation 86 | /// Details about the navigation failure 87 | void OnNavigationFailed(object sender, NavigationFailedEventArgs e) 88 | { 89 | throw new Exception("Failed to load Page " + e.SourcePageType.FullName); 90 | } 91 | 92 | /// 93 | /// Invoked when application execution is being suspended. Application state is saved 94 | /// without knowing whether the application will be terminated or resumed with the contents 95 | /// of memory still intact. 96 | /// 97 | /// The source of the suspend request. 98 | /// Details about the suspend request. 99 | private void OnSuspending(object sender, SuspendingEventArgs e) 100 | { 101 | var deferral = e.SuspendingOperation.GetDeferral(); 102 | //TODO: Save application state and stop any background activity 103 | deferral.Complete(); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/LockScreenLogo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/LockScreenLogo.scale-200.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/SplashScreen.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/SplashScreen.scale-200.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/Square150x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/Square150x150Logo.scale-200.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/Square44x44Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/Square44x44Logo.scale-200.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/Square44x44Logo.targetsize-24_altform-unplated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/Square44x44Logo.targetsize-24_altform-unplated.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/StoreLogo.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Assets/Wide310x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kiewic/MediaPlayerElementWithHttpClient/964d619da30a179893450cd0dfe90fcb6ea8156c/MediaPlayerElementWithHttpClient/Assets/Wide310x150Logo.scale-200.png -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/HttpRandomAccessStream.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Runtime.InteropServices.WindowsRuntime; 4 | using System.Threading.Tasks; 5 | using Windows.Foundation; 6 | using Windows.Storage.Streams; 7 | using Windows.Web.Http; 8 | 9 | namespace MediaPlayerElementWithHttpClient 10 | { 11 | class HttpRandomAccessStream : IRandomAccessStreamWithContentType 12 | { 13 | private HttpClient client; 14 | private IInputStream inputStream; 15 | private ulong size; 16 | private ulong requestedPosition; 17 | private string etagHeader; 18 | private string lastModifiedHeader; 19 | private Uri requestedUri; 20 | 21 | // No public constructor, factory methods instead to handle async tasks. 22 | private HttpRandomAccessStream(HttpClient client, Uri uri) 23 | { 24 | this.client = client; 25 | requestedUri = uri; 26 | requestedPosition = 0; 27 | } 28 | 29 | static public IAsyncOperation CreateAsync(HttpClient client, Uri uri) 30 | { 31 | HttpRandomAccessStream randomStream = new HttpRandomAccessStream(client, uri); 32 | 33 | return AsyncInfo.Run(async (cancellationToken) => 34 | { 35 | await randomStream.SendRequesAsync().ConfigureAwait(false); 36 | return randomStream; 37 | }); 38 | } 39 | 40 | private async Task SendRequesAsync() 41 | { 42 | Debug.Assert(inputStream == null); 43 | 44 | HttpRequestMessage request = null; 45 | request = new HttpRequestMessage(HttpMethod.Get, requestedUri); 46 | 47 | request.Headers.Add("Range", String.Format("bytes={0}-", requestedPosition)); 48 | 49 | if (!String.IsNullOrEmpty(etagHeader)) 50 | { 51 | request.Headers.Add("If-Match", etagHeader); 52 | } 53 | 54 | if (!String.IsNullOrEmpty(lastModifiedHeader)) 55 | { 56 | request.Headers.Add("If-Unmodified-Since", lastModifiedHeader); 57 | } 58 | 59 | HttpResponseMessage response = await client.SendRequestAsync( 60 | request, 61 | HttpCompletionOption.ResponseHeadersRead).AsTask().ConfigureAwait(false); 62 | 63 | if (response.Content.Headers.ContentType != null) 64 | { 65 | this.ContentType = response.Content.Headers.ContentType.MediaType; 66 | } 67 | 68 | size = response.Content.Headers.ContentLength.Value; 69 | 70 | if (response.StatusCode != HttpStatusCode.PartialContent && requestedPosition != 0) 71 | { 72 | throw new Exception("HTTP server did not reply with a '206 Partial Content' status."); 73 | } 74 | 75 | if (!response.Headers.ContainsKey("Accept-Ranges")) 76 | { 77 | throw new Exception(String.Format( 78 | "HTTP server does not support range requests: {0}", 79 | "http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.5")); 80 | } 81 | 82 | if (String.IsNullOrEmpty(etagHeader) && response.Headers.ContainsKey("ETag")) 83 | { 84 | etagHeader = response.Headers["ETag"]; 85 | } 86 | 87 | if (String.IsNullOrEmpty(lastModifiedHeader) && response.Content.Headers.ContainsKey("Last-Modified")) 88 | { 89 | lastModifiedHeader = response.Content.Headers["Last-Modified"]; 90 | } 91 | if (response.Content.Headers.ContainsKey("Content-Type")) 92 | { 93 | contentType = response.Content.Headers["Content-Type"]; 94 | } 95 | 96 | inputStream = await response.Content.ReadAsInputStreamAsync().AsTask().ConfigureAwait(false); 97 | } 98 | 99 | private string contentType = string.Empty; 100 | 101 | public string ContentType 102 | { 103 | get { return contentType; } 104 | private set { contentType = value; } 105 | } 106 | 107 | public bool CanRead 108 | { 109 | get 110 | { 111 | return true; 112 | } 113 | } 114 | 115 | public bool CanWrite 116 | { 117 | get 118 | { 119 | return false; 120 | } 121 | } 122 | 123 | public IRandomAccessStream CloneStream() 124 | { 125 | // If there is only one MediaPlayerElement using the stream, it is safe to return itself. 126 | return this; 127 | } 128 | 129 | public IInputStream GetInputStreamAt(ulong position) 130 | { 131 | throw new NotImplementedException(); 132 | } 133 | 134 | public IOutputStream GetOutputStreamAt(ulong position) 135 | { 136 | throw new NotImplementedException(); 137 | } 138 | 139 | public ulong Position 140 | { 141 | get 142 | { 143 | return requestedPosition; 144 | } 145 | } 146 | 147 | public void Seek(ulong position) 148 | { 149 | if (requestedPosition != position) 150 | { 151 | if (inputStream != null) 152 | { 153 | inputStream.Dispose(); 154 | inputStream = null; 155 | } 156 | Debug.WriteLine("Seek: {0:N0} -> {1:N0}", requestedPosition, position); 157 | requestedPosition = position; 158 | } 159 | } 160 | 161 | public ulong Size 162 | { 163 | get 164 | { 165 | return size; 166 | } 167 | set 168 | { 169 | throw new NotImplementedException(); 170 | } 171 | } 172 | 173 | public void Dispose() 174 | { 175 | if (inputStream != null) 176 | { 177 | inputStream.Dispose(); 178 | inputStream = null; 179 | } 180 | } 181 | 182 | public Windows.Foundation.IAsyncOperationWithProgress ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) 183 | { 184 | return AsyncInfo.Run(async (cancellationToken, progress) => 185 | { 186 | progress.Report(0); 187 | 188 | try 189 | { 190 | if (inputStream == null) 191 | { 192 | await SendRequesAsync().ConfigureAwait(false); 193 | } 194 | } 195 | catch (Exception ex) 196 | { 197 | Debug.WriteLine(ex); 198 | throw; 199 | } 200 | 201 | IBuffer result = await inputStream.ReadAsync(buffer, count, options).AsTask(cancellationToken, progress).ConfigureAwait(false); 202 | 203 | // Move position forward. 204 | requestedPosition += result.Length; 205 | Debug.WriteLine("requestedPosition = {0:N0}", requestedPosition); 206 | 207 | return result; 208 | }); 209 | } 210 | 211 | public Windows.Foundation.IAsyncOperation FlushAsync() 212 | { 213 | throw new NotImplementedException(); 214 | } 215 | 216 | public Windows.Foundation.IAsyncOperationWithProgress WriteAsync(IBuffer buffer) 217 | { 218 | throw new NotImplementedException(); 219 | } 220 | } 221 | } -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/MainPage.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/MainPage.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Windows.UI.Xaml.Controls; 3 | using Windows.Web.Http; 4 | 5 | // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 6 | 7 | namespace MediaPlayerElementWithHttpClient 8 | { 9 | /// 10 | /// An empty page that can be used on its own or navigated to within a Frame. 11 | /// 12 | public sealed partial class MainPage : Page 13 | { 14 | private MediaPlayerElement mediaPlayerElement; 15 | 16 | public MainPage() 17 | { 18 | this.InitializeComponent(); 19 | StartMediaPlayerElement(); 20 | } 21 | 22 | private async void StartMediaPlayerElement() 23 | { 24 | HttpClient client = new HttpClient(); 25 | 26 | // Add custom headers or credentials. 27 | client.DefaultRequestHeaders.Add("Foo", "Bar"); 28 | 29 | // Authenticate with username 'foo' and password 'bar'. 30 | client.DefaultRequestHeaders.Add("Authorization", "Basic Zm9vOmJhcg=="); 31 | 32 | // Try any of the following Uris (audio or video). 33 | Uri uri = new Uri("http://heyhttp.org/song.mp3?basic=1&user=foo&password=bar&lastModified=true"); 34 | //Uri uri = new Uri("http://video.ch9.ms/ch9/70cc/83e17e76-8be8-441b-b469-87cf0e6a70cc/ASPNETwithScottHunter_high.mp4"); 35 | 36 | HttpRandomAccessStream stream = await HttpRandomAccessStream.CreateAsync(client, uri); 37 | 38 | mediaPlayerElement = new MediaPlayerElement(); 39 | mediaPlayerElement.AreTransportControlsEnabled = true; 40 | mediaPlayerElement.AutoPlay = true; 41 | this.Content = mediaPlayerElement; 42 | mediaPlayerElement.SetMediaPlayer(new Windows.Media.Playback.MediaPlayer()); 43 | mediaPlayerElement.Source = Windows.Media.Core.MediaSource.CreateFromStream(stream, stream.ContentType); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/MediaPlayerElementWithHttpClient.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | x86 7 | {8856A648-D019-43B1-BC82-901B2617DD8A} 8 | AppContainerExe 9 | Properties 10 | MediaPlayerElementWithHttpClient 11 | MediaPlayerElementWithHttpClient 12 | en-US 13 | UAP 14 | 10.0.14393.0 15 | 10.0.14393.0 16 | 14 17 | 512 18 | {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 19 | MediaPlayerElementWithHttpClient_TemporaryKey.pfx 20 | 21 | 22 | true 23 | bin\x86\Debug\ 24 | DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP 25 | ;2008 26 | full 27 | x86 28 | false 29 | prompt 30 | true 31 | 32 | 33 | bin\x86\Release\ 34 | TRACE;NETFX_CORE;WINDOWS_UWP 35 | true 36 | ;2008 37 | pdbonly 38 | x86 39 | false 40 | prompt 41 | true 42 | true 43 | 44 | 45 | true 46 | bin\ARM\Debug\ 47 | DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP 48 | ;2008 49 | full 50 | ARM 51 | false 52 | prompt 53 | true 54 | 55 | 56 | bin\ARM\Release\ 57 | TRACE;NETFX_CORE;WINDOWS_UWP 58 | true 59 | ;2008 60 | pdbonly 61 | ARM 62 | false 63 | prompt 64 | true 65 | true 66 | 67 | 68 | true 69 | bin\x64\Debug\ 70 | DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP 71 | ;2008 72 | full 73 | x64 74 | false 75 | prompt 76 | true 77 | 78 | 79 | bin\x64\Release\ 80 | TRACE;NETFX_CORE;WINDOWS_UWP 81 | true 82 | ;2008 83 | pdbonly 84 | x64 85 | false 86 | prompt 87 | true 88 | true 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | App.xaml 97 | 98 | 99 | 100 | MainPage.xaml 101 | 102 | 103 | 104 | 105 | 106 | Designer 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | MSBuild:Compile 123 | Designer 124 | 125 | 126 | MSBuild:Compile 127 | Designer 128 | 129 | 130 | 131 | 14.0 132 | 133 | 134 | 141 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 8 | 9 | 13 | 14 | 15 | 16 | 17 | MediaPlayerElementWithHttpClient 18 | gistanki 19 | Assets\StoreLogo.png 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 34 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("MediaPlayerElementWithHttpClient")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("MediaPlayerElementWithHttpClient")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Version information for an assembly consists of the following four values: 18 | // 19 | // Major Version 20 | // Minor Version 21 | // Build Number 22 | // Revision 23 | // 24 | // You can specify all the values or you can default the Build and Revision Numbers 25 | // by using the '*' as shown below: 26 | // [assembly: AssemblyVersion("1.0.*")] 27 | [assembly: AssemblyVersion("1.0.0.0")] 28 | [assembly: AssemblyFileVersion("1.0.0.0")] 29 | [assembly: ComVisible(false)] -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/Properties/Default.rd.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /MediaPlayerElementWithHttpClient/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0" 4 | }, 5 | "frameworks": { 6 | "uap10.0": {} 7 | }, 8 | "runtimes": { 9 | "win10-arm": {}, 10 | "win10-arm-aot": {}, 11 | "win10-x86": {}, 12 | "win10-x86-aot": {}, 13 | "win10-x64": {}, 14 | "win10-x64-aot": {} 15 | } 16 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MediaPlayerElementWithHttpClient 2 | 3 | ### TL;DR 4 | 5 | > Use `Windows.UI.Xaml.Controls.MediaPlayerElement` with `Windows.Web.Http.HttpClient`. 6 | 7 | ### Docs 8 | 9 | [`MediaPlayerElement`][mediaplayerelement] is a control to play audio and video in Windows Universal/Store apps. 10 | 11 | However, when the audio or video is served from an internet server, using [`HttpClient`][httpclient] is a better choice to handle the request because you can provide: 12 | 13 | * Authentication credentials. 14 | * Custom headers. 15 | * Etc. 16 | 17 | The `HttpRandomAccessStream` class is a wrapper on top of `HttpClient` that can stream content from the internet and can be consumed as an `IRandomAcessStream` or `IRandomAccessStreamWithContentType`. 18 | 19 | ### Example 20 | 21 | MediaPlayerElement mediaPlayerElement = new MediaPlayerElement(); 22 | 23 | HttpClient client = new HttpClient(); 24 | 25 | // Add custom headers, credentials, etc. 26 | client.DefaultRequestHeaders.Add("Foo", "Bar"); 27 | 28 | Uri uri = new Uri("http://video.ch9.ms/ch9/70cc/83e17e76-8be8-441b-b469-87cf0e6a70cc/ASPNETwithScottHunter_high.mp4"); 29 | 30 | HttpRandomAccessStream stream = await HttpRandomAccessStream.CreateAsync(client, uri); 31 | 32 | mediaPlayerElement.SetMediaPlayer(new MediaPlayer()); 33 | mediaPlayerElement.Source = MediaSource.CreateFromStream(stream, stream.ContentType); 34 | 35 | 36 | **Note:** The server must support HTTP [Range](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.5) headers. 37 | 38 | ### Feedback 39 | 40 | Please give it a try and provide feedback. 41 | 42 | 43 | [mediaplayerelement]: https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.mediaplayerelement 44 | [httpclient]: https://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.httpclient.aspx 45 | -------------------------------------------------------------------------------- /WithoutHtttpClient.md: -------------------------------------------------------------------------------- 1 | `Windows.UI.Xaml.Controls.MediaPlayerElement` without `HttpClient` sample: 2 | 3 | mediaPlayer = new MediaPlayerElement(); 4 | mediaPlayer.AutoPlay = true; 5 | this.Content = mediaPlayer; 6 | 7 | Uri uri = new Uri("http://video.ch9.ms/ch9/70cc/83e17e76-8be8-441b-b469-87cf0e6a70cc/ASPNETwithScottHunter_high.mp4"); 8 | 9 | mediaPlayer.SetMediaPlayer(new Windows.Media.Playback.MediaPlayer()); 10 | mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromUri(uri); 11 | 12 | Easy! 13 | --------------------------------------------------------------------------------