├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── src └── Aplayer.php └── tests └── test.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '5.4' 4 | - '5.5' 5 | - '5.6' 6 | - '7.0' 7 | - '7.1' 8 | - hhvm 9 | - nightly -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-aplayer 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/daryl/php-aplayer/v/stable)](https://packagist.org/packages/daryl/php-aplayer) 4 | [![Total Downloads](https://poser.pugx.org/daryl/php-aplayer/downloads)](https://packagist.org/packages/daryl/php-aplayer) 5 | [![Latest Unstable Version](https://poser.pugx.org/daryl/php-aplayer/v/unstable)](https://packagist.org/packages/daryl/php-aplayer) 6 | [![License](https://poser.pugx.org/daryl/php-aplayer/license)](https://packagist.org/packages/daryl/php-aplayer) 7 | 8 | A php package for Aplayer.[http://aplayer.js.org](http://aplayer.js.org) 9 | 10 | And thank @metowolf with his [Meting](https://github.com/metowolf/Meting) 11 | 12 | **Only netease music provided now, others is comming soon** 13 | 14 | ## How to use 15 | 16 | ```bash 17 | composer require daryl/php-aplayer 18 | ``` 19 | 20 | ```php 21 | $aplayer = new Aplayer\Aplayer(); 22 | $aplayer->out(); 23 | ``` 24 | 25 | ## Methods 26 | 27 | ```php 28 | Aplayer::setSong($songId); //Id of the netease music, default 22817183, one of my favirote music. 29 | Aplayer::setPlaylist($playlistId); //Id of the netease music, default 476998713, one of my favirote playlist. 30 | Aplayer::setSongType('song' or 'playlist') //To choose song or playlist. 31 | Aplayer::setElementId($elementId) //To set the id of the aplayer element, default player1. 32 | ``` 33 | 34 | Others are the setters of the option which in Aplayer. 35 | 36 | ## TODO 37 | * [x] netease music 38 | * [x] playlist for netease music 39 | * [ ] multi elements in one page 40 | 41 | Just enjoy it! 42 | 43 | 44 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "daryl/php-aplayer", 3 | "description": "A php package of Aplayer.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "daryl", 8 | "email": "daryl.moe@outlook.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.6.0", 13 | "metowolf/meting": "^1.3" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Daryl\\": "src/" 18 | } 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "^6.1@dev" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Aplayer.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | /** 13 | * Aplayer.php. 14 | * 15 | * @author daryl 16 | * @copyright 2017 daryl music = new Meting($site); 96 | $this->narrow = false; 97 | $this->autoplay = true; 98 | $this->showlrc = 1; 99 | $this->mutex = true; 100 | $this->theme = '#e6d0b2'; 101 | $this->mode = 'random'; 102 | $this->preload = 'metadata'; 103 | $this->listmaxheight = 513; 104 | $this->song = '22817183'; 105 | $this->playlist = '476998713'; 106 | $this->songType = 'song'; 107 | $this->elementId = 'player1'; 108 | } 109 | 110 | /** 111 | * To get the format array of song. 112 | * 113 | * @param int $song 114 | * 115 | * @return array 116 | */ 117 | protected function getSongs($song) 118 | { 119 | $songs = $this->music->song($song); 120 | $songs = json_decode($songs, true); 121 | if (400 == $songs['code']) { 122 | throw new \Exception($songs['msg']); 123 | } 124 | 125 | $lyric = $this->music->lyric($song); 126 | $lyric = json_decode($lyric, true); 127 | 128 | $pic = $this->music->pic($songs['songs'][0]['al']['pic']); 129 | $pic = json_decode($pic, true); 130 | 131 | $url = $this->music->url($song); 132 | $url = json_decode($url, true); 133 | 134 | return [ 135 | 'title' => $songs['songs'][0]['name'], 136 | 'author' => $songs['songs'][0]['ar'][0]['name'], 137 | 'lrc' => @$lyric['lrc']['lyric'], 138 | 'pic' => $pic['url'], 139 | 'url' => $url['data'][0]['url'], 140 | ]; 141 | } 142 | 143 | /** 144 | * To get the format array of playlist. 145 | * 146 | * @return array 147 | */ 148 | protected function getPlaylist() 149 | { 150 | $playlist = $this->music->playlist($this->playlist); 151 | $songs = json_decode($playlist, true); 152 | if (400 == $songs['code']) { 153 | throw new \Exception($songs['msg']); 154 | } 155 | 156 | $res = []; 157 | foreach ($songs['playlist']['tracks'] as $key => $song) { 158 | $res[] = $this->getSongs($song['id']); 159 | } 160 | 161 | return $res; 162 | } 163 | 164 | /** 165 | * To print the full text of aplayer. 166 | */ 167 | public function out() 168 | { 169 | if ('song' == $this->songType) { 170 | try { 171 | $songs = $this->getSongs($this->song); 172 | } catch (Exception $e) { 173 | exit($e->getMessage()); 174 | } 175 | } elseif ('playlist' == $this->songType) { 176 | try { 177 | $songs = $this->getPlaylist($this->song); 178 | } catch (Exception $e) { 179 | exit($e->getMessage()); 180 | } 181 | } 182 | 183 | $options = [ 184 | 'element' => 'document.getElementById(\'' . $this->elementId . '\')', 185 | 'narrow' => $this->narrow, 186 | 'showlrc' => $this->showlrc, 187 | 'mutex' => $this->mutex, 188 | 'theme' => $this->theme, 189 | 'mode' => $this->mode, 190 | 'preload' => $this->preload, 191 | 'listmaxheight' => $this->listmaxheight . 'px', 192 | 'music' => $songs, 193 | ]; 194 | $str = str_replace( 195 | '"document.getElementById(\'' . $this->elementId . '\')"', 196 | 'document.getElementById(\'' . $this->elementId . '\')', 197 | json_encode($options, JSON_UNESCAPED_SLASHES) 198 | ); 199 | $src = '
'; 200 | $src .= ''; 201 | $src .= ''; 204 | 205 | echo $src; 206 | } 207 | 208 | /** 209 | * To set a song id. 210 | * 211 | * @param string $song 212 | */ 213 | public function setSong($song = '22817204') 214 | { 215 | if (!is_numeric($song)) { 216 | throw new \Exception('Invalid id of song.'); 217 | } 218 | 219 | $this->song = $song; 220 | } 221 | 222 | /** 223 | * To set a playlist id. 224 | * 225 | * @param string $playlist 226 | */ 227 | public function setPlaylist($playlist = '476998713') 228 | { 229 | if (!is_numeric($playlist)) { 230 | throw new \Exception('Invalid id of playlist.'); 231 | } 232 | 233 | $this->playlist = $playlist; 234 | } 235 | 236 | /** 237 | * Sets the value of narrow. 238 | * 239 | * @param bool $narrow 240 | */ 241 | public function setNarrow($narrow) 242 | { 243 | $this->narrow = $narrow ? true : false; 244 | } 245 | 246 | /** 247 | * Sets the value of autoplay. 248 | * 249 | * @param bool $autoplay 250 | */ 251 | public function setAutoplay($autoplay) 252 | { 253 | $this->autoplay = $autoplay ? true : false; 254 | } 255 | 256 | /** 257 | * Sets the value of showlrc. 258 | * 259 | * @param int $showlrc 260 | */ 261 | public function setShowlrc($showlrc) 262 | { 263 | if (in_array($show, [0, 1, 2])) { 264 | $this->showlrc = $showlrc; 265 | } else { 266 | throw new \Exception('Invalid value for showlrc.'); 267 | } 268 | } 269 | 270 | /** 271 | * Sets the value of mutex. 272 | * 273 | * @param bool $mutex 274 | */ 275 | public function setMutex($mutex) 276 | { 277 | $this->mutex = $mutex ? true : false; 278 | } 279 | 280 | /** 281 | * Sets the value of theme. 282 | * 283 | * @param string $theme 284 | */ 285 | public function setTheme($theme) 286 | { 287 | $themeHex = substr($theme, 1); 288 | if (!ctype_xdigit($themeHex)) { 289 | throw new \Exception('Invalid value for theme.'); 290 | } elseif (0x000000 > '0x' . $themeHex && 0xffffff < '0x' . $themeHex) { 291 | throw new \Exception('Invalid value for theme.'); 292 | } else { 293 | $this->theme = $theme; 294 | } 295 | } 296 | 297 | /** 298 | * Sets the value of mode. 299 | * 300 | * @param string $mode 301 | */ 302 | public function setMode($mode) 303 | { 304 | if (in_array($mode, ['random', 'single', 'circulation', 'order'])) { 305 | $this->mode = $mode; 306 | } else { 307 | throw new \Exception('Invalid value for mode.'); 308 | } 309 | } 310 | 311 | /** 312 | * Sets the value of preload. 313 | * 314 | * @param string $preload 315 | */ 316 | public function setPreload($preload) 317 | { 318 | if (in_array($prelo, ['none', 'metadata', 'auto'])) { 319 | throw new \Exception('Invalid value for preload.'); 320 | } 321 | return $this->preload; 322 | } 323 | 324 | /** 325 | * Sets the value of listmaxheight. 326 | * 327 | * @param int listmaxheight 328 | */ 329 | public function setListmaxheight($listmaxheight) 330 | { 331 | if (is_numeric($listmaxheight)) { 332 | $this->listmaxheight = $listmaxheight; 333 | } else { 334 | throw new \Exception('Invalid value for listmaxheight.'); 335 | } 336 | } 337 | 338 | /** 339 | * Sets the value of songType. 340 | * 341 | * @param string $songType 342 | */ 343 | public function setSongType($songType) 344 | { 345 | if (in_array($songType, ['song', 'playlist'])) { 346 | $this->songType = $songType; 347 | } else { 348 | throw new \Exception('Invalid value for song type.'); 349 | } 350 | } 351 | 352 | /** 353 | * Set the id of the element which aplayer displays on. 354 | * 355 | * @param string $elementId 356 | */ 357 | public function setElementId($elementId) 358 | { 359 | $this->elementId = $elementId; 360 | } 361 | } -------------------------------------------------------------------------------- /tests/test.php: -------------------------------------------------------------------------------- 1 | setSongType('song'); 7 | $aplayer->out(); --------------------------------------------------------------------------------