KristofferStrube.Blazor.MediaStreamRecording
0.1.0-alpha.2
Prefix Reserved
dotnet add package KristofferStrube.Blazor.MediaStreamRecording --version 0.1.0-alpha.2
NuGet\Install-Package KristofferStrube.Blazor.MediaStreamRecording -Version 0.1.0-alpha.2
<PackageReference Include="KristofferStrube.Blazor.MediaStreamRecording" Version="0.1.0-alpha.2" />
paket add KristofferStrube.Blazor.MediaStreamRecording --version 0.1.0-alpha.2
#r "nuget: KristofferStrube.Blazor.MediaStreamRecording, 0.1.0-alpha.2"
// Install KristofferStrube.Blazor.MediaStreamRecording as a Cake Addin #addin nuget:?package=KristofferStrube.Blazor.MediaStreamRecording&version=0.1.0-alpha.2&prerelease // Install KristofferStrube.Blazor.MediaStreamRecording as a Cake Tool #tool nuget:?package=KristofferStrube.Blazor.MediaStreamRecording&version=0.1.0-alpha.2&prerelease
Blazor.MediaStreamRecording
A Blazor wrapper for the MediaStream Recording browser API.
This Web API makes it easy to record sound from a MediaStream
.
It additionally makes it possible to query which encodings the current platform has available for recording.
This project implements a wrapper around the API for Blazor so that we can easily and safely record sound in the browser.
Demo
The sample project can be demoed at https://kristofferstrube.github.io/Blazor.MediaStreamRecording/
On each page, you can find the corresponding code for the example in the top right corner.
On the API Coverage Status page, you can see how much of the WebIDL specs this wrapper has covered.
Getting Started
Prerequisites
You need to install .NET 7.0 or newer to use the library.
Installation
You can install the package via NuGet with the Package Manager in your IDE or alternatively using the command line:
dotnet add package KristofferStrube.Blazor.MediaStreamRecording
Usage
The package can be used in Blazor WebAssembly and Blazor Server projects.
Import
You need to reference the package in order to use it in your pages. This can be done in _Import.razor
by adding the following.
@using KristofferStrube.Blazor.MediaStreamRecording
Recording MediaStream
The primary purpose of the API is to record some MediaStream
. You can get a MediaStream
using the Blazor.MediaCaptureStreams library. After this you can use the following minimal sample for recording a MediaStream
.
// List to collect each recording part.
List<Blob> blobsRecorded = new();
// Create new MediaRecorder from some existing MediaStream.
await using MediaRecorder recorder = await MediaRecorder.CreateAsync(JSRuntime, mediaStream);
// Add event listener for when each data part is available.
await using var dataAvailableEventListener =
await EventListener<BlobEvent>.CreateAsync(JSRuntime, async (BlobEvent e) =>
{
Blob blob = await e.GetDataAsync();
blobsRecorded.Add(blob);
});
await recorder.AddOnDataAvailableEventListenerAsync(dataAvailableEventListener);
// Starts Recording
await recorder.StartAsync();
// Records for 5 seconds.
await Task.Delay(5000);
// Stops recording
await recorder.StopAsync();
// Combines and collects the total audio data.
Blob combinedBlob = await Blob.CreateAsync(JSRuntime, [.. blobsRecorded]);
byte[] audioData = await combinedBlob.ArrayBufferAsync();
await combinedBlob.JSReference.DisposeAsync();
// Dispose of blob parts created while recording.
foreach (Blob blob in blobsRecorded)
await blob.JSReference.DisposeAsync();
Related repositories
The library uses the following other packages to support its features:
- https://github.com/KristofferStrube/Blazor.WebIDL (To make error handling JSInterop)
- https://github.com/KristofferStrube/Blazor.DOM (To implement
MediaRecorder
which extendsEventTarget
) - https://github.com/KristofferStrube/Blazor.Window (For the
ErrorEvent
type that is also exposed via theonError
event handler onMediaRecorder
) - https://github.com/KristofferStrube/Blazor.MediaCaptureStreams (To enable the capturing of the
MediaStream
s that should be recorded)
Related articles
This repository was built with inspiration and help from the following series of articles:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- KristofferStrube.Blazor.FileAPI (>= 0.4.0)
- KristofferStrube.Blazor.MediaCaptureStreams (>= 0.4.0)
- KristofferStrube.Blazor.Window (>= 0.1.0-alpha.6)
-
net8.0
- KristofferStrube.Blazor.FileAPI (>= 0.4.0)
- KristofferStrube.Blazor.MediaCaptureStreams (>= 0.4.0)
- KristofferStrube.Blazor.Window (>= 0.1.0-alpha.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on KristofferStrube.Blazor.MediaStreamRecording:
Repository | Stars |
---|---|
KristofferStrube/Blazor.WebAudio
A Blazor wrapper for the Web Audio browser API.
|
Version | Downloads | Last updated |
---|---|---|
0.1.0-alpha.2 | 60 | 10/29/2024 |
0.1.0-alpha.1 | 172 | 8/26/2024 |