How can you fix this error from mciSendString() and make it play a sound?

你。 提交于 2021-01-29 11:25:21

问题


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?


回答1:


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");
}


来源:https://stackoverflow.com/questions/56822577/how-can-you-fix-this-error-from-mcisendstring-and-make-it-play-a-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!