如何在c ++程序中播放或打开* .mp3或* .wav声音文件? [英] How to play or open *.mp3 or *.wav sound file in c++ program?

查看:2185
本文介绍了如何在c ++程序中播放或打开* .mp3或* .wav声音文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是计算机科学的学生。我有一个最后一学期项目,以开发一个短小的游戏在图形和声音。我搜索了很多网站,但已经没有有效的答案,我的查询累了。我在这里问这个问题...等待最佳解决方案...

解决方案

首先,代码:

  #include< Mmsystem.h> 
#include< mciapi.h>
//这两个标头已经包含在< Windows.h> header
#pragma comment(lib,Winmm.lib)

打开*。 mp3:

  mciSendString(open \*。mp3\type mpegvideo alias mp3,NULL,0,NULL ); 

播放* .mp3:

  mciSendString(play mp3,NULL,0,NULL); 

播放并等待* .mp3播放完毕:

  mciSendString(play mp3 wait,NULL,0,NULL); 

要重播(从头开始播放)* .mp3:

  mciSendString(play mp3 from 0,NULL,0,NULL); 

要重播,等待* .mp3播放完毕:

  mciSendString(play mp3 from 0 wait,NULL,0,NULL); 

播放* .mp3并在每次结束时循环播放:

  mciSendString(play mp3 repeat,NULL,0,NULL); 

如果你想在* .mp3播放完毕后做些什么,通过 WNDCLASSEX 结构, CreateWindowEx 创建的C $ c> RegisterClassEx 循环中的 GetMessage TranslateMessage DispatchMessage >

  mciSendString(play mp3 notify,NULL,0,hwnd); // hwnd是从CreateWindowEx返回的窗口的句柄。如果这不工作,那么用MAKELONG(hwnd,0)替换hwnd。 

在窗口过程中,添加 case MM_MCINOTIFY: 如果您对控制台进行了编程,则会在mp3播放完毕后执行。



< >应用程序,并且您不处理 Windows ,那么您可以通过指定 CREATE_SUSPENDED 来暂停状态 CreateThread code> dwCreationFlags 参数中的code>标志,并将返回值保存在 static 变量中,想。例如,我称之为mp3。当然, static 变量的类型是 HANDLE



c> cp $

  DWORD WINAPI MP3Proc(_In_ LPVOID lpParameter)// lpParameter可以是一个指向存储数据的结构的指针,这些数据不能在此函数之外访问。你可以在`CreateThread`之前准备这个结构,并在`lpParameter`中给出它的地址。
{
Data * data =(Data *)lpParameter; //如果你调用这个结构Data,但你可以调用它任何你想要的。
while(true)
{
mciSendString(play mp3 from 0 wait,NULL,0,NULL)
//在mp3播放结束时你想做什么
SuspendThread(GetCurrentThread()); //或者你保存在静态变量中的这个线程的句柄
}
}

所有你现在要做的就是 ResumeThread(mp3); 每次你想重放你的mp3,会有一些会发生每次完成。 / p>

您可以 #define play_my_mp3 ResumeThread(mp3); 让您的代码更易读。



当然你可以删除 while(true) SuspendThread strong> from 0 代码,如果您只想播放mp3文件一次,并在结束后执行任何您想要的操作。



如果删除 SuspendThread 呼叫,那么声音会一遍又一遍播放,并在结束时执行操作。这等同于:

  mciSendString(play mp3 repeat notify,NULL,0,hwnd); //或MAKELONG(hwnd,0)而不是


暂停中间的* .mp3:

  mciSendString(pause mp3,NULL,0 , 空值); 

并恢复:

  mciSendString(resume mp3,NULL,0,NULL); 

在中间停止:

  mciSendString(stop mp3,NULL,0,NULL); 

请注意,您无法恢复已停止但仅暂停的声音,通过执行播放命令。
当你玩完这个* .mp3后,不要忘记:

  mciSendString ,NULL,0,NULL); 

所有这些操作也适用于波形文件使用waveaudio而不是mpegvideo。
也可以直接播放而不打开它们:

  PlaySound(*。wav,GetModuleHandle ),SND_FILENAME); 

如果不想指定模块的句柄:

  sndPlaySound(*。wav,SND_FILENAME); 

如果您不想等到播放结束:

  PlaySound(*。wav,GetModuleHandle(NULL),SND_FILENAME | SND_ASYNC); 
//或
sndPlaySound(*。wav,SND_FILENAME | SND_ASYNC);

一遍又一遍播放波形文件:


$ b b

  PlaySound(*。wav,GetModuleHandle(NULL),SND_FILENAME | SND_ASYNC | SND_LOOP); 
//或
sndPlaySound(*。wav,SND_FILENAME | SND_ASYNC | SND_LOOP);请注意,您必须同时指定 SND_ASYNC
<和 SND_LOOP 标志,因为你永远不会等到一个重复自己无数次的声音结束了。



也可以 fopen 波形文件,并将其所有字节复制到缓冲区(一个巨大/巨大(非常大)的字节数组)与 fread function然后:

  PlaySound(buffer,GetModuleHandle(NULL),SND_MEMORY); 
//或
PlaySound(buffer,GetModuleHandle(NULL),SND_MEMORY | SND_ASYNC);
//或
PlaySound(buffer,GetModuleHandle(NULL),SND_MEMORY | SND_ASYNC | SND_LOOP);
//或
sndPlaySound(buffer,SND_MEMORY);
//或
sndPlaySound(buffer,SND_MEMORY | SND_ASYNC);
//或
sndPlaySound(buffer,SND_MEMORY | SND_ASYNC | SND_LOOP);

OpenFile CreateFile CreateFile2 ReadFile ReadFileEx 函数可以代替 fopen fread 函数。



希望这完全回答你的问题。


I'm a student of Computer Science. I have a final semester Project to develop a short game in graphics along with sound. I have search out many websites but got tired after having no efficient answer for my query. I'm asking this question here... waiting for a optimal solution...

解决方案

First of all, write the following code:

#include <Mmsystem.h>
#include <mciapi.h>
//these two headers are already included in the <Windows.h> header
#pragma comment(lib, "Winmm.lib")

To open *.mp3:

mciSendString("open \"*.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);

To play *.mp3:

mciSendString("play mp3", NULL, 0, NULL);

To play and wait until the *.mp3 has finished playing:

mciSendString("play mp3 wait", NULL, 0, NULL);

To replay (play again from start) the *.mp3:

mciSendString("play mp3 from 0", NULL, 0, NULL);

To replay and wait until the *.mp3 has finished playing:

mciSendString("play mp3 from 0 wait", NULL, 0, NULL);

To play the *.mp3 and replay it every time it ends like a loop:

mciSendString("play mp3 repeat", NULL, 0, NULL);

If you want to do something when the *.mp3 has finished playing, then you need to RegisterClassEx by the WNDCLASSEX structure, CreateWindowEx and process it's messages with the GetMessage, TranslateMessage and DispatchMessage functions in a while loop and call:

mciSendString("play mp3 notify", NULL, 0, hwnd); //hwnd is an handle to the window returned from CreateWindowEx. If this doesn't work, then replace the hwnd with MAKELONG(hwnd, 0).

In the window procedure, add the case MM_MCINOTIFY: The code in there will be executed when the mp3 has finished playing.

But if you program a Console Application and you don't deal with windows, then you can CreateThread in suspend state by specifying the CREATE_SUSPENDED flag in the dwCreationFlags parameter and keep the return value in a static variable and call it whatever you want. For instance, I call it mp3. The type of this static variable is HANDLE of course.

Here is the ThreadProc for the lpStartAddress of this thread:

DWORD WINAPI MP3Proc(_In_ LPVOID lpParameter) //lpParameter can be a pointer to a structure that store data that you cannot access outside of this function. You can prepare this structure before `CreateThread` and give it's address in the `lpParameter`
{
    Data *data = (Data*)lpParameter; //If you call this structure Data, but you can call it whatever you want.
    while (true)
    {
        mciSendString("play mp3 from 0 wait", NULL, 0, NULL);
        //Do here what you want to do when the mp3 playback is over
        SuspendThread(GetCurrentThread()); //or the handle of this thread that you keep in a static variable instead
    }
}

All what you have to do now is to ResumeThread(mp3); every time you want to replay your mp3 and something will happen every time it finishes.

You can #define play_my_mp3 ResumeThread(mp3); to make your code more readable.

Of course you can remove the while (true), SuspendThread and the from 0 codes, if you want to play your mp3 file only once and do whatever you want when it is over.

If you only remove the SuspendThread call, then the sound will play over and over again and do something whenever it is over. This is equivalent to:

mciSendString("play mp3 repeat notify", NULL, 0, hwnd); //or MAKELONG(hwnd, 0) instead

in windows.

To pause the *.mp3 in middle:

mciSendString("pause mp3", NULL, 0, NULL);

and to resume it:

mciSendString("resume mp3", NULL, 0, NULL);

To stop it in middle:

mciSendString("stop mp3", NULL, 0, NULL);

Note that you cannot resume a sound that has been stopped, but only paused, but you can replay it by carrying out the play command. When you're done playing this *.mp3, don't forget to:

mciSendString("close mp3", NULL, 0, NULL);

All these actions also apply to (work with) wave files too, but with wave files, you can use "waveaudio" instead of "mpegvideo". Also you can just play them directly without opening them:

PlaySound("*.wav", GetModuleHandle(NULL), SND_FILENAME);

If you don't want to specify an handle to a module:

sndPlaySound("*.wav", SND_FILENAME);

If you don't want to wait until the playback is over:

PlaySound("*.wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC);
//or
sndPlaySound("*.wav", SND_FILENAME | SND_ASYNC);

To play the wave file over and over again:

PlaySound("*.wav", GetModuleHandle(NULL), SND_FILENAME | SND_ASYNC | SND_LOOP);
//or
sndPlaySound("*.wav", SND_FILENAME | SND_ASYNC | SND_LOOP);

Note that you must specify both the SND_ASYNC and SND_LOOP flags, because you never going to wait until a sound, that repeats itself countless times, is over!

Also you can fopen the wave file and copy all it's bytes to a buffer (an enormous/huge (very big) array of bytes) with the fread function and then:

PlaySound(buffer, GetModuleHandle(NULL), SND_MEMORY);
//or
PlaySound(buffer, GetModuleHandle(NULL), SND_MEMORY | SND_ASYNC);
//or
PlaySound(buffer, GetModuleHandle(NULL), SND_MEMORY | SND_ASYNC | SND_LOOP);
//or
sndPlaySound(buffer, SND_MEMORY);
//or
sndPlaySound(buffer, SND_MEMORY | SND_ASYNC);
//or
sndPlaySound(buffer, SND_MEMORY | SND_ASYNC | SND_LOOP);

Either OpenFile or CreateFile or CreateFile2 and either ReadFile or ReadFileEx functions can be used instead of fopen and fread functions.

Hope this fully answers perfectly your question.

这篇关于如何在c ++程序中播放或打开* .mp3或* .wav声音文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆