Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
stephentoub opened this issue Jan 5, 2023 · 3 comments · Fixed by #82612
Closed

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

stephentoub opened this issue Jan 5, 2023 · 3 comments · Fixed by #82612

Comments

@stephentoub
Copy link
Member

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 5, 2023
@stephentoub
Copy link
Member Author

Related:
#69159

@ghost
Copy link

ghost commented Jan 5, 2023

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: stephentoub
Assignees: -
Labels:

area-System.IO

Milestone: -

@ghost
Copy link

ghost commented Jan 9, 2023

Tagging subscribers to this area: @dotnet/area-microsoft-win32
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: stephentoub
Assignees: -
Labels:

area-Microsoft.Win32, untriaged

Milestone: -

@ViktorHofer ViktorHofer added this to the 8.0.0 milestone Jan 23, 2023
@ViktorHofer ViktorHofer removed the untriaged New issue has not been triaged by the area owner label Jan 23, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 24, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 2, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants