Skip to content

Commit

Permalink
- Adjusting Music Player service to only change the volume of the son…
Browse files Browse the repository at this point in the history
…g instead of the entire device, when the song is a local file
  • Loading branch information
SaviorXTanren committed Sep 12, 2023
1 parent 4867f04 commit ff910af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 8 additions & 6 deletions MixItUp.WPF/Services/WindowsAudioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ public async Task Play(string filePath, int volume, string deviceName, bool trac
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(async () =>
{
using (WaveOutEvent waveStream = this.PlayWithOutput(filePath, volume, deviceName))
Tuple<WaveOutEvent, WaveStream> output = this.PlayWithOutput(filePath, volume, deviceName);
WaveOutEvent waveOut = output.Item1;
using (waveOut)
{
if (waveStream != null)
if (waveOut != null)
{
while (waveStream.PlaybackState == PlaybackState.Playing && !cancellationTokenSource.Token.IsCancellationRequested)
while (waveOut.PlaybackState == PlaybackState.Playing && !cancellationTokenSource.Token.IsCancellationRequested)
{
await Task.Delay(500);
}
waveStream.Dispose();
waveOut.Dispose();
cancellationTokenSource.Cancel();
}
}
Expand Down Expand Up @@ -124,7 +126,7 @@ public IEnumerable<string> GetOutputDevices()
return results;
}

public WaveOutEvent PlayWithOutput(string filePath, int volume, string deviceName)
public Tuple<WaveOutEvent, WaveStream> PlayWithOutput(string filePath, int volume, string deviceName)
{
int deviceNumber = -1;
if (!string.IsNullOrEmpty(deviceName))
Expand Down Expand Up @@ -152,7 +154,7 @@ public WaveOutEvent PlayWithOutput(string filePath, int volume, string deviceNam
{
outputEvent.Init(waveStream);
outputEvent.Play();
return outputEvent;
return new Tuple<WaveOutEvent, WaveStream>(outputEvent, waveStream);
}
return null;
}
Expand Down
11 changes: 9 additions & 2 deletions MixItUp.WPF/Services/WindowsMusicPlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public MusicPlayerSong CurrentSong

private CancellationTokenSource backgroundPlayThreadTokenSource = new CancellationTokenSource();
private WaveOutEvent currentWaveOutEvent;
private WaveStream currentWaveStream;

private SemaphoreSlim sempahore = new SemaphoreSlim(1);

Expand Down Expand Up @@ -158,7 +159,11 @@ public async Task ChangeVolume(int amount)
await this.sempahore.WaitAndRelease(() =>
{
this.Volume = amount;
if (this.currentWaveOutEvent != null)
if (this.currentWaveStream != null && this.currentWaveStream is AudioFileReader)
{
((AudioFileReader)this.currentWaveStream).Volume = (ServiceManager.Get<IAudioService>() as WindowsAudioService).ConvertVolumeAmount(this.Volume);
}
else if (this.currentWaveOutEvent != null)
{
this.currentWaveOutEvent.Volume = (ServiceManager.Get<IAudioService>() as WindowsAudioService).ConvertVolumeAmount(this.Volume);
}
Expand Down Expand Up @@ -286,7 +291,9 @@ private void PlayInternal(string filePath)
this.backgroundPlayThreadTokenSource = new CancellationTokenSource();

WindowsAudioService audioService = ServiceManager.Get<IAudioService>() as WindowsAudioService;
this.currentWaveOutEvent = audioService.PlayWithOutput(filePath, this.Volume, ChannelSession.Settings.MusicPlayerAudioOutput);
Tuple<WaveOutEvent, WaveStream> output = audioService.PlayWithOutput(filePath, this.Volume, ChannelSession.Settings.MusicPlayerAudioOutput);
this.currentWaveOutEvent = output.Item1;
this.currentWaveStream = output.Item2;
Task backgroundPlayThreadTask = Task.Run(async () => await this.PlayBackground(this.currentWaveOutEvent, this.currentSongIndex), this.backgroundPlayThreadTokenSource.Token);
}

Expand Down

0 comments on commit ff910af

Please sign in to comment.