Skip to content

SoundPlayer from Stream doesn't work for partial reads #80264

@stephentoub

Description

@stephentoub

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:

cc: @eerhardt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions