Skip to content

Commit

Permalink
MPV will now continue to play where you left of
Browse files Browse the repository at this point in the history
  • Loading branch information
Zexuz committed Apr 22, 2019
1 parent 68593d8 commit abdcec6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Mpv.JsonIpc/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public async Task<float> GetVolume()
return result.Data;
}

public async Task PlayMedia(string path)
public async Task PlayMedia(string path, TimeSpan startPosition)
{
var request = _ipc.CreateCommand(new[] {"loadfile", path});
var request = _ipc.CreateCommand(new[] {"loadfile", path, "replace", $"start={startPosition.TotalSeconds}"});
await _ipc.ExecuteCommand<string>(request);
}

Expand Down
2 changes: 1 addition & 1 deletion Mpv.JsonIpc/IApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IApi
{
Task ShowText(string text, TimeSpan duration);
Task<float> GetVolume();
Task PlayMedia(string path);
Task PlayMedia(string path, TimeSpan startPosition);
Task<TimeSpan> GetCurrentPosition();
Task<TimeSpan> GetDuration();
}
Expand Down
7 changes: 5 additions & 2 deletions SnackTime.Core/Session/Item.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;

namespace SnackTime.Core.Session
{
public class Item
{
public MediaFileId MediaFileId { get; set; }
public string Path { get; set; }
public MediaFileId MediaFileId { get; set; }
public string Path { get; set; }
public TimeSpan StartPosition { get; set; }
}
}
32 changes: 23 additions & 9 deletions SnackTime.WebApi/Controllers/RemoteController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Mpv.JsonIpc;
using SnackTime.Core;
using SnackTime.Core.Media.Episodes;
using SnackTime.Core.Process;
using SnackTime.Core.Session;
using SnackTime.Core.Settings;

namespace SnackTime.WebApi.Controllers
{
Expand All @@ -15,42 +17,54 @@ public class RemoteController : ControllerBase
private readonly ProcessManager _processManager;
private readonly EpisodeFileLookupProvider _fileLookupProvider;
private readonly Queue<Item> _queue;
private readonly SettingsService _settingsService;

public RemoteController(ProcessManager processManager, EpisodeFileLookupProvider fileLookupProvider, Queue<Item> queue)
public RemoteController
(
ProcessManager processManager,
EpisodeFileLookupProvider fileLookupProvider,
Queue<Item> queue,
SettingsService settingsService
)
{
_processManager = processManager;
_fileLookupProvider = fileLookupProvider;
_queue = queue;
_settingsService = settingsService;
}

[HttpGet("play/{mediaFileIdStr}/{startPositionInSec?}")]
public async Task<ActionResult> PlayMedia(string mediaFileIdStr, int startPositionInSec)
public async Task<ActionResult> PlayMedia(string mediaFileIdStr, double startPositionInSec)
{
if (!MediaFileId.TryParse(mediaFileIdStr, out var mediaFileId))
return BadRequest($"{nameof(mediaFileIdStr)} is invalid");

if (startPositionInSec < 0)
return BadRequest($"{nameof(startPositionInSec)} must be > 0");

var settings = _settingsService.Get();

if (string.IsNullOrWhiteSpace(settings.System.MpvPath))
return BadRequest("mpv path must be set in the settings tab");

if (!_processManager.IsMpvRunning())
{
var path = "C:\\Program Files (x86)\\SVP 4\\mpv64\\mpv.exe";
var arguments = new[] {$"--input-ipc-server={NamedPipeFactory.GetPipeNameForCurrentOs()}"};
_processManager.StartProcess(path, arguments);
_processManager.StartProcess(settings.System.MpvPath, arguments);
}

if (!_processManager.IsSvpRunning())
if (!_processManager.IsSvpRunning() && !string.IsNullOrWhiteSpace(settings.System.SvpPath))
{
var path = "C:\\Program Files (x86)\\SVP 4\\SVPManager.exe";
_processManager.StartProcess(path);
_processManager.StartProcess(settings.System.SvpPath);
}

var fileInfo = await _fileLookupProvider.GetFileInfoForId(mediaFileId.FileId);

_queue.AddToQueue(new Item
{
Path = fileInfo.Path,
MediaFileId = mediaFileId
MediaFileId = mediaFileId,
StartPosition = TimeSpan.FromSeconds(startPositionInSec),
});

return StatusCode(202);
Expand Down
3 changes: 2 additions & 1 deletion SnackTime.WebApi/MediaPlayerObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ private async Task Tick()
}

var item = _queue.Pop();
await _api.PlayMedia(item.Path, item.StartPosition);
await _api.ShowText($"Now playing {item.Path.Substring(item.Path.LastIndexOf('\\') + 1)}", TimeSpan.FromSeconds(5));
await _api.PlayMedia(item.Path);

var duration = await _api.GetDuration();
currentSession = _sessionFactory.CreateNewSession(item.MediaFileId, duration);
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/logic/api/remote/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export class Endpoints {
public static PlayFile(id: string) {
return `${this.prefix}/play/${id}`;
}

public static PlayFileOnStartPosition(id: string, seconds: number) {
return `${this.prefix}/play/${id}/${seconds}`;
}
}
12 changes: 9 additions & 3 deletions app/src/views/Series.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<td>
<button
:disabled="episode.playableId === ''"
@click="play(episode.playableId)"
@click="play(episode)"
class="btn"
>
play
Expand Down Expand Up @@ -128,8 +128,14 @@ export default class SeriesInfo extends Vue {
M.AutoInit();
}
private async play(fileId: string) {
let res = await HttpClient.get<object>(Endpoints.PlayFile(fileId));
private async play(episode: Episode) {
let startPos =
episode.progress && episode.progress.watchedInSec
? episode.progress.watchedInSec
: 0;
await HttpClient.get<object>(
Endpoints.PlayFileOnStartPosition(episode.playableId, startPos)
);
}
get imageUrl(): string {
Expand Down

0 comments on commit abdcec6

Please sign in to comment.