Repro:
using System.Media;
byte[] wav = File.ReadAllBytes(@"C:\Windows\Media\chimes.wav");
var player = new SoundPlayer();
player.Stream = new TrickleStream(wav);
player.PlaySync();
class TrickleStream : MemoryStream
{
public TrickleStream(byte[] bytes) : base(bytes) { }
public override int Read(byte[] buffer, int offset, int count) =>
base.Read(buffer, offset, Math.Min(count, 1));
public override int Read(Span<byte> buffer) =>
base.Read(buffer.IsEmpty ? buffer : buffer.Slice(0, 1));
}
That should play a chime but instead crashes with an exception about the audio data's header being invalid. That's because it assumes that Stream.Read will always return as much data as was requested, which is not a valid assumption:
|
_stream.Read(_streamData, 0, streamLen); |
cc: @eerhardt
Repro:
That should play a chime but instead crashes with an exception about the audio data's header being invalid. That's because it assumes that Stream.Read will always return as much data as was requested, which is not a valid assumption:
runtime/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs
Line 313 in 1928cd2
cc: @eerhardt