123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- @code
- {
- private bool _isInit = false;
- private List<byte[]> _chunks = new();
- private List<string> _logs = new();
- private string _playUrl;
- }
- @if (_isInit == false)
- {
- <button @onclick="Init">Init</button>
- }
- else
- {
- <button @onclick="Start">Start</button>
- <button @onclick="Stop">Stop</button>
- <button @onclick="()=>_chunks.Clear()">Clear</button>
- for (var index = 0; index < _chunks.Count; index++)
- {
- var chunk = _chunks[index];
- <button @onclick="() => Play(chunk)"> CHUNK-@index-@(chunk.Length)</button>
- }
- <audio src="@_playUrl" controls autoplay>
- <source src="@_playUrl" type="audio/webm;codecs=OPUS" />
- </audio>
- }
- <hr />
- @foreach (var s in _logs)
- {
- <div>
- <pre>@s</pre>
- </div>
- }
- @code
- {
- protected override async Task OnAfterRenderAsync(bool firstRender)
- {
- await base.OnAfterRenderAsync(firstRender);
- if (firstRender)
- {
- SvcModule.OnInit += VoiceInit;
- SvcModule.ChunkArrive += ChunkArrive;
- }
- }
- async Task Init()
- {
- try
- {
- _isInit = await SvcModule.Init();
- }
- catch (Exception e)
- {
- _logs.Add(e.ToString());
- }
- StateHasChanged();
- }
- private void VoiceInit()
- {
- }
- async Task Start()
- {
- try
- {
- await SvcModule.Start();
- }
- catch (Exception e)
- {
- _logs.Add(e.ToString());
- }
- StateHasChanged();
- }
- private void ChunkArrive(byte[] obj)
- {
- _chunks.Add(obj);
- StateHasChanged();
- }
- async Task Stop()
- {
- await SvcModule.Stop();
- }
- private async Task Play(byte[] chunk)
- {
- _playUrl = await SvcModule.CreateBlob(chunk);
- StateHasChanged();
- }
- }
|