c++ - c++ sfml mp3音乐播放器错误

标签 c++ audio sfml

我正在尝试使用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;
}
目录:
enter image description here
编辑
我尝试了不同的格式,例如mp3 wav ogg,
没变。
目录中的当前文件:
enter image description here

最佳答案

错误2是跟进错误,因为加载未正确失败(这是SFML中的错误,或者曾经是SFML中的错误)。

您的实际问题是SFML不支持MP3文件。将该文件转换为WAV或OGG,然后重试。

关于c++ - c++ sfml mp3音乐播放器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139428/

相关文章:

c++ - 全局变量超出范围!有没有搞错?

c++ - c++和sfml编译问题

c++ - C/C++——在Linux下寻找实时计时事件

audio - 用于开发音频混合Web应用程序的平台是什么?

c# - 如何在 C# 中将音频流式传输到扬声器

iphone - Objective-C:解析.m3u文件以获得.mp3

c++ - 如何在 SFML 中使用顶点数组?

c++ - @parm 在 C++ 中是什么意思或暗示?

c++ - 无法将 splitterChannel 与 poco 的高级 xml 配置一起使用

c++ - 编写自定义纯虚拟处理程序 : What is the state of the stack and registers when it is called