Untitled

 avatar
unknown
plain_text
2 months ago
1.2 kB
5
Indexable
private void PlaySound(string path)
{
    SDL_AudioSpec spec;
    IntPtr audioBuf;
    uint audioLen;

    if (SDL_LoadWAV(path, out spec, out audioBuf, out audioLen) != true)
    {
        Console.WriteLine("Failed to load WAV: " + SDL_GetError());
        return;
    }

    // Get default playback device
    int deviceCount;
    IntPtr devices = SDL_GetAudioPlaybackDevices(out deviceCount);

    if (deviceCount == 0)
    {
        Console.WriteLine("No audio devices found");
        SDL_free(audioBuf);
        return;
    }

    uint deviceId;
    unsafe
    {
        uint* devicePtr = (uint*)devices;
        deviceId = devicePtr[0];
    }

    IntPtr stream = SDL_OpenAudioDeviceStream(deviceId, ref spec, null, IntPtr.Zero);

    if (stream == IntPtr.Zero)
    {
        Console.WriteLine("Failed to open stream: " + SDL_GetError());
        SDL_free(audioBuf);
        SDL_free(devices);
        return;
    }

    SDL_ResumeAudioStreamDevice(stream);

    SDL_PutAudioStreamData(stream, audioBuf, (int)audioLen);
    SDL_FlushAudioStream(stream);

    Console.WriteLine($"Playing {path}");

    SDL_free(audioBuf);
    SDL_free(devices);
}
Editor is loading...
Leave a Comment