Skip to content

Commit 62d32b5

Browse files
committed
refactor: open 方法增加返回值
1 parent 95e0dce commit 62d32b5

5 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/BootstrapBlazor/Services/MediaDevices/DefaultMediaDevices.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ private async Task<JSModule> LoadModule()
2121
return await module.InvokeAsync<List<MediaDeviceInfo>?>("enumerateDevices");
2222
}
2323

24-
public async Task Open(MediaTrackConstraints constraints)
24+
public async Task<bool> Open(MediaTrackConstraints constraints)
2525
{
2626
var module = await LoadModule();
27-
await module.InvokeVoidAsync("open", constraints);
27+
return await module.InvokeAsync<bool>("open", constraints);
2828
}
2929

3030
public async Task Close(string? videoSelector)

src/BootstrapBlazor/Services/MediaDevices/DefaultVideoDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DefaultVideoDevice(IMediaDevices deviceService) : IVideoDevice
2222
return ret;
2323
}
2424

25-
public Task Open(MediaTrackConstraints constraints)
25+
public Task<bool> Open(MediaTrackConstraints constraints)
2626
{
2727
return deviceService.Open(constraints);
2828
}

src/BootstrapBlazor/Services/MediaDevices/IMediaDevices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IMediaDevices
2121
/// </summary>
2222
/// <param name="constraints"></param>
2323
/// <returns></returns>
24-
Task Open(MediaTrackConstraints constraints);
24+
Task<bool> Open(MediaTrackConstraints constraints);
2525

2626
/// <summary>
2727
/// The close() method of the MediaDevices interface stops capturing media from the specified device and closes the MediaStream object.

src/BootstrapBlazor/Services/MediaDevices/IVideoDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IVideoDevice
2121
/// </summary>
2222
/// <param name="constraints"></param>
2323
/// <returns></returns>
24-
Task Open(MediaTrackConstraints constraints);
24+
Task<bool> Open(MediaTrackConstraints constraints);
2525

2626
/// <summary>
2727
/// Close the video device with the specified selector.

src/BootstrapBlazor/wwwroot/modules/media.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@ export async function open(options) {
2929
if (height) {
3030
constrains.video.height = { ideal: height };
3131
}
32-
const stream = await navigator.mediaDevices.getUserMedia(constrains);
33-
const media = registerBootstrapBlazorModule("MediaDevices");
34-
media.stream = stream;
3532

36-
if (videoSelector) {
37-
const video = document.querySelector(videoSelector);
38-
if (video) {
39-
video.srcObject = stream;
33+
let ret = false;
34+
try {
35+
const stream = await navigator.mediaDevices.getUserMedia(constrains);
36+
const media = registerBootstrapBlazorModule("MediaDevices");
37+
media.stream = stream;
38+
39+
if (videoSelector) {
40+
const video = document.querySelector(videoSelector);
41+
if (video) {
42+
video.srcObject = stream;
43+
}
4044
}
45+
ret = true;
46+
}
47+
catch (err) {
48+
console.error("Error accessing media devices.", err);
4149
}
50+
return ret;
4251
}
4352

4453
export async function close(videoSelector) {

0 commit comments

Comments
 (0)