Skip to content

justr-1/snapcast-api

Repository files navigation

.NET Snapcast API Wrapper

This is a .NET wrapper for the Snapcast RPC API.

Target Frameworks

  • netstandard2.0
  • net6.0

Install

NuGet

Install-Package justr.snapcast-api

Usage

Init

Default

var api = new SnapcastApiClient("snapserver.lan", 1780);

With logging

// init logging
var loggerFactory = LoggerFactory.Create(_ => _.AddDebug());
var logger = loggerFactory.CreateLogger<SnapcastApiClient>();

// init api client
var api = new SnapcastApiClient(new() { Host = "snapserver.lan", Port = 1780 }, logger);

With di and logging

// init di
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();

// init api client
var api = serviceProvider.GetService<SnapcastApiClient>();

static void ConfigureServices(ServiceCollection services)
{
    services.AddLogging(_ => _.AddDebug());
    services.AddTransient<SnapcastApiClient>();
    services.AddSingleton(new Connection { Host = "snapserver.lan", Port = 1780 });
}

Requests

Server

api.ServerGetStatus();
api.ServerGetRPCVersion();
api.ServerDeleteClient(clientId);

Client

api.ClientGetStatus(clientId);
api.ClientSetLatency(clientId, latency);
api.ClientSetName(clientId, name);
api.ClientSetMuted(clientId, muted);
api.ClientSetVolume(clientId, percent);
api.ClientChangeVolume(clientId, percentDiff);

Group

api.GroupGetStatus(groupId);
api.GroupSetClients(groupId, clientId1, clientId2);
api.GroupSetClients(groupId, new List<string> { clientId1, clientId2 });
api.GroupSetName(groupId, name);
api.GroupSetMute(groupId, muted);
api.GroupSetStream(groupId, streamId);

Stream

api.StreamAddStream(streamUri); // pipe:///tmp/snapfifo?name=stream1
api.StreamRemoveStream(streamId);

Notifications

api.OnNotification += OnNotification;
await api.StartNotificationListener();

static void OnNotification(object? sender, IParams @params)
{
    Console.WriteLine(@params.GetType());
}

Exceptions

The SnapcastApiException will be thrown if anything goes wrong.

Roadmap

  • API access using http protocol
  • Support Snapcast 0.25 API requests and responses
  • Support notifications
  • Support new Snapcast 0.26 API features (metadata and control api for audio streams)
  • (API access using tcp socket)