Skip to content

Commit edc71cb

Browse files
committed
refactor: 增加 type 区分音视频
1 parent 94359d5 commit edc71cb

File tree

5 files changed

+10
-12
lines changed

5 files changed

+10
-12
lines changed

src/BootstrapBlazor/Services/MediaDevices/DefaultAudioDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DefaultAudioDevice(IMediaDevices deviceService) : IAudioDevice
2424

2525
public Task<bool> Open(MediaTrackConstraints constraints)
2626
{
27-
return deviceService.Open(constraints);
27+
return deviceService.Open("audio", constraints);
2828
}
2929

3030
public Task<bool> Close(string? selector)

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

3030
public async Task<bool> Close(string? selector)

src/BootstrapBlazor/Services/MediaDevices/DefaultVideoDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DefaultVideoDevice(IMediaDevices deviceService) : IVideoDevice
2424

2525
public Task<bool> Open(MediaTrackConstraints constraints)
2626
{
27-
return deviceService.Open(constraints);
27+
return deviceService.Open("video", constraints);
2828
}
2929

3030
public Task<bool> Close(string? selector)

src/BootstrapBlazor/Services/MediaDevices/IMediaDevices.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ public interface IMediaDevices
1919
/// <summary>
2020
/// The open() method of the MediaDevices interface creates a new MediaStream object and starts capturing media from the specified device.
2121
/// </summary>
22+
/// <param name="type">video or audio</param>
2223
/// <param name="constraints"></param>
2324
/// <returns></returns>
24-
Task<bool> Open(MediaTrackConstraints constraints);
25+
Task<bool> Open(string type, MediaTrackConstraints constraints);
2526

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

src/BootstrapBlazor/wwwroot/modules/media.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ export async function enumerateDevices() {
1212
return ret;
1313
}
1414

15-
export async function open(options) {
16-
const isVideo = options.video && options.video !== false;
15+
export async function open(type, options) {
1716
let ret = false;
18-
if (isVideo) {
17+
if (type === "video") {
1918
ret = await openVideoDevice(options);
2019
}
21-
else {
20+
else if (type === "audio") {
2221
ret = await record(options);
2322
}
2423
return ret;
@@ -41,8 +40,7 @@ const openVideoDevice = async options => {
4140
video: {
4241
deviceId: options.deviceId ? { exact: options.deviceId } : null,
4342
facingMode: { ideal: options.facingMode || "environment" }
44-
},
45-
audio: false
43+
}
4644
}
4745

4846
const { videoSelector, width, height } = options;
@@ -163,7 +161,6 @@ const closeStream = stream => {
163161

164162
export async function record(options) {
165163
const constrains = {
166-
video: false,
167164
audio: {
168165
deviceId: options.deviceId ? { exact: options.deviceId } : null
169166
}

0 commit comments

Comments
 (0)