Skip to content

Commit 493e3c0

Browse files
committed
chore: update MediaElement docs to include custom http headers feature
1 parent 064a347 commit 493e3c0

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

docs/maui/views/MediaElement.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,38 @@ By default, the media that is defined by the `Source` property doesn't immediate
153153

154154
Platform provided media playback controls are enabled by default, and can be disabled by setting the `ShouldShowPlaybackControls` property to `false`.
155155

156+
### Play remote media with custom HTTP headers
157+
158+
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.
159+
160+
```csharp
161+
MediaElement.Source = new UriMediaSource
162+
{
163+
Uri = new Uri("https://example.com/stream.m3u8"),
164+
HttpHeaders = new Dictionary<string, string>
165+
{
166+
["Authorization"] = "Bearer my-token"
167+
}
168+
};
169+
```
170+
171+
Or using the `MediaSource.FromUri` overload:
172+
173+
```csharp
174+
var headers = new Dictionary<string, string>
175+
{
176+
["Authorization"] = "Bearer my-token"
177+
};
178+
MediaElement.Source = MediaSource.FromUri(
179+
new Uri("https://example.com/stream.m3u8"), headers);
180+
```
181+
182+
> [!NOTE]
183+
> 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.
184+
185+
> [!IMPORTANT]
186+
> On iOS and macOS, the `AVURLAssetHTTPHeaderFieldsKey` option requires iOS 16.0+ / macOS 13.0+.
187+
156188
### Use Rich Media Notifications
157189
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:
158190
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.
@@ -245,6 +277,7 @@ A `MediaElement` can play media by setting its `Source` property to a remote or
245277

246278
- `FromFile`, returns a `FileMediaSource` instance from a `string` argument.
247279
- `FromUri`, returns a `UriMediaSource` instance from a `Uri` argument.
280+
- `FromUri(Uri, IDictionary<string, string>)`, returns a `UriMediaSource` instance from a `Uri` argument with custom HTTP headers applied to all requests.
248281
- `FromResource`, returns a `ResourceMediaSource` instance from a `string` argument.
249282

250283
In addition, the `MediaSource` class also has implicit operators that return `MediaSource` instances from `string` and `Uri` arguments.
@@ -255,7 +288,7 @@ In addition, the `MediaSource` class also has implicit operators that return `Me
255288
The `MediaSource` class also has these derived classes:
256289

257290
- `FileMediaSource`, which is used to specify a local media file from a `string`. This class has a `Path` property that can be set to a `string`. In addition, this class has implicit operators to convert a `string` to a `FileMediaSource` object, and a `FileMediaSource` object to a `string`.
258-
- `UriMediaSource`, which is used to specify a remote media file from a URI. This class has a `Uri` property that can be set to a `Uri`.
291+
- `UriMediaSource`, which is used to specify a remote media file from a URI. This class has a `Uri` property that can be set to a `Uri`. It also has an `HttpHeaders` property of type `IDictionary<string, string>` that allows setting custom HTTP headers (e.g. `Authorization`) sent with all HTTP requests for this media source, including manifest and segment downloads.
259292
- `ResourceMediaSource`, which is used to specify an embedded file that is provided through the app's resource files. This class has a `Path` property that can be set to a `string`.
260293

261294
> [!NOTE]

0 commit comments

Comments
 (0)