2021-10-03 16:08:47 +00:00
< img src = "icon.png" width = "128" >
2021-10-03 15:53:58 +00:00
2017-04-12 09:29:41 +00:00
MpcNET
2016-11-30 13:53:37 +00:00
===========
2021-10-03 16:08:47 +00:00
Pure .NET Client Library for [**Music Player Daemon** ](https://www.musicpd.org/ ) Servers.
The heart and soul of [Stylophone ](https://github.com/Difegue/Stylophone ).
2017-04-11 14:59:19 +00:00
2021-10-03 19:28:12 +00:00
[**Nightly Packages** ](https://github.com/Difegue/MpcNET/packages/ ) - [**Stable Versions** ](https://www.nuget.org/packages/MpcNET/ )
2017-04-11 14:59:19 +00:00
## Usage
2021-10-03 19:28:12 +00:00
2017-04-11 14:59:19 +00:00
### Connection
2017-04-12 10:41:58 +00:00
To create a client for MPD, you must first create a `IPEndPoint` for the Server with the right IP and Port.
````C#
var mpdEndpoint = new IPEndPoint(IPAddress.Loopback, 6600);
````
Then create a Client and Connect to MPD.
````C#
2021-10-03 18:57:28 +00:00
var client = new MpcConnection(mpdEndpoint);
2017-04-12 10:41:58 +00:00
var connected = await client.ConnectAsync();
````
The `ConnectAsync()` method is returning a bool to indicate if the connection was successfully. However, this can be queried directly on the Client also:
````C#
var isConnected = client.IsConnected;
````
and for MPD version, additional property is available:
````C#
var mpdVersion = client.Version
````
To disconnect the Client use the follow method:
````C#
await client.DisconnectAsync();
````
2021-10-03 18:57:28 +00:00
or just dispose the client:
````C#
client.Dispose();
````
2017-04-11 14:59:19 +00:00
### Send Command
2021-10-03 18:57:28 +00:00
````C#
using (var client = new MpcConnection(mpdEndpoint)) {
await client.ConnectAsync();
// Look in /Commands to see everything that implements IMpcCommand
var request = await client.SendAsync(new IMpcCommand< List < string > >(parameters));
if (!request.IsResponseValid) {
var mpdError = request.Response?.Result?.MpdError;
if (mpdError != null & & mpdError != "")
Console.WriteLine($"Error: {mpdError}");
else
Console.WriteLine($"Invalid server response: {response}.");
} else {
List< string > response = request.Response.Content;
// do stuff
}
}
````
### Command Lists
````C#
var commandList = new CommandList();
commandList.Add(new IMpcCommand< ?>(firstCommandArgument);
commandList.Add(new IMpcCommand< ?>(secondCommandArgument);
commandList.Add(new IMpcCommand< ?>(thirdCommandArgument);
using (var client = new MpcConnection(mpdEndpoint)) {
await client.ConnectAsync();
var request = await client.SendAsync(commandList);
// Response string contains responses of all the commands, split by commas
string response = request.Response?.Content;
}
````
### Binary Responses
````C#
// Get albumart from MPD
List< byte > data = new List< byte > ();
using (var client = new MpcConnection(mpdEndpoint))
{
long totalBinarySize = 9999;
long currentSize = 0;
do
{
var albumReq = await client.SendAsync(new AlbumArtCommand(f.Path, currentSize));
if (!albumReq.IsResponseValid) break;
var response = albumReq.Response.Content;
if (response.Binary == 0) break; // MPD isn't giving us any more data, let's roll with what we have.
totalBinarySize = response.Size;
currentSize += response.Binary;
data.AddRange(response.Data);
Debug.WriteLine($"Downloading albumart: {currentSize}/{totalBinarySize}");
} while (currentSize < totalBinarySize ) ;
}
````