-
Notifications
You must be signed in to change notification settings - Fork 131
chore: update MediaElement docs to include custom http headers feature #639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -153,6 +153,38 @@ By default, the media that is defined by the `Source` property doesn't immediate | |||||||||||||||||
|
|
||||||||||||||||||
| Platform provided media playback controls are enabled by default, and can be disabled by setting the `ShouldShowPlaybackControls` property to `false`. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Play remote media with custom HTTP headers | ||||||||||||||||||
|
|
||||||||||||||||||
| A `MediaElement` can send custom HTTP headers when loading remote media. This is useful for authenticated streams that require an `Authorization` header or other custom headers. | ||||||||||||||||||
|
|
||||||||||||||||||
| ```csharp | ||||||||||||||||||
| mediaElement.Source = new UriMediaSource | ||||||||||||||||||
| { | ||||||||||||||||||
| Uri = new Uri("https://example.com/stream.m3u8"), | ||||||||||||||||||
| HttpHeaders = new Dictionary<string, string> | ||||||||||||||||||
| { | ||||||||||||||||||
| ["Authorization"] = "Bearer my-token" | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Or using the `MediaSource.FromUri` overload: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```csharp | ||||||||||||||||||
| var headers = new Dictionary<string, string> | ||||||||||||||||||
| { | ||||||||||||||||||
| ["Authorization"] = "Bearer my-token" | ||||||||||||||||||
| }; | ||||||||||||||||||
| mediaElement.Source = MediaSource.FromUri( | ||||||||||||||||||
| new Uri("https://example.com/stream.m3u8"), headers); | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| > [!NOTE] | ||||||||||||||||||
| > HTTP headers are applied to all HTTP requests made for the media source, including manifest and segment downloads for adaptive streams (HLS/DASH). This feature is supported on Android, iOS, macOS, and Windows. Tizen does not support custom HTTP headers. | ||||||||||||||||||
|
|
||||||||||||||||||
| > [!IMPORTANT] | ||||||||||||||||||
| > On iOS and macOS, custom HTTP headers are set via the `AVURLAssetHTTPHeaderFieldsKey` option, which requires iOS 16.0+ / macOS 13.0+. On earlier versions, custom headers will not be applied. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Use Rich Media Notifications | ||||||||||||||||||
| A `MediaElement` can show rich media notifications on Android, iOS, Mac Catalyst, and Windows when media is playing in the background. To enable rich media notifications, the following steps are required: | ||||||||||||||||||
| 1. Enable background video playback by setting the `enableForegroundService` parameter to `true` when calling the `UseMauiCommunityToolkitMediaElement` method in *MauiProgram.cs*. Refer to the **Initializing the package** section above for more information. | ||||||||||||||||||
|
|
@@ -241,10 +273,11 @@ An example of how to use this syntax in XAML can be seen below. | |||||||||||||||||
|
|
||||||||||||||||||
| ## Understand MediaSource types | ||||||||||||||||||
|
|
||||||||||||||||||
| A `MediaElement` can play media by setting its `Source` property to a remote or local media file. The `Source` property is of type `MediaSource`, and this class defines three static methods: | ||||||||||||||||||
| A `MediaElement` can play media by setting its `Source` property to a remote or local media file. The `Source` property is of type `MediaSource`, and this class defines the following static methods: | ||||||||||||||||||
|
|
||||||||||||||||||
| - `FromFile`, returns a `FileMediaSource` instance from a `string` argument. | ||||||||||||||||||
| - `FromUri`, returns a `UriMediaSource` instance from a `Uri` argument. | ||||||||||||||||||
| - `FromUri(Uri, IDictionary<string, string>)`, returns a `UriMediaSource` instance from a `Uri` argument with custom HTTP headers applied to all requests. | ||||||||||||||||||
| - `FromResource`, returns a `ResourceMediaSource` instance from a `string` argument. | ||||||||||||||||||
|
Comment on lines
278
to
281
|
||||||||||||||||||
| - `FromFile`, returns a `FileMediaSource` instance from a `string` argument. | |
| - `FromUri`, returns a `UriMediaSource` instance from a `Uri` argument. | |
| - `FromUri(Uri, IDictionary<string, string>)`, returns a `UriMediaSource` instance from a `Uri` argument with custom HTTP headers applied to all requests. | |
| - `FromResource`, returns a `ResourceMediaSource` instance from a `string` argument. | |
| - `FromFile(string)`, returns a `FileMediaSource` instance from a `string` argument. | |
| - `FromUri(Uri)`, returns a `UriMediaSource` instance from a `Uri` argument. | |
| - `FromUri(Uri, IDictionary<string, string>)`, returns a `UriMediaSource` instance from a `Uri` argument with custom HTTP headers applied to all requests. | |
| - `FromResource(string)`, returns a `ResourceMediaSource` instance from a `string` argument. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Kaaybi!
To ensure we call
OnSourceChanged()each time a new header is added to the dictionary, I updatedUriSource.Headersto be apublicreadonly-property and to use anObservableDictionaryunder the hood that fires an event any time its content changes.(I.e.
public event EventHandler? ContentsChangednow fires any time something is added to or removed fromUriSource.Headers, and we callOnSourceChanged()when it does.)This means that we can no longer pass in a
DictionarytoUriMediaSource.HttpHeaders.Long story short, we just need to update the docs here to use the
Addmethod instead of passing in aDictionary: