0

Sorry for the bad code and English,

So I was testing out SFML Audio and I was trying to make an audio player, but It crashes while I tried to run it,

So basically it was supposed to play an audio file and play it. Also, there's another thing I'm having an issue with that is if the audio is paused it rewinds itself as though it has been stopped

No arguments were given in and it didn't print anything either it just crashed.

#include <windows.h>
#include <iostream>
#include <SFML/Audio.hpp>
#include <string>
#include <thread>

sf::Music audio_player;

bool looping = false;

/*
 *
 * audio_state = 2 // Playing
 * audio_state = 1 // Paused
 * audio_state = 0 // Stopped
 *
 */

bool audio_playable() // Checks if the audio is either paused or stopped
{
    if(audio_player.getStatus() == 0 ||
       audio_player.getStatus() == 1  )
        return true;
    else return false;
}

void loop()
{
    looping = true;
    while(1)
    {
        if(GetKeyState('P') & 0x8000 && audio_player.getStatus() == 2)
        {
            audio_player.pause();
            std::cout << "Paused\n";
        }

        if(GetKeyState('S') & 0x8000 && audio_player.getStatus() == 2)
        {
            audio_player.stop();
            std::cout << "Stoped\n";
        }

        if(GetKeyState('L') & 0x8000 && audio_playable() )
        {
            audio_player.play();
            std::cout << "Started\n";
        }

        if(GetKeyState(VK_ESCAPE) & 0x8000)
        {
            looping = false;
        }
    }
}

int main(int argc, char* argv[])
{

    std::string file_name = ""; //Initialize the file location

    if(argc < 1) // If there are no arguments like the file location ask for the file location
    {
        std::cout << "File Location: ";
        std::cin  >> file_name;

        if(file_name == "") 
        {
            std::cout << "File Location cannot be null" << std::endl;
            main(argc, argv);
        }
    }

    if(file_name == "")file_name = argv[1];

    audio_player.openFromFile(file_name);
    audio_player.play();

    std::thread pauseFunc(loop); // new thread for the pause play function

    while(looping)
    {
    }

    return 0;
}
RAGBOT
  • 15
  • 1
  • 7
  • Calling main() recursively is Undefined Behavior: https://stackoverflow.com/a/11349553/12041020 – m88 Mar 20 '21 at 12:16
  • Maybe you should learn more about the meaning of `argc` and `argv` if you want to use them...? Add a diagnostic printout of your `argc` and see what value it has when you run your program. Then look at the source once again and think what the code does when `agrc` has _that_ value.... (Printing the values from `argv` may help, too...) – CiaPan Mar 20 '21 at 12:28
  • Also, if you didn't call either `join()` or `detach()` before `pauseFunc` is destroyed, your program will crash. – m88 Mar 20 '21 at 12:30
  • And, as @m88 said before, _never_ invoke your `main()` from within your program. – CiaPan Mar 20 '21 at 12:38
  • @m88 it still crashes but with a return code of -1073741819. I added a function called start and put all my code from main in there and also there is no use for join or detach in my code so do I still have to put it? – RAGBOT Mar 20 '21 at 12:50
  • @CiaPan I haven't given it arguments in my test so I don't think it's an issue with the args... – RAGBOT Mar 20 '21 at 12:51
  • If something goes wrong, check what it was. Don't ASSUME. You assumed you know what you do when you were writing your program, and you've got an undeniable proof you assumed wrong.Now it's time to debug, not to guess. – CiaPan Mar 20 '21 at 12:55
  • @CiaPan im gonna test it – RAGBOT Mar 20 '21 at 13:05
  • @CiaPan I tried removing argc and argv from the code to see if it worked without those and IT WORKED, it was probably because I didn't add the const keyword before char* argv[] I'm gonna test with that and reply – RAGBOT Mar 20 '21 at 13:10
  • @CiaPan ok now I tried with the cont keyword but it still didn't work but then tried removing if(argc < 1) but kept the code inside it worked. – RAGBOT Mar 20 '21 at 13:15
  • Do you still have 'main(argc, argv);' in your start() function or not? – Martin Sand Mar 20 '21 at 14:25
  • A hint: you test if `argc <1`. Which way does that test go? – CiaPan Mar 20 '21 at 14:26
  • @MartinSand no not anymore. – RAGBOT Mar 20 '21 at 14:59

0 Answers0