-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApi.cs
60 lines (50 loc) · 1.69 KB
/
Api.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Globalization;
using System.Threading.Tasks;
namespace Mpv.JsonIpc
{
public class Api : IApi
{
private readonly IIpc _ipc;
public Api(IIpc ipc)
{
_ipc = ipc;
}
public async Task ShowText(string text, TimeSpan duration)
{
var request = _ipc.CreateCommand(new[] {"show-text", text}, duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
await _ipc.ExecuteCommand<string>(request);
}
public async Task<float> GetVolume()
{
var result = await _ipc.GetProperty<float>(Property.Volume);
return result.Data;
}
public async Task PlayMedia(string path, TimeSpan startPosition)
{
var request = _ipc.CreateCommand(new[] {"loadfile", path, "replace", $"start={startPosition.TotalSeconds}"});
await _ipc.ExecuteCommand<string>(request);
}
public async Task<TimeSpan> GetCurrentPosition()
{
var result = await _ipc.GetProperty<double>(Property.Position);
return TimeSpan.FromSeconds(result.Data);
}
public async Task<TimeSpan> GetDuration()
{
var result = await _ipc.GetProperty<double>(Property.Duration);
int counter = 0;
while (Math.Abs(result.Data) < 1)
{
await Task.Delay(100);
result = await _ipc.GetProperty<double>(Property.Duration);
counter++;
if (counter > 20)
{
break;
}
}
return TimeSpan.FromSeconds(result.Data);
}
}
}