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

问题描述 投票:-1回答:1

我正在尝试使用mp3音乐文件播放器为游戏实现音乐,但它无法加载mp3(error1)文件,每次按“E”时它都会输出错误(error2)。

error1:无法打开声音文件“music.mp3”无法打开流

error2:播放音频流失败:声音参数尚未初始化,请先调用初始化

码:

#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

c++ audio sfml
1个回答
1
投票

错误2是一个跟进错误,因为加载没有正确失败(这是或者是SFML中的错误)。

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

© www.soinside.com 2019 - 2024. All rights reserved.