Skip to content

Commit

Permalink
fix: volume control
Browse files Browse the repository at this point in the history
  • Loading branch information
geloczi committed Apr 1, 2023
1 parent 6bd65d7 commit 86b8442
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions MusicPlayback/WavStreamPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
using System.Threading;
using MusicPlayback.Utils;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace MusicPlayback
{
public class WavStreamPlayer : IAudioStreamPlayer
{
#region Fields
private readonly object _lock = new object();
private const int SleepMillisecondsWhenBufferFull = 2;
private IWavePlayer _outputDevice;
protected readonly object _lock = new object();

protected readonly MyBufferedWaveProvider _bufferedWaveProvider;
private readonly VolumeSampleProvider _volumeSampleProvider;
private readonly OutputApiType _outputType;
#endregion Fields

Expand Down Expand Up @@ -56,7 +59,7 @@ public double Volume
lock (_lock)
{
if (_outputDevice != null)
_outputDevice.Volume = (float)_volume;
_volumeSampleProvider.Volume = (float)Volume;
}
}
}
Expand All @@ -70,6 +73,7 @@ public WavStreamPlayer(OutputApiType outputType, int bufferSizeInSeconds)
{
_outputType = outputType;
_bufferedWaveProvider = new MyBufferedWaveProvider(new WaveFormat(), bufferSizeInSeconds);
_volumeSampleProvider = new VolumeSampleProvider(_bufferedWaveProvider.ToSampleProvider());
}
#endregion Constructor

Expand Down Expand Up @@ -207,9 +211,9 @@ private void CreateOutputDevice()
default:
throw new NotSupportedException($"{_outputType}");
}
_outputDevice.Volume = (float)_volume;
_outputDevice.PlaybackStopped += OutputDevice_PlaybackStopped;
_outputDevice.Init(_bufferedWaveProvider);
_volumeSampleProvider.Volume = (float)Volume;
_outputDevice.Init(_volumeSampleProvider);
}

private void DisposeOutputDevice()
Expand Down

0 comments on commit 86b8442

Please sign in to comment.