做个mp3播放器,用realplay和WMP做出来的程序内存占用太大。
/****
mciSendString
The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string.
MCIERROR mciSendString(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);
Parameters
lpszCommand
Address of a null-terminated string that specifies an MCI command string. For more information about the command strings, see Command Strings.
lpszReturnString
Address of a buffer that receives return information. If no return information is needed, this parameter can be NULL.
cchReturn
Size, in characters, of the return buffer specified by the lpszReturnString parameter.
hwndCallback
Handle of a callback window if the "notify" flag was specified in the command string.
Return Values
Returns zero if successful or an error otherwise. The low-order word of the returned doubleword value contains the error return value. If the error is device-specific, the high-order word of the return value is the driver identifier; otherwise, the high-order word is zero. For a list of possible error values, see Constants: MCIERR Return Values.
To retrieve a text description of mciSendString return values, pass the return value to the mciGetErrorString function.
*****/
如果你仅仅是播放MP3,建议使用API函数mciSendString,我把该函数的详细资料罗列如下供你参考。
Option Explicit
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal
lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
mciSendString是用来播放多媒体文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,下面介绍
它的使用方法:
该函数有四个参数:
第一个参数:要发送的命令字符串。字符串结构是:[命令][设备别名][命令参数].
第二个参数:返回信息的缓冲区,为一指定了大小的字符串变量.
第三个参数:缓冲区的大小,就是字符变量的长度.
第四个参数:回调方式,一般设为零
返回值:函数执行成功返回零,否则返回错误代码
一、常用命令
1.打开(Open),格式:Open 设备名 [type 设备型式][alias 别名]
Dim mName as string
mName = "f:\\mpeg\\mpeg1.avi"
mciSendString "open mName type MPEGVideo Alias movie parent %u Style %u notify",0&, 0, 0
其中:
open 操作命令
mName 全路径文件名
type MPEGVideo 是指打开MPEG,AVI等类型,如果不加这一句,就是打开WAV,MP3等
Alias movie 定义了该操作的别名为movie,后续操作只要指明别名即可
parent %u 源
Style %u 样式
notify 通知
2.播放(Play),格式:Play 设备名 [from 起点][to 终点]
mciSendString "play movie", 0&, 0, 0
mciSendString "play movie fullscreen", 0&, 0, 0 '全屏播放
3.暂停(Pause):
mciSendString "pause movie", 0&, 0, 0
4.继续(Resume):
mciSendString "resume movie", 0&, 0, 0
5.停止(Stop):
mciSendString "stop movie", 0&, 0, 0
6.关闭(Colse):
mciSendString "close movie", 0&, 0, 0
7.前进到下一个位置:
mciSendString "step movie", 0&, 0, 0
8.后退到上一个位置:
mciSendString "step movie reverse", 0&, 0, 0
9.前进或后退 N 個位置(其中 N<0 即表示后退)
mciSendString "step movie by " & str(N), 0&, 0, 0
10.获取当前播放位置:
Dim ST As String*64
mciSendString "status movie position", st, len(st), 0
11. 获取媒体的总长度:
mciSendString "status movie length", st, len(st), 0
l=val(st) 'l就是所播放文件的长度
12.获取播放当前状态:
Dim ST As String*64
mciSendString "status movie mode", ST, Len(ST), 0
If Left(ST, 7) = "stopped" Then (处理代码) '播放完毕
13.循环播放:
mciSendString "play movie repeat", 0&, 0, 0
二、控制声音大小(1-1000):
Dim V As Long
mciSendString "status movie volume",&V, len(v), 0 'V是获取的音量大小值。
V = 50
mciSendString "setaudio movie volume to 数值", 0, 0, 0 'V是设置的音量值
三、设置播放位置.(需事先设定时间格式),格式:Seek 设备名 [to 位置 | to start | to end]
Dim P1 as Long, P2 as Long
P1 = 100: P2 = 3000
mciSendString "seek movie to ", P1, 0, 0 'P1是当前起始位置,单位:毫秒
mciSendString "seek movie to start", 0&, 0, 0 '定位到开头位置
mciSendString "play movie", 0&, 0, 0 '定位后再播放
或者:
mciSendString "play movie FROM P1 to P2",0&, 0, 0 'P1是起始位置,P2是停止位置。单位:毫秒
mciSendString "seek movie to end", 0&, 0, 0 '定位到最后位置
四、在指定控件上播放视频:
mciSendString "open AVI 文件名 parent hWnd style child", 0&, 0, 0
其中,hWnd 是控件的句柄
执行上述命令之后,影片会被放置在控件的左上角,且影片的大小不受控件大小的影响,如果想要改变
影片播放的位置及大小,可以在執行 play 指令前先执行 put 指令,格式如下:
mcisendString "put AVI 文件名 window at X Y [Width Height]", 0&, 0, 0
其中:X、Y为影片左上角坐标,Width、Height为影片的宽高度
五、如果播放视频还可控制亮度(1-2000)
Dim B As Long
mciSendString "status movie brightness", B, 0, 0 'B是获取的亮度值。
B = 50
mciSendString "setvideo movie brightness to " & B, &0, 0, 0 'B是设置的亮度值
六、录音设置:
录音前,用以下语句初始化
1.设为8位:
mciSendString "set wave bitpersample 8", "", 0, 0
2.设为11025Hz
mciSendString "set wave samplespersec 11025", "", 0, 0
3.设为立体声:
mciSendString "set wave channels 2", "", 0, 0
4.实现PCM格式(不一定正确):
MCISENDSTRING "set wave format tag pcm","", 0, 0
5.开始录音:
mciSendString "close movie",0&,0,0
mciSendString "open new type WAVEAudio alias movie",0&,0,0
mciSendString "record movie",0&,0,0
6.保存录音到c:\123.wav
mciSendString "stop movie",0&,0,0
mciSendString "save movie C:\\123.wav",0&,0,0
mciSendString "close movie",0&,0,0
七、开关光驱:
mciSendString "set cdaudio door open", "", 0, 0 '打开
mciSendString "set cdaudio door close", "", 0, 0 '关闭
八、其它
1.设置设备的各种状态(Set)
Set alias_name[audio all off][audio all on][time format ms]:
Set命令用来设置设备的各种状态.如:静音,有声音,时间格式为毫秒等.
2.取得设备的状态(Status)
Status alias_name[length][mode][position]:
Status命令用来取得设备的状态.如:该媒体文件的长度,该媒体文件所处状态,该媒体文件的当前位置等. 的长度,该媒体文件所处状态,该
媒体文件的当前位置等.
参考代码:
TCHAR fileName[]="D:\\俺的文档\\my music\\爷爷泡的茶.mp3";
TCHAR shortName[MAX_PATH];
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
TCHAR cmd[MAX_PATH+10];
wsprintf(cmd,"play %s",shortName);
mciSendString(cmd,"",NULL,NULL);
(调用mciSendString第一个参数传“play 文件全路径”就可以,“文件全路径”最好传绝对路径,不建议wanghepeng10那样值传递文件名。
另外如果文件全路径中含有空格的话要使用GetShortPathName转换成短路径。)