FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Move Unix-specific implementation into a base class that Linux- and M… · DEVBOX10/NetCoreAudio@868b7ac · GitHub

Repository navigation

Commit 868b7ac

Browse files
fiodarsazanavets
committed
Move Unix-specific implementation into a base class that Linux- and MAc-specific players can inherit from
1 parent 2060b49 commit 868b7ac

4 files changed

Lines changed: 115 additions & 173 deletions

File tree

‎NetCoreAudio/NetCoreAudio.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/mobiletechtracker/NetCoreAudio</RepositoryUrl>
1313
<RepositoryType>GitHub</RepositoryType>
1414
<PackageTags>.NET Core audio</PackageTags>
15-
<Version>1.3.4</Version>
15+
<Version>1.4.0</Version>
1616
<PackageIconUrl>https://avatars3.githubusercontent.com/u/42421108?s=400&amp;u=d456e9d70c60cc963a855285290e7d8428e3a46c&amp;v=4</PackageIconUrl>
1717
</PropertyGroup>
1818

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,15 @@
11
using NetCoreAudio.Interfaces;
2-
using System;
3-
using System.Diagnostics;
4-
using System.Threading.Tasks;
52

63
namespace NetCoreAudio.Players
74
{
8-
internal class LinuxPlayer : IPlayer
5+
internal class LinuxPlayer : UnixPlayerBase, IPlayer
96
{
10-
private Process _process = null;
11-
12-
public event EventHandler PlaybackFinished;
13-
14-
public bool Playing { get; private set; }
15-
16-
public bool Paused { get; private set; }
17-
18-
public async Task Play(string fileName)
19-
{
20-
await Stop();
21-
_process = StartAplayPlayback(fileName);
22-
Playing = true;
23-
}
24-
25-
public async Task Pause()
26-
{
27-
if (Playing && !Paused)
28-
{
29-
await PauseOrResume();
30-
Paused = true;
31-
}
32-
}
33-
34-
public async Task Resume()
35-
{
36-
if (Playing && Paused)
7+
protected override string BashToolName
8+
{
9+
get
3710
{
38-
await PauseOrResume();
39-
Paused = false;
11+
return "aplay";
4012
}
41-
}
42-
43-
public Task Stop()
44-
{
45-
if (_process != null)
46-
{
47-
_process.Kill();
48-
_process.Dispose();
49-
_process = null;
50-
}
51-
52-
Playing = false;
53-
Paused = false;
54-
55-
return Task.CompletedTask;
56-
}
57-
58-
private Process StartAplayPlayback(string fileName)
59-
{
60-
var escapedArgs = fileName.Replace("\"", "\\\"");
61-
62-
var process = new Process()
63-
{
64-
StartInfo = new ProcessStartInfo
65-
{
66-
FileName = "/bin/bash",
67-
Arguments = $"-c \"aplay '{escapedArgs}' -i\"",
68-
RedirectStandardOutput = true,
69-
RedirectStandardInput = true,
70-
UseShellExecute = false,
71-
CreateNoWindow = true,
72-
}
73-
};
74-
process.Exited += HandlePlaybackFinished;
75-
process.Start();
76-
return process;
77-
}
78-
79-
private Task PauseOrResume()
80-
{
81-
if (_process != null)
82-
{
83-
// Space character pauses/resumes aplay running in interactive mode
84-
_process.StandardInput.WriteLine("\x20");
85-
}
86-
87-
return Task.CompletedTask;
88-
}
89-
90-
private void HandlePlaybackFinished(object sender, EventArgs e)
91-
{
92-
Playing = false;
93-
PlaybackFinished?.Invoke(this, e);
94-
}
13+
}
9514
}
9615
}

‎NetCoreAudio/Players/MacPlayer.cs‎

Lines changed: 7 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,15 @@
11
using NetCoreAudio.Interfaces;
2-
using System;
3-
using System.Diagnostics;
4-
using System.Threading.Tasks;
52

63
namespace NetCoreAudio.Players
74
{
8-
internal class MacPlayer : IPlayer
9-
{
10-
private Process _process = null;
11-
12-
private const string KillProcessCommand = "ps -A | grep -m1 'afplay' | awk '{print $1}' | kill {0}";
13-
14-
public event EventHandler PlaybackFinished;
15-
16-
public bool Playing { get; private set; }
17-
18-
public bool Paused { get; private set; }
19-
20-
public async Task Play(string fileName)
21-
{
22-
await Stop();
23-
_process = StartBashProcess($"afplay '{fileName}'");
24-
_process.Exited += HandlePlaybackFinished;
25-
Playing = true;
26-
}
27-
28-
public Task Pause()
29-
{
30-
if (Playing && !Paused)
31-
{
32-
var tempProcess = StartBashProcess(string.Format(KillProcessCommand, "-17"));
33-
tempProcess.WaitForExit();
34-
Paused = true;
35-
}
36-
37-
return Task.CompletedTask;
38-
}
39-
40-
public Task Resume()
41-
{
42-
if (Playing && Paused)
43-
{
44-
var tempProcess = StartBashProcess(string.Format(KillProcessCommand, "-19"));
45-
tempProcess.WaitForExit();
46-
Paused = false;
47-
}
48-
49-
return Task.CompletedTask;
50-
}
51-
52-
public Task Stop()
53-
{
54-
if (_process != null)
5+
internal class MacPlayer : UnixPlayerBase, IPlayer
6+
{
7+
protected override string BashToolName
8+
{
9+
get
5510
{
56-
_process.Kill();
57-
_process.Dispose();
58-
_process = null;
11+
return "afplay";
5912
}
60-
61-
Playing = false;
62-
Paused = false;
63-
64-
return Task.CompletedTask;
65-
}
66-
67-
private Process StartBashProcess(string command)
68-
{
69-
var escapedArgs = command.Replace("\"", "\\\"");
70-
71-
var process = new Process()
72-
{
73-
StartInfo = new ProcessStartInfo
74-
{
75-
FileName = "/bin/bash",
76-
Arguments = $"-c \"{escapedArgs}\"",
77-
RedirectStandardOutput = true,
78-
RedirectStandardInput = true,
79-
UseShellExecute = false,
80-
CreateNoWindow = true,
81-
}
82-
};
83-
process.Start();
84-
return process;
85-
}
86-
87-
private void HandlePlaybackFinished(object sender, EventArgs e)
88-
{
89-
Playing = false;
90-
PlaybackFinished?.Invoke(this, e);
91-
}
13+
}
9214
}
9315
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using NetCoreAudio.Interfaces;
2+
using System;
3+
using System.Diagnostics;
4+
using System.Threading.Tasks;
5+
6+
namespace NetCoreAudio.Players
7+
{
8+
internal class UnixPlayerBase : IPlayer
9+
{
10+
private Process _process = null;
11+
12+
internal const string PauseProcessCommand = "killall -STOP {0}";
13+
internal const string ResumeProcessCommand = "killall -CONT {0}";
14+
15+
protected virtual string BashToolName { get; }
16+
17+
public event EventHandler PlaybackFinished;
18+
19+
public bool Playing { get; private set; }
20+
21+
public bool Paused { get; private set; }
22+
23+
public async Task Play(string fileName)
24+
{
25+
await Stop();
26+
_process = StartBashProcess($"{BashToolName} '{fileName}'");
27+
_process.Exited += HandlePlaybackFinished;
28+
_process.ErrorDataReceived += HandlePlaybackFinished;
29+
_process.Disposed += HandlePlaybackFinished;
30+
Playing = true;
31+
}
32+
33+
public Task Pause()
34+
{
35+
if (Playing && !Paused)
36+
{
37+
var tempProcess = StartBashProcess(string.Format(PauseProcessCommand, BashToolName));
38+
tempProcess.WaitForExit();
39+
Paused = true;
40+
}
41+
42+
return Task.CompletedTask;
43+
}
44+
45+
public Task Resume()
46+
{
47+
if (Playing && Paused)
48+
{
49+
var tempProcess = StartBashProcess(string.Format(ResumeProcessCommand, BashToolName));
50+
tempProcess.WaitForExit();
51+
Paused = false;
52+
}
53+
54+
return Task.CompletedTask;
55+
}
56+
57+
public Task Stop()
58+
{
59+
if (_process != null)
60+
{
61+
_process.Kill();
62+
_process.Dispose();
63+
_process = null;
64+
}
65+
66+
Playing = false;
67+
Paused = false;
68+
69+
return Task.CompletedTask;
70+
}
71+
72+
protected Process StartBashProcess(string command)
73+
{
74+
var escapedArgs = command.Replace("\"", "\\\"");
75+
76+
var process = new Process()
77+
{
78+
StartInfo = new ProcessStartInfo
79+
{
80+
FileName = "/bin/bash",
81+
Arguments = $"-c \"{escapedArgs}\"",
82+
RedirectStandardOutput = true,
83+
RedirectStandardInput = true,
84+
UseShellExecute = false,
85+
CreateNoWindow = true,
86+
}
87+
};
88+
process.Start();
89+
return process;
90+
}
91+
92+
internal void HandlePlaybackFinished(object sender, EventArgs e)
93+
{
94+
if (Playing)
95+
{
96+
Playing = false;
97+
PlaybackFinished?.Invoke(this, e);
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL