0

I wish to use mciSendString to play a basic wav file, from a point say 20 seconds from the beginning of the audio. I have tried using it to just open and play a basic wav file in the same directory as the program, however to no avail. This is the basic code I have:

int main() {
    char lpszReturnString[16384];
    MCI_PLAY_PARMS song = { NULL, 0, 15 };
    MCIERROR open = mciSendString("open \"C:\\Users\\ethan\\source\\repos\\Project2\\Project2\\America.wav\" type waveaudio alias America", lpszReturnString, lstrlen(lpszReturnString), NULL);
    MCIERROR set = mciSendString("set America time format samples", lpszReturnString, lstrlen(lpszReturnString), NULL);
    MCIERROR play = mciSendString("play America from 1", lpszReturnString, lstrlen(lpszReturnString), NULL);
    cout << LOWORD(open) << endl;
    cout << HIWORD(open) << endl;
    cout << LOWORD(set) << endl;
    cout << HIWORD(set) << endl;
    cout << LOWORD(play) << endl;
    cout << HIWORD(play) << endl;
    system("pause");
}

The output to the console is:

0
0
0
0
320
0

So I understand there is an error in the play mciSendString which translates to "MCIERR_WAVE_OUTPUTSINUSE". What does this mean and how can i fix this?

  • 2
    `lpszReturnString` is an uninitialised buffer so `lstrlen(lpszReturnString)` is undefined behaviour. You probably want `sizeof(lpszReturnString)` instead. – Jonathan Potter Jun 30 '19 at 06:29
  • Why are you using `mciSendString()` instead of `mciSendCommand()`? And why are you setting the time format to samples when you want to be able to specify a playback position in seconds? – Remy Lebeau Jun 30 '19 at 23:46

1 Answers1

0

In addition, since you want to start playing WAV files from a specific location, such as 20 seconds, you need to modify it to this way.

#include <Windows.h>
#include <iostream>
#pragma comment (lib,"Winmm.lib")
using namespace std;

int main() {
        char lpszReturnString[16384];
        memset(lpszReturnString, 0, sizeof(lpszReturnString));
        MCI_PLAY_PARMS song = { NULL, 0, 15 };
        MCIERROR open = mciSendString("open \"C:\\Users\\ethan\\source\\repos\\Project2\\Project2\\America.wav\" type waveaudio alias America" , lpszReturnString, sizeof(lpszReturnString), NULL);
        MCIERROR set = mciSendString("set America time format ms ", lpszReturnString, sizeof(lpszReturnString), NULL);
        MCIERROR seek = mciSendString("seek America to 20000", NULL, 0, 0);
        MCIERROR play = mciSendString("play America", lpszReturnString, sizeof(lpszReturnString), NULL);
        cout << LOWORD(open) << endl;
        cout << HIWORD(open) << endl;
        cout << LOWORD(set) << endl;
        cout << HIWORD(set) << endl;
        cout << LOWORD(play) << endl;
        cout << HIWORD(play) << endl;;
        system("pause");
}
Strive Sun
  • 5,988
  • 1
  • 9
  • 26