App.razor 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. @code
  2. {
  3. private bool _isInit = false;
  4. private List<byte[]> _chunks = new();
  5. private List<string> _logs = new();
  6. private string _playUrl;
  7. }
  8. @if (_isInit == false)
  9. {
  10. <button @onclick="Init">Init</button>
  11. }
  12. else
  13. {
  14. <button @onclick="Start">Start</button>
  15. <button @onclick="Stop">Stop</button>
  16. <button @onclick="()=>_chunks.Clear()">Clear</button>
  17. for (var index = 0; index < _chunks.Count; index++)
  18. {
  19. var chunk = _chunks[index];
  20. <button @onclick="() => Play(chunk)"> CHUNK-@index-@(chunk.Length)</button>
  21. }
  22. <audio src="@_playUrl" controls autoplay>
  23. <source src="@_playUrl" type="audio/webm;codecs=OPUS" />
  24. </audio>
  25. }
  26. <hr />
  27. @foreach (var s in _logs)
  28. {
  29. <div>
  30. <pre>@s</pre>
  31. </div>
  32. }
  33. @code
  34. {
  35. protected override async Task OnAfterRenderAsync(bool firstRender)
  36. {
  37. await base.OnAfterRenderAsync(firstRender);
  38. if (firstRender)
  39. {
  40. SvcModule.OnInit += VoiceInit;
  41. SvcModule.ChunkArrive += ChunkArrive;
  42. }
  43. }
  44. async Task Init()
  45. {
  46. try
  47. {
  48. _isInit = await SvcModule.Init();
  49. }
  50. catch (Exception e)
  51. {
  52. _logs.Add(e.ToString());
  53. }
  54. StateHasChanged();
  55. }
  56. private void VoiceInit()
  57. {
  58. }
  59. async Task Start()
  60. {
  61. try
  62. {
  63. await SvcModule.Start();
  64. }
  65. catch (Exception e)
  66. {
  67. _logs.Add(e.ToString());
  68. }
  69. StateHasChanged();
  70. }
  71. private void ChunkArrive(byte[] obj)
  72. {
  73. _chunks.Add(obj);
  74. StateHasChanged();
  75. }
  76. async Task Stop()
  77. {
  78. await SvcModule.Stop();
  79. }
  80. private async Task Play(byte[] chunk)
  81. {
  82. _playUrl = await SvcModule.CreateBlob(chunk);
  83. StateHasChanged();
  84. }
  85. }