MCIERR_DEVICE_OPEN error with mciSendString() c++

48 Views Asked by At

I'm trying to make a simple audio player class for C++. I've been using mciSendString() to actually play the audio.

The class constructor passes in a string for the file location (File), and then it checks to see if it can actually open the file. The way I have that going is that it tries to get the length of the audio file, and when it can't get anything then it prints the MCI error.

// return false if file cannot be opened
void openFile(const std::string& file) {
    bool opened = true;

    // check if file is empty
    std::string S2 = "status " + file + " length";
    char buffer[128];

    std::string err = mciErrorLookup( // mciErrorLookup is a function that i made that returns the mci error as a string
        mciSendString(TEXT(S2.c_str()), buffer, 128, NULL)
    );

    try {
        // tries to convert the buffer, which should hold the length of the audio, to string
        auto _ = std::stoi(buffer);
    } catch (const std::invalid_argument& e) {
        opened = false;
    }

    if (!opened)
        // if opened is false, then "err" must be something
        // [if mciSendString does not return an error then "err" will equal "unknown error"]
        throw std::runtime_error("Failed to open audio file \"" + file + "\"\n" + err);
    }

The audio class works fine on a few of my audio files, shorter mp3s (~7Kb and ~30Kb). But when I try to play a song (larger file, 4.96Mb), openFile() throws MCIERR_DEVICE_OPEN.

Microsoft docs say "The device name is already used as an alias by this application. Use a unique alias." for MCIERR_DEVICE_OPEN. What I don't understand is why this is working fine for smaller files but then not working for larger files.

I've looked all over but can't find anything about mciSendString() limitations for audio file sizes. I did see something about it not working if the directory name was too large, but even when I shortened the directory for the large audio file the error persisted. I'm not sure what I should do as I'm not even 100% sure what is happening. Any help would be appreciated.

0

There are 0 best solutions below