我正在尝试使用mp3音乐文件播放器将音乐实现到游戏中,但它无法加载mp3(error1)文件,并且每次我按“E”键都将显示error(error2)。
error1: failed to open sound file "music.mp3" couldn't open stream
error2: Failed to play audio stream: sound parameters have not been initialized call initialize first
代码:
#include <SFML/Graphics.hpp>
#include <SFML\Audio.hpp>
#include <iostream>
const int WIDTH = 800;
const int HEIGHT = 800;
const std::string TITLE = "Game 2000";
int main()
{
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), TITLE);
window.setVerticalSyncEnabled(true);
sf::CircleShape shape(400.f);
shape.setFillColor(sf::Color::Green);
sf::Clock clock;
sf::Music music;
if (!music.openFromFile("music.mp3"))
std::cout << "cannot find music.mp3" << std::endl;
while (window.isOpen())
{
sf::Time time = clock.getElapsedTime();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
music.play();
//std::cout << 1.0f / time.asSeconds() << std::endl;
clock.restart().asSeconds();
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
目录:编辑
我尝试了不同的格式,例如mp3 wav ogg,
没变。
目录中的当前文件:
最佳答案
错误2是跟进错误,因为加载未正确失败(这是SFML中的错误,或者曾经是SFML中的错误)。
您的实际问题是SFML不支持MP3文件。将该文件转换为WAV或OGG,然后重试。
关于c++ - c++ sfml mp3音乐播放器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139428/