Skip to content

Commit

Permalink
Updated Device Lister demo to dispose references.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Apr 16, 2024
1 parent b9554f1 commit 47c2e8b
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@using System.Text.Json.Serialization;
@inject IMediaDevicesService MediaDevicesService
@inject IJSRuntime JSRuntime
@implements IAsyncDisposable

<PageTitle>Media Capture and Streams - Device Lister</PageTitle>

Expand Down Expand Up @@ -67,6 +68,7 @@ else
id: await info.GetDeviceIdAsync()
)
);
await info.DisposeAsync();
}
}
catch (Exception)
Expand All @@ -81,20 +83,20 @@ else

try
{
var mediaStream = await mediaDevices.GetUserMediaAsync(
await using MediaStream mediaStream = await mediaDevices.GetUserMediaAsync(
new MediaStreamConstraints()
{
Audio = true,
Video = true
}
);

var audioTrack = (await mediaStream.GetAudioTracksAsync()).FirstOrDefault();
await using MediaStreamTrack? audioTrack = (await mediaStream.GetAudioTracksAsync()).FirstOrDefault();
if (audioTrack is not null)
{
await audioTrack.StopAsync();
}
var videoTrack = (await mediaStream.GetVideoTracksAsync()).FirstOrDefault();
await using MediaStreamTrack? videoTrack = (await mediaStream.GetVideoTracksAsync()).FirstOrDefault();
if (videoTrack is not null)
{
await videoTrack.StopAsync();
Expand All @@ -105,4 +107,12 @@ else
error = $"Could not load media.";
}
}

public async ValueTask DisposeAsync()
{
if (mediaDevices is not null)
{
await mediaDevices.DisposeAsync();
}
}
}

0 comments on commit 47c2e8b

Please sign in to comment.