Skip to content

Commit 897d657

Browse files
authored
feat(IVideoDevice): add GetData method (#5967)
* feat: 增加 GetData 方法 * refactor: 增加 getAudioData 方法 * doc: 增加下载按钮 * refactor: 更新实现逻辑 * test: 更新单元测试 * doc: 更新示例 * doc: 更新示例文档
1 parent b7e0afe commit 897d657

File tree

11 files changed

+67
-4
lines changed

11 files changed

+67
-4
lines changed

src/BootstrapBlazor.Server/Components/Samples/AudioDevices.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private IAudioDevice? AudioDeviceService { get; set; }</Pre>
1717
<Button Text="@Localizer["AudioDeviceRequestText"]" Icon="fa-solid fa-microphone" OnClick="OnRequestDevice"></Button>
1818
<Button Text="@Localizer["AudioDeviceOpenText"]" Icon="fa-solid fa-play" OnClick="OnOpen" IsDisabled="_isOpen || string.IsNullOrEmpty(_deviceId)"></Button>
1919
<Button Text="@Localizer["AudioDeviceCloseText"]" Icon="fa-solid fa-stop" OnClick="OnClose" IsDisabled="!_isOpen"></Button>
20+
<Button Text="@Localizer["AudioDeviceDownloadText"]" Icon="fa-solid fa-download" OnClick="OnDownload" IsDisabled="!_isDownload"></Button>
2021
</div>
2122
</div>
2223
<div class="col-12">

src/BootstrapBlazor.Server/Components/Samples/AudioDevices.razor.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public partial class AudioDevices : IAsyncDisposable
1313
[Inject, NotNull]
1414
private IAudioDevice? AudioDeviceService { get; set; }
1515

16+
[Inject, NotNull]
17+
private DownloadService? DownloadService { get; set; }
18+
1619
private readonly List<IMediaDeviceInfo> _devices = [];
1720

1821
private List<SelectedItem> _items = [];
@@ -21,6 +24,8 @@ public partial class AudioDevices : IAsyncDisposable
2124

2225
private bool _isOpen = false;
2326

27+
private bool _isDownload = false;
28+
2429
private async Task OnRequestDevice()
2530
{
2631
var devices = await AudioDeviceService.GetDevices();
@@ -49,8 +54,18 @@ private async Task OnOpen()
4954

5055
private async Task OnClose()
5156
{
52-
_isOpen = false;
5357
await AudioDeviceService.Close(".bb-audio");
58+
_isOpen = false;
59+
_isDownload = true;
60+
}
61+
62+
private async Task OnDownload()
63+
{
64+
var stream = await AudioDeviceService.GetData();
65+
if (stream != null)
66+
{
67+
await DownloadService.DownloadFromStreamAsync($"data_{DateTime.Now:HHmmss}.wav", stream);
68+
}
5469
}
5570

5671
private async Task DisposeAsync(bool disposing)

src/BootstrapBlazor.Server/Components/Samples/VideoDevices.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private async Task OnDownload()
6969
var stream = await VideoDeviceService.GetPreviewData();
7070
if (stream != null)
7171
{
72-
await DownloadService.DownloadFromStreamAsync("preview.png", stream);
72+
await DownloadService.DownloadFromStreamAsync($"preview_{DateTime.Now:HHmmss}.png", stream);
7373
}
7474
}
7575

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7137,6 +7137,9 @@
71377137
"BaseUsageIntro": "Perform different operations by calling different API methods",
71387138
"AudioDeviceRequestText": "List",
71397139
"AudioDeviceOpenText": "Record",
7140-
"AudioDeviceCloseText": "Stop"
7140+
"AudioDeviceCloseText": "Stop",
7141+
"AudioDevicePauseText": "Pause",
7142+
"AudioDeviceResumeText": "Resume",
7143+
"AudioDeviceDownloadText": "Download"
71417144
}
71427145
}

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7139,6 +7139,7 @@
71397139
"AudioDeviceOpenText": "录音",
71407140
"AudioDeviceCloseText": "停止",
71417141
"AudioDevicePauseText": "暂停",
7142-
"AudioDeviceResumeText": "恢复"
7142+
"AudioDeviceResumeText": "恢复",
7143+
"AudioDeviceDownloadText": "下载"
71437144
}
71447145
}

src/BootstrapBlazor/Services/MediaDevices/DefaultAudioDevice.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ public Task<bool> Close(string? selector)
3131
{
3232
return deviceService.Close(selector);
3333
}
34+
35+
public Task<Stream?> GetData()
36+
{
37+
return deviceService.GetAudioData();
38+
}
3439
}

src/BootstrapBlazor/Services/MediaDevices/DefaultMediaDevices.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,16 @@ public async Task<bool> Apply(MediaTrackConstraints constraints)
6262
var module = await LoadModule();
6363
return await module.InvokeAsync<bool>("apply", constraints);
6464
}
65+
66+
public async Task<Stream?> GetAudioData()
67+
{
68+
Stream? ret = null;
69+
var module = await LoadModule();
70+
var stream = await module.InvokeAsync<IJSStreamReference?>("getAudioData");
71+
if (stream != null)
72+
{
73+
ret = await stream.OpenReadStreamAsync(stream.Length);
74+
}
75+
return ret;
76+
}
6577
}

src/BootstrapBlazor/Services/MediaDevices/IAudioDevice.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ public interface IAudioDevice
2929
/// <param name="selector"></param>
3030
/// <returns></returns>
3131
Task<bool> Close(string? selector);
32+
33+
/// <summary>
34+
/// Gets the stream of the audio.
35+
/// </summary>
36+
/// <returns></returns>
37+
Task<Stream?> GetData();
3238
}

src/BootstrapBlazor/Services/MediaDevices/IMediaDevices.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@ public interface IMediaDevices
5555
/// <param name="constraints"></param>
5656
/// <returns></returns>
5757
Task<bool> Apply(MediaTrackConstraints constraints);
58+
59+
/// <summary>
60+
/// Gets the stream of the audio.
61+
/// </summary>
62+
/// <returns></returns>
63+
Task<Stream?> GetAudioData();
5864
}

src/BootstrapBlazor/wwwroot/modules/media.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export async function record(options) {
202202
audio.classList.remove("d-none");
203203
audio.classList.remove("hidden");
204204
audio.removeAttribute("hidden");
205+
media.audioBlob = blob;
205206
}
206207
}
207208
delete media.audioSelector;
@@ -233,3 +234,8 @@ export function stop(selector) {
233234
}
234235
return ret;
235236
}
237+
238+
export function getAudioData() {
239+
const media = registerBootstrapBlazorModule("MediaDevices");
240+
return media.audioBlob
241+
}

0 commit comments

Comments
 (0)