在C ++中使用SFML。尝试在循环中播放wav文件,但它只播放第一秒左右

时间:2021-11-01 19:44:44

I am trying to run music in a game, but for whatever reason, the file refuses to play entirely. It's a 7 second track, but only the first second or so plays and then I'm given silence. I also programmed this in such a way that it should loop, but it does not do so. Can anyone tell me what I'm doing wrong? Again, the file DOES play, but only the first second and only once.

我试图在游戏中运行音乐,但无论出于何种原因,文件都拒绝完全播放。这是一个7秒的曲目,但只有第一秒左右播放,然后我沉默。我也以这样的方式编程,它应该循环,但它不会这样做。谁能告诉我我做错了什么?同样,文件正在播放,但只播放第一秒而且只播放一次。

sf::Music music;

if (!music.openFromFile("data/sounds/music.wav")){
    std::cout << "Error...";
}
else{
    music.setLoop(true);
    music.setVolume(50);
    music.play();
}

1 个解决方案

#1


The music will only play as long as the sf::Music object remains in scope. If the sf::Music object is declared in a method, it is destructed at the end of that method, and the music will stop too.

只要sf :: Music对象保留在范围内,音乐才会播放。如果在方法中声明sf :: Music对象,则在该方法结束时对其进行销毁,音乐也将停止。

Give your sf::Music instance class scope; It will keep playing until the class goes out of scope or .stop()/.pause() is called.

给你的sf :: Music实例类范围;它将继续播放,直到课程超出范围或.stop()/。暂停()被调用。

So instead of:

所以代替:

class MyClass {
public:
    void playMahJam() {
        sf::Music jam; // Will be destructed at end of method

        if (!music.openFromFile("data/sounds/music.wav")){
            std::cout << "Error..." << std::endl;
        }
        else{
            music.setLoop(true);
            music.setVolume(50);
            music.play();
        }
    } // end playMahJam()
};

Try:

class MyClass {
public:
    void playMahJam() {

        if (!music.openFromFile("data/sounds/music.wav")){
            std::cout << "Error..." << std::endl;
        }
        else{
            music.setLoop(true);
            music.setVolume(50);
            music.play();
        }
    } // end playMahJam()
private:
    sf::Music jam;
};

#1


The music will only play as long as the sf::Music object remains in scope. If the sf::Music object is declared in a method, it is destructed at the end of that method, and the music will stop too.

只要sf :: Music对象保留在范围内,音乐才会播放。如果在方法中声明sf :: Music对象,则在该方法结束时对其进行销毁,音乐也将停止。

Give your sf::Music instance class scope; It will keep playing until the class goes out of scope or .stop()/.pause() is called.

给你的sf :: Music实例类范围;它将继续播放,直到课程超出范围或.stop()/。暂停()被调用。

So instead of:

所以代替:

class MyClass {
public:
    void playMahJam() {
        sf::Music jam; // Will be destructed at end of method

        if (!music.openFromFile("data/sounds/music.wav")){
            std::cout << "Error..." << std::endl;
        }
        else{
            music.setLoop(true);
            music.setVolume(50);
            music.play();
        }
    } // end playMahJam()
};

Try:

class MyClass {
public:
    void playMahJam() {

        if (!music.openFromFile("data/sounds/music.wav")){
            std::cout << "Error..." << std::endl;
        }
        else{
            music.setLoop(true);
            music.setVolume(50);
            music.play();
        }
    } // end playMahJam()
private:
    sf::Music jam;
};