首页 > 开发 > 综合 > 正文

无外部控件制作多媒体播放器(一)

2024-07-21 02:16:09
字体:
来源:转载
供稿:网友


利用mci指令制作播放器,简单实用,很适合于做为自己软件的一个附带功能或背景音乐,正是基于这点需求,我准备分几个部分来写:

1、mci指令的简单使用;
2、媒体播放的进度控制与音量调节;
3、音乐信息的读取,包括mp3(id3v1 & id3v2)与asf(wma & wmv)等;
4、音乐列表的建立与保存(m3u格式)

本来主要是想写播放音乐的,举个播放视频的例子,没什么别的意思,只是感觉播放音乐实在是简单,没什么可写,同时也是为了说明,mci放视频也是可以的。

private const ws_child = &h40000000
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
private declare function getshortpathname lib "kernel32" alias "getshortpathnamea" (byval lpszlongpath as string, byval lpszshortpath as string, byval cchbuffer as long) as long
private declare function setwindowpos lib "user32" (byval hwnd as long, byval hwndinsertafter as long, byval x as long, byval y as long, byval cx as long, byval cy as long, byval wflags as long) as long
private function shortname(lname as string) as string
    '取得短文件名
    dim s as string, i as long
    i = 512
    s = space$(i)
    getshortpathname lname, s, i
    shortname = left$(s, instr(1, s, vbnullchar) - 1)
end function

private function playmci(cmd as string, optional returnstr as string) as long
    '播放mci
    dim s as string
    s = space$(256)
    playmci = mcisendstring(cmd, s, 256, 0)
    returnstr = left$(s, instr(1, s, vbnullchar) - 1)
end function

private function showvideo(strfilename as string, hwd as long, x as long, y as long, w as long, h as long) as long
    dim i as long, s as string
    if dir(strfilename, vbhidden or vbreadonly or vbsystem) = vbnullstring or strfilename = vbnullstring then exit function
    i = playmci("open """ & shortname(strfilename) & """ alias song parent " & hwd & " style " & ws_child & " wait")
    if i <> 0 then exit function
    i = playmci("status song window handle wait", s)
    if i <> 0 then goto fail
    i = val(s)
    if i = 0 then goto fail
    setwindowpos i, 0, x, y, w, h, 0
    playmci "play song"
    showvideo = i   '若成功返回视频窗口的句柄
    exit function
fail:
    playmci "close song"
end function

private sub cmdplay_click()
    i=showvideo("h:/1.wmv", me.hwnd, 0, 0, 100, 100)
   '返回的这个句柄,很有用的,可用于移动窗口位置,或subclass它,加上弹出菜单,响应鼠标动作等
    if i <> 0 then
        cmdplay.enabled = false
        cmdstop.enabled = true
    end if
end sub
private sub cmdstop_click()
    playmci "close song"
    cmdplay.enabled = true
    cmdstop.enabled = false
end sub

private sub form_load()
    me.scalemode = 3
    cmdplay.enabled = true
    cmdstop.enabled = false
    cmdplay.caption = "播放"
    cmdstop.caption = "停止"
end sub
private sub form_unload(cancel as integer)
    playmci "close song"
end sub


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表