Browse Source

support safari(mp4a)

HOME 2 years ago
parent
commit
79840771f3

+ 11 - 1
SimpleWebChat.BlazorWasm/UI/VoiceChat.razor

@@ -3,6 +3,7 @@
 @inject HttpClient http
 @code {
     private bool ready = false;
+    private string msg = "← Click it";
 
     private string num = "";
     private List<string> logs = new();
@@ -18,6 +19,7 @@
         <div class="row m-2">
             <div class="col-12">
                 <button @onclick="@Init" class="btn btn-primary">Open Microphone</button>
+                <span>@msg</span>
             </div>
         </div>
     }
@@ -79,7 +81,15 @@
 
         private async Task Init()
         {
-            ready = await AudioCaptureModule.Init();
+            try
+            {
+                ready = await AudioCaptureModule.Init();
+            }
+            catch (Exception e)
+            {
+                msg = e.Message;
+            }
+
             if (ready)
             {
                 AudioCaptureModule.ChunkArrive += SendChunk;

+ 9 - 1
SimpleWebChat.BlazorWasm/wwwroot/audio-capture-module.js

@@ -12,9 +12,17 @@ export async function init() {
     else throw "failure to getUserMedia";
 
     if (stream) {
+
+        var mime;
+        if (MediaRecorder.isTypeSupported('audio/webm;codecs=OPUS')) {
+            mime = 'audio/webm;codecs=OPUS';
+        } else if (MediaRecorder.isTypeSupported("audio/mp4;codecs=mp4a.40.2")) {
+            mime = "audio/mp4;codecs=mp4a.40.2";
+        } else throw "No support audio recording codec in OPUS or MP4A";
+
         recorder = new MediaRecorder(stream, {
             audioBitsPerSecond: 64000,
-            mimeType: 'audio/webm;codecs=OPUS'
+            mimeType: mime
         });
 
         recorder.ondataavailable = async (r) => {