flofaber/mphpd

音乐播放器守护进程的PHP库

v1.2.6 2024-09-14 11:41 UTC

README

MphpD 是音乐播放器守护进程的库。它允许您以简单且面向对象的方式在您的PHP应用程序中控制MPD,同时处理转义、解析、错误报告以及所有其他痛苦的事情。

支持整个MPD 协议

警告

此库及其文档相对较新,因此可能并不完美。

请在此处报告您遇到的任何错误或问题 here。谢谢。

安装

您可以通过使用composer安装此库

composer require flofaber/mphpd

然后自动加载它

require_once __DIR__ . "/vendor/autoload.php";

或者简单地 下载 它并将其包含在您的代码中,如下所示

require_once __DIR__ . "/MphpD/MphpD.php";

使用方法

创建一个新的MphpD实例

use FloFaber\MphpD\MphpD;
use FloFaber\MphpD\MPDException;

$mphpd = new MphpD([
  "host" => "127.0.0.1",
  "port" => 6600,
  "timeout" => 5
]);

并连接到MPD

try{
  $mphpd->connect();
}catch (MPDException $e){
  echo $e->getMessage();
  return false;
}

示例

以下是一些您可以使用它的示例

// get MPD's status like current song, volume, state, etc...
$status = $mphpd->status();

// if you only want to retrieve only one (or more) values
// you can pass it a list of keys.
$state = $mphpd->status([ "state" ]);

// clear the queue
$mphpd->queue()->clear();

// load the first 10 songs of a playlist into the queue and exit on failure.
if(!$mphpd->playlist("some-playlist")->load([0,10])){
  echo $mphpd->get_last_error()["message"]; // prints "No such playlist"
  return false;
}

// shuffle the queue
$mphpd->queue()->shuffle();

// adjust volume to 40%
$mphpd->player()->volume(40);

// start playing
$mphpd->player()->play();

有关更多信息,请参阅 文档

支持的PHP版本

  • 7.4及以上

所需的PHP扩展

  • 仅需要 sockets,这是大多数PHP安装默认包含的。

所需的PHP函数

一个PHP函数列表,MphpD需要它们进行套接字通信

  • fgets
  • fputs
  • fread
  • stream_get_meta_data
  • stream_set_chunk_size
  • stream_set_timeout
  • stream_socket_client