forked from dziemborowicz/hourglass
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
286 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
|
||
using NAudio.Wave; | ||
|
||
using Hourglass.Windows.Audio; | ||
|
||
namespace Hourglass.NAudio; | ||
|
||
public class AudioPlayer: IAudioPlayer | ||
{ | ||
private readonly WaveOutEvent _waveOutEvent = new(); | ||
|
||
private AudioFileReader? _audioFile; | ||
|
||
public AudioPlayer(EventHandler stoppedEventHandler) => | ||
_waveOutEvent.PlaybackStopped += (s, e) => stoppedEventHandler(s, e); | ||
|
||
public void Open(string uri) | ||
{ | ||
_audioFile?.Dispose(); | ||
_audioFile = null; | ||
_audioFile = new AudioFileReader(uri); | ||
|
||
_waveOutEvent.Init(_audioFile); | ||
} | ||
|
||
public void Play() | ||
{ | ||
_audioFile!.Position = 0; | ||
_waveOutEvent.Play(); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_waveOutEvent.Stop(); | ||
|
||
_audioFile?.Dispose(); | ||
_audioFile = null; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Stop(); | ||
|
||
_waveOutEvent.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net48</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
<LangVersion>latest</LangVersion> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<AssemblyTitle>Hourglass NAudio</AssemblyTitle> | ||
<Product>Hourglass NAudio</Product> | ||
<Company>Ivan Ivon</Company> | ||
<NeutralLanguage>en-US</NeutralLanguage> | ||
<Copyright>Copyright © $([System.DateTime]::Now.Year) Ivan Ivon</Copyright> | ||
<AssemblyVersion>$(Version)</AssemblyVersion> | ||
<FileVersion>$(Version)</FileVersion> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DisableDiagnosticTracing>true</DisableDiagnosticTracing> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Hourglass\Hourglass.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="NAudio"> | ||
<Version>2.2.1</Version> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Windows.Media; | ||
|
||
namespace Hourglass.Windows.Audio; | ||
|
||
internal class AudioPlayer: IAudioPlayer | ||
{ | ||
private readonly MediaPlayer _mediaPlayer = new(); | ||
|
||
public AudioPlayer(EventHandler stoppedEventHandler) => | ||
_mediaPlayer.MediaEnded += stoppedEventHandler; | ||
|
||
public void Open(string uri) => | ||
_mediaPlayer.Open(new(uri)); | ||
|
||
public void Play() | ||
{ | ||
_mediaPlayer.Position = TimeSpan.Zero; | ||
_mediaPlayer.Play(); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_mediaPlayer.Stop(); | ||
_mediaPlayer.Close(); | ||
} | ||
|
||
public void Dispose() => | ||
Stop(); | ||
} |
Oops, something went wrong.