【问题标题】:How can you fix this error from mciSendString() and make it play a sound?如何从 mciSendString() 修复此错误并使其播放声音?
【发布时间】:2019-06-30 05:17:29
【问题描述】:

我希望使用 mciSendString 播放基本的 wav 文件,从音频开始的 20 秒开始。 我曾尝试使用它在与程序相同的目录中打开和播放基本的 wav 文件,但无济于事。 这是我的基本代码:

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

控制台的输出是:

0
0
0
0
320
0

所以我知道播放 mciSendString 中有一个错误,它转换为“MCIERR_WAVE_OUTPUTSINUSE”。 这是什么意思,我该如何解决?

【问题讨论】:

  • lpszReturnString 是一个未初始化的缓冲区,所以 lstrlen(lpszReturnString) 是未定义的行为。你可能想要sizeof(lpszReturnString)
  • 你为什么用mciSendString()而不是mciSendCommand()?当您希望能够以秒为单位指定播放位置时,为什么要将时间格式设置为采样?

标签: c++ winapi mcisendstring


【解决方案1】:

另外,由于要从特定位置开始播放WAV文件,比如20秒,需要修改成这样。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2020-06-25
    • 1970-01-01
    • 2013-07-20
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多