Snail.Toolkit.Blazor.Extensions.Microphone 1.0.1

dotnet add package Snail.Toolkit.Blazor.Extensions.Microphone --version 1.0.1
                    
NuGet\Install-Package Snail.Toolkit.Blazor.Extensions.Microphone -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Snail.Toolkit.Blazor.Extensions.Microphone" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Snail.Toolkit.Blazor.Extensions.Microphone" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Snail.Toolkit.Blazor.Extensions.Microphone" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Snail.Toolkit.Blazor.Extensions.Microphone --version 1.0.1
                    
#r "nuget: Snail.Toolkit.Blazor.Extensions.Microphone, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Snail.Toolkit.Blazor.Extensions.Microphone@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Snail.Toolkit.Blazor.Extensions.Microphone&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Snail.Toolkit.Blazor.Extensions.Microphone&version=1.0.1
                    
Install as a Cake Tool

Toolkit.Blazor.Extensions.Microphone

A comprehensive audio handling extension for Blazor applications, providing easy-to-use components for microphone capture and audio playback.

Installation

dotnet add package Snail.Toolkit.Blazor.Extensions.Microphone

Features

  • Microphone Capture: Access device microphone with simple API
  • Audio Playback: Stream PCM audio data to browser
  • Reactive Streams: Observable-based audio data handling
  • Flexible Buffering: Choose between streamed or complete audio capture
  • Cross-Platform: Works in all modern browsers supporting Web Audio API

Basic Usage

Microphone Capture Component

<TMicrophone @ref="microphone" 
             StreamBytes="HandleAudioStream"
             Enable="@isRecording">
    <button @onclick="ToggleRecording">
        @(isRecording ? "Stop" : "Start") Recording
    </button>
</TMicrophone>

@code {
    private TMicrophone? microphone;
    private bool isRecording;
    
    private void HandleAudioStream(IObservable<byte[]> audioStream)
    {
        audioStream.Subscribe(chunk => {
            // Process audio chunks in real-time
        });
    }
    
    private async Task ToggleRecording()
    {
        isRecording = !isRecording;
        await microphone?.Process();
    }
}

Audio Playback Component

<TAudioPlayback Bytes="@audioData" AutoPlay="true"/>

@code {
    private byte[] audioData = /* your PCM audio data */;
}

Advanced Usage

Complete Audio Capture with Buffer

<TMicrophone Bytes="HandleCompleteRecording" 
             Enable="@isRecording">
    
    <div class="@(isRecording ? "recording" : "")">
        <span>@duration.ToString(@"mm\:ss")</span>
    </div>
</TMicrophone>

@code {
    private TimeSpan duration;
    private bool isRecording;
    
    private async Task HandleCompleteRecording(byte[] completeAudio)
    {
        // Process full audio recording
        duration = CalculateAudioDuration(completeAudio);
    }
}

Custom Audio Processing Pipeline

<TMicrophone @ref="microphone" 
             StreamBytes="ConfigureProcessing">
    
</TMicrophone>

@code {
    private TMicrophone? microphone;
    
    private void ConfigureProcessing(IObservable<byte[]> audioStream)
    {
        audioStream
            .Buffer(TimeSpan.FromMilliseconds(500))
            .Subscribe(bufferedChunks => {
                // Process buffered audio
            });
    }
}

Component API Reference

TMicrophone

Property Type Description
StreamBytes EventCallback<IObservable<byte[]>> Callback providing audio data stream
Bytes EventCallback<byte[]> Callback for complete audio buffer
Enable bool Controls microphone state
ChildContent RenderFragment<(EventCallback, bool)> Template with (toggle callback, current state)

TAudioPlayback

Property Type Description
StreamBytes IObservable<byte[]> Observable stream of audio data
Bytes byte[] Complete audio data for playback
AutoPlay bool Automatically start playback
ChildContent RenderFragment<(EventCallback, bool)> Template with playback controls

Best Practices

  1. Audio Formats: Use 16-bit PCM @ 48kHz for best compatibility
  2. Memory Management:
    • Dispose components properly
    • Use streaming for long recordings
  3. Permissions: Handle microphone permission prompts gracefully
  4. Error Handling: Implement error callbacks for device access issues

Browser Support

Requires browsers with:

  • Web Audio API support
  • MediaDevices.getUserMedia() support
  • ES6 modules

License

Toolkit.Blazor.Extensions.Microphone is a free and open source project, released under the permissible MIT license.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 329 6/12/2025
1.0.0 164 6/4/2025