C++中PlaySound函数的正确使用 [英] Correct use of PlaySound function in C++
问题描述
我在使用内置函数 PlaySound 时遇到了一些问题.我不断收到两个错误,第一个是:
I'm having some issues utilizing the built-in function PlaySound. I continuously receive two errors, the first being:
const char *"类型的参数与LPCWSTR"类型的参数不兼容,
argument of type "const char *" is incompatible with parameter of type "LPCWSTR",
第二个是:
'BOOL PlaySoundW(LPCWSTR,HMODULE,DWORD)':无法将参数 1 从 'const char [35]' 转换为 'LPCWSTR'.
'BOOL PlaySoundW(LPCWSTR,HMODULE,DWORD)': cannot convert argument 1 from 'const char [35]' to 'LPCWSTR'.
我似乎无法自己解决这些问题,并且需要一些帮助来弄清楚如何摆脱这些错误.这是我的源代码的一部分,包括我认为导致错误的内容.
I can't seem to resolve these issues on my own, and would like some help with figuring out how to get rid of the errors. Here's a section of my source code, including what I believe to be causing the error.
#include <iostream>
#include <string>
#include <iomanip>
#include <dos.h>
#include <windows.h>
#include <playsoundapi.h>
#include <mmsystem.h>
using namespace std;
int main()
{
PlaySound("C:\\Users\\Cristian\\Desktop\\cafe.mp3", NULL, SND_FILENAME | SND_ASYNC);
return 0;
}
如果我错误地使用了 PlaySound 功能,请指出正确的方向.
If I am using the PlaySound function incorrectly,please point me to the correct direction.
推荐答案
LPCWSTR
是 const wchar_t *
的宏 - 所以你需要使用一个宽字符 wchar_t
字符串 L""
而不是普通的 char
字符串 ""
.
LPCWSTR
is a macro for const wchar_t *
- so you need to use a wide-character wchar_t
string L""
instead of a normal char
string ""
.
const wchar_t* path = L"C:\\Users\\Cristian\\Desktop\\cafe.mp3";
PlaySound( path , NULL, SND_FILENAME | SND_ASYNC );
老式的 Win32 方法是将 TCHAR
与可选的 #define UNICODE
一起使用,但这被认为是不合时宜的,因为ANSI"Win32 函数不支持UCS-2/UTF-16(令人惊讶的是,MBCS 并不指 UTF-8).
The old-school Win32 way would be to use TCHAR
with optional #define UNICODE
but this is considered an anachronism as the "ANSI" Win32 functions don't support UCS-2/UTF-16 (and MBCS does not refer to UTF-8, surprisingly).
请注意,您可能希望使用 SND_SYNC
而不是 SND_ASYNC
,因为您的程序将在声音结束播放之前终止.
Note that you probably want to use SND_SYNC
instead of SND_ASYNC
because your program will terminate before the sound finishes plaiyng.
最后,PlaySound
不支持 MP3 文件——只支持 Wave 文件——所以你的代码无论如何都无法工作.
Finally, PlaySound
does not support MP3 files - only Wave files - so your code won't work regardless.
要在 Win32 中播放 MP3 文件,您需要使用:
To play MP3 files in Win32 you need to use either:
MCI(媒体控制接口 - 来自 Win3x 时代的古老 API,但令人惊讶的是它是最简单的 - 只需要两个函数调用):
MCI (Media Control Interface - an ancient API from the Win3x days, yet surprisingly is the simplest - needing only a two function calls):
mciSendString("open \"fileName.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
DirectShow - 这是 官方的 Windows 多媒体 API,但它基于 COM 并且需要您创建组件图(文件解析器、解码器、输出设备等),以便有一个陡峭的学习曲线.请参阅此处了解所需的最低代码 - 近 60 行(https://msdn.microsoft.com/en-us/library/windows/desktop/dd389098.aspx )
DirectShow - This is the official Windows multimedia API, but it's based on COM and requires you to create a component-graph (file parser, decoders, output devices, etc) so it has a steep learning curve. See here for the minimum code required - almost 60 lines ( https://msdn.microsoft.com/en-us/library/windows/desktop/dd389098.aspx )
Windows Vista 引入了 MediaFoundation 来取代 DirectShow,但根据我的经验,它在程序员人体工程学方面并不比 DirectShow 好多少:https://msdn.microsoft.com/en-us/library/windows/desktop/ms703190(v=vs.85).aspx
Windows Vista introduced MediaFoundation to replace DirectShow, but in my experience it's not much better than DirectShow in terms of programmer-ergonomics: https://msdn.microsoft.com/en-us/library/windows/desktop/ms703190(v=vs.85).aspx
对于 Windows 10,有一个用于播放的 WinRT API - 但我没有做太多研究,我不知道您是否可以从真正的"Win32 程序中调用它,或者它是否仅保留用于沙盒UWP 应用程序:https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/media-playback
For Windows 10, there is a WinRT API for playback - but I haven't done much research and I don't know if you can call it from "real" Win32 programs or if it's reserved only for sandboxed UWP applications: https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/media-playback
这篇关于C++中PlaySound函数的正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!