koel/app/Http/Controllers/API/SongController.php

54 lines
1.7 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
2018-08-22 17:59:14 +00:00
use App\Factories\StreamerFactory;
use App\Http\Requests\API\SongPlayRequest;
2016-03-05 09:01:12 +00:00
use App\Http\Requests\API\SongUpdateRequest;
2017-04-24 06:38:41 +00:00
use App\Models\Song;
2018-08-18 13:19:40 +00:00
use App\Services\MediaInformationService;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2015-12-13 04:42:28 +00:00
class SongController extends Controller
{
2018-08-18 13:19:40 +00:00
private $mediaInformationService;
2018-08-22 17:59:14 +00:00
private $streamerFactory;
2018-08-18 13:19:40 +00:00
2018-08-22 17:59:14 +00:00
public function __construct(MediaInformationService $mediaInformationService, StreamerFactory $streamerFactory)
2018-08-18 13:19:40 +00:00
{
$this->mediaInformationService = $mediaInformationService;
2018-08-22 17:59:14 +00:00
$this->streamerFactory = $streamerFactory;
2018-08-18 13:19:40 +00:00
}
2015-12-30 04:14:47 +00:00
/**
2016-08-10 01:23:52 +00:00
* Play/stream a song.
2015-12-30 04:14:47 +00:00
*
* @link https://github.com/phanan/koel/wiki#streaming-music
*
* @param null|bool $transcode Whether to force transcoding the song.
* If this is omitted, by default Koel will transcode FLAC.
* @param null|int $bitRate The target bit rate to transcode, defaults to OUTPUT_BIT_RATE.
* Only taken into account if $transcode is truthy.
2016-08-16 15:12:11 +00:00
*
2017-06-04 01:30:45 +00:00
* @return RedirectResponse|Redirector
2015-12-30 04:14:47 +00:00
*/
2018-08-24 15:27:19 +00:00
public function play(SongPlayRequest $request, Song $song, ?bool $transcode = null, ?int $bitRate = null)
2015-12-30 04:14:47 +00:00
{
2018-08-22 17:59:14 +00:00
return $this->streamerFactory
->createStreamer($song, $transcode, $bitRate, floatval($request->time))
->stream();
2015-12-30 04:14:47 +00:00
}
2016-03-05 09:01:12 +00:00
/**
* Update songs info.
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2016-03-05 09:01:12 +00:00
*/
public function update(SongUpdateRequest $request)
{
return response()->json(Song::updateInfo($request->songs, $request->data));
}
2015-12-13 04:42:28 +00:00
}