跳转到内容

Audio 模块

类: SoundBuffer、Sound、Music、SoundStream、SoundSource、SoundRecorder、SoundBufferRecorder、InputSoundFile、OutputSoundFile、SoundFileFactory / Reader / Writer、AudioResource
命名空间: Listener、PlaybackDevice(设备管理)

sf::SoundBuffer buffer("shot.wav"); // 或 loadFromFile
sf::Sound sound(buffer); // 无默认构造;必须绑定 buffer
sound.setLooping(true); // 原 setLoop/getLoop → setLooping/isLooping
sound.setVolume(50.f);
sound.setPitch(1.2f);
sound.play(); // pause / stop / setPlayingOffset(sf::seconds(2))
  • 格式:WAV、OGG/Vorbis、FLAC;MP3 解码支持、编码不支持
  • loadFromSamples 等需 Channel Map(std::vector<sf::SoundChannel>)——相对 2.x 破坏性变更
  • 生命周期: Sound 只引用 SoundBuffer,buffer 必须活过 Sound
sf::Music music("bgm.ogg"); // openFromFile(流式,非完整 load)
music.setLoopPoints({sf::milliseconds(500), sf::seconds(4)});
music.play();
  • Music 不可拷贝、可移动
  • 流式播放时源文件 / 内存必须保持有效

setPosition、setRelativeToListener、setMinDistance、setAttenuation、setSpatializationEnabled,以及 cone / velocity / doppler 等(见 SoundSource.hpp)。

sf::Listener::setGlobalVolume、setPosition、setDirection、setUpVector、setVelocity、setCone …

getAvailableDevices()、getDefaultDevice()、setDevice(name)、setDeviceToDefault()、setDeviceToNull()、getDevice()、getDeviceSampleRate() → optional<uint32_t>、setNotificationCallback

相关教程:Sounds。