mciSendString函数

#include <iostream>
#include <Windows.h>  //Unicode字符串需要用到
#include <conio.h> //_getch获取用户输入需要用到
#pragma comment (lib,"winmm.lib")//播放音乐函数需要用到MCIERROR mciSendString(LPCTSTR lpszCommand,//命令字符串LPTSTR  lpszReturnString,//指向接收返回信息的缓冲区,为NULL时不返回信息UINT    cchReturn,//上述缓冲区大小HANDLE  hwndCallback//在命令串中含通知时,它指定一个回调窗口的句柄,一般为NULL
);

后面三个参数似乎暂时不需要,就全部默认填上行了。

第一个参数

常用命令

1.open:

_stprintf_s(lpszCommand, TEXT("open %s %s %s"), lpszDevice, //设备名称,就是媒体名字lpszOpenFlags, lpszFlags
); 

eg:mciSendString(L"open music.wav alias BGM", NULL, 0, NULL)

music.wav : 媒体名字

alias BGM: 起别名为"BGM"

2.play

这里举几个例子 "play BGM form 3000 to 5000"

BGM:   别名

form 3000  :从第3秒开始播放(3*1000毫秒)

to 5000:到第5秒结束。

play的参数表

Value Meaning
at time Indicates when the device should begin performing this command, or, if the device has been cued, when the cued command begins. For more information, see the cue command.
fast Indicates that the device should play faster than normal. To determine the exact speed on a videodisc player, use the "speed" flag of the status command. To specify the speed more precisely, use the "speed" flag of this command.
from position Specifies a starting position for the playback. If the "from" flag is not specified, playback begins at the current position. For cdaudio devices, if the "from" position is greater than the end position of the disc, or if the "from" position is greater than the "to" position, the driver returns an error. For videodisc devices, the default positions are in frames for CAV discs and in hours, minutes, and seconds for CLV discs.
fullscreen Specifies that a full-screen display should be used. Use this flag only when playing compressed files. (Uncompressed files won't play full-screen.)
repeat Specifies that playback should restart when the end of the content is reached.
reverse Specifies that the play direction is backward. You cannot specify an ending location with the "reverse" flag. For videodiscs, "scan" applies only to CAV format.
scan Plays as fast as possible without disabling video (although audio might be disabled). For videodiscs, "scan" applies only to CAV format.
slow Plays slowly. To determine the exact speed on a videodisc player, use the "speed" flag of the status command. To specify the speed more precisely, use the "speed" flag of this command. For videodiscs, "slow" applies only to CAV format.
speed integer Plays a videodisc at the specified speed, in frames per second. This flag applies only to CAV discs.
to position Specifies an ending position for the playback. If the "to" flag is not specified, playback stops at the end of the content. For cdaudio devices, if the "to" position is greater than the end position of the disc, the driver returns an error. For videodisc devices, the default positions are in frames for CAV discs and in hours, minutes, and seconds for CLV discs.
window Specifies that playing should use the window associated with the device instance. This is the default setting.

下面是两个使用例

void play() {//打开music.wav文件起个新名字BGMmciSendString(L"open music.wav alias BGM", NULL, 0, NULL);mciSendString(L"play BGM", NULL, 0, NULL);while (1);//防止马上停止
}
void play2() {//但也可以直接播放,不取名字mciSendString(L"play music.wav", NULL, 0, NULL);while (1);//防止马上停止
}

其他常用指令

1)停止(pause ):"pause music"

2)继续(Resume):“resume music”
注意:暂停后发出Play命令也可以继续

3)停止:"stop music"

4)关闭:"close music"

5)快进/后退一个位置:"step music by frames reverse"  下划线为参数

这个好像是视频用的,音频用不了

6)获取进度:"status music position"

7)获取长度:"status music length"        同上都是status命令,但是参数不同

结合鼠标消息

#define PLAY 1
#define STOP 0void play() {mciSendString(L"open music.wav alias BGM", NULL, 0, NULL);while (1) {input=mouse();if (input == PLAY)mciSendString(L"play BGM from 9000 ", NULL, 0, NULL);if(input==STOP)mciSendString(L"pauseBGM", NULL, 0, NULL);}
}void Button() {TCHAR str[] = TEXT("播放");settextstyle(60, 35, str);outtextxy(500, 250, str);TCHAR str2[] = TEXT("暂停");settextstyle(60, 35, str2);outtextxy(500, 450, str2);
}int isButton(ExMessage mouse) {if (mouse.x > 500 && mouse.x < 500 + 60 && mouse.y>250 && mouse.y < 250 + 35)return PLAY;if (mouse.x > 500 && mouse.x < 500 + 60 && mouse.y>450 && mouse.y < 450 + 35)return STOP;return -1;
}int mouse() {ExMessage mouse;while (1) {mouse = getmessage(EM_MOUSE);if (mouse.message == WM_LBUTTONDOWN) {return isButton(mouse);}}
}void ini() {initgraph(1200, 600);Button();play3();
}

效果

可以播放音乐,也可以暂停,至于按钮的位置只进行了粗略的计算,学习函数使用为主。

参考博客  C/C++笔记之播放音乐的函数_搬砖的码蚁的博客-CSDN博客_music函数

参考文档MCI Command Strings - Win32 apps | Microsoft Docs

C/C++播放音乐的函数的学习相关推荐

  1. C/C++笔记之播放音乐的函数

    前缀为mci(Media Control Iterface): //头文件mmsystem.h或windows.h //需要添加静态库 (lib,"winmm.lib") //支持 ...

  2. Android播放音乐案例分享

    本文实例为大家分享了Android播放音乐案例的具体实现代码,供大家参考,具体内容如下 分析: 和上一篇文章的结构是一样的,只不过我们需要在这里里面加上播放音乐的一些操作: 其实也就是调用系统的播放音 ...

  3. STM32 使用STM32F103控制无源蜂鸣器发声播放音乐

    使用STM32控制无源蜂鸣器发声播放音乐 [注:学习STM32总结做的笔记,大神勿喷.有不足之处还望不吝赐教. -----本篇大部分转载而来,有侵权请联系作者,谢谢.] 原博客地址: https:// ...

  4. Vue实现音乐播放效果(适合小白学习)

    Vue实现音乐播放效果(适合小白学习) 播放器介绍 五个html主要标签 功能介绍 (1)搜索 (2)播放效果: 完整代码: 播放器介绍 总体的效果如图所示,没有加很多css特效,需要美化的小白,可以 ...

  5. mciSendString函数简介(播放音乐以及录音相关操作)

    函数功能:播放多媒体音乐,视频等 mciSendString是用来播放多媒体文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等.这个函数有自己的mci指令,可以通过不同的指令实现不同的功 ...

  6. Harmony OS 设备开发学习记录(六)-- 通过PWM模块播放音乐

    Harmony OS 设备开发学习记录(六)-- 通过PWM模块播放音乐 基于hispark wifi套件采用harmony os 2.0全量代码 一.看原理图确定硬件电路 本例采用红绿灯扩展版上的蜂 ...

  7. STC-B学习板蜂鸣器播放音乐

    功能:单片机烧录好程序后,按key1按键播放<IF YOU>,再次按key1可暂停播放,按key2可切换歌曲到<国际歌>,再按key3可切换歌曲到<孤勇者> 芯片: ...

  8. 用户可以通过音乐播放器来选择以及学习自己喜欢的歌曲

    很多人都难以理解音乐发烧友对音乐的热爱,虽然静哥也难以理解.如今随着移动互联网的发展,音乐产品在移动端的需求并没有下降.根据CNNIC发布的第33次<中国互联网发展状况统计报告>显示,在2 ...

  9. C/C++里面使用loadimage导入图片和使用miSendString播放音乐函数运行不显示图片不播放音乐的解决问题

    1.查看图片格式是否与代码里面的一样是否已.jpe. jpeg.png等图片后缀名,可以选种图片右键属性查看.(https://img-blog.csdnimg.cn/3bbe80f956324a9e ...

最新文章

  1. springboot mybatis plus 关闭驼峰式命名转换为下划线
  2. 在Mac OS X中配置Apache + PHP + MySQL
  3. Hadoop源码阅读-HDFS-day2
  4. AI和大数据下,前端技术将如何发展?
  5. 一文快速了解oCPX
  6. 【Java】最基本的命令行登录程序Demo
  7. CentOS上搭建Nginx + Mono 运行 asp.net
  8. 如何用java写单链表_如何使用Java实现单链表?
  9. python画图显示中文乱码_解决Python pandas plot输出图形中显示中文乱码问题
  10. inside the C++ Object model总结
  11. RecycleView的普通适配器(另加RecycleView的格局格式)
  12. Java实现PDF生成(Word文档转Pdf)
  13. VC 消息钩子编程
  14. 036多级节点实现层叠展开与收缩的功能
  15. 置信区间、P值那点事
  16. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之项目设计
  17. 输入三个整数a,b,c。并进行两两相加,最后比较相加和的最大值。
  18. 码流格式: Annex-B, AVCC(H.264)与HVCC(H.265), extradata详解
  19. python(2048)
  20. 消息称:华为将官宣为全国老款手机内存扩容

热门文章

  1. Matlab中inv函数的使用
  2. CCF201809-1 卖菜(JAVA)
  3. ccf认证--201809-1 卖菜(100分)
  4. 如何使用云桌面系统提高涉密网络安全性与方便性
  5. SOT-23封装如何查找元件型号
  6. 白话空间统计二十五:空间权重矩阵外篇:功能地理学
  7. 淘气的小丁-简易聊天器
  8. cura切片操作学习
  9. [每周心学]示弟立志说(附译文)
  10. qlikview从mysql中抽取数据的过程,mysql数据同步到sqlserver的过程