找回密码
 加入
搜索
查看: 6571|回复: 14

[AU3基础] 简单播放器的注释[已解决]

  [复制链接]
发表于 2010-3-31 17:43:20 | 显示全部楼层 |阅读模式
本帖最后由 newuser 于 2010-4-1 07:00 编辑

对以下代码的if判断和一些变量的赋值看的不是很懂,请大家帮忙补全相应注释,当然也请修改我的错误注释,在先等待:

#include <GUIConstants.au3>
#include <Sound.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


#NoTrayIcon



#Region ### START Koda GUI section ###
Global $Form1 = GUICreate("muh's mp3 player", 366, 315, 372, 229)
;设计播放器主窗口
GUISetBkColor(0xA6CAF0)
;设置主窗口背景颜色
$Button1 = GUICtrlCreateButton("Play", 192, 24, 57, 33, 0)
$Button2 = GUICtrlCreateButton("Stop", 296, 24, 57, 33, 0)
$Button3 = GUICtrlCreateButton("Player repeat mode", 224, 80, 89, 41, $BS_MULTILINE)
$Slider1 = GUICtrlCreateSlider(184, 168, 169, 25) ;创建调节音量大小的滑动条
;设计3个按钮用于 play ,stop ,player repeat mode
GUICtrlsetBkColor(-1, 0xA6CAF0)
;设计 窗口中控件颜色
GUICtrlSetData(-1, 100)
;修改指定控件的相关数据,但到底是哪个控件被修改了呢?
;GUICtrlSetData ( 控件ID, 数据 [, 默认值] )
$Label1 = GUICtrlCreateLabel("Volume", 240, 144, 58, 24)
;Volume是Static1控件的标题
;创建声音的音量滚动条
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
;为控件设置字体,是为$Label1设置吗?
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\pic.jpg", 8, 8, 169, 241, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
;设置控件的背景图片,可我怎么没看到图片效果?难道需要我在同一目录放置一个pic.jpg,试试
;$Pic1怎么是 Static2控件的背景图片,我是否能让它自动换图片?
$Label2 = GUICtrlCreateLabel("File selected: ", 8, 264, 387, 20)
;$Label2是 Static3控件
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;为$Label2设置字体
$MenuItem3 = GUICtrlCreateMenu("&File")
$MenuItem4 = GUICtrlCreateMenuItem("Load file", $MenuItem3)
;创建子菜单,$MenuItem3表示它是子菜单的父菜单,以下类似
$MenuItem2 = GUICtrlCreateMenu("&Settings")
$MenuItem5 = GUICtrlCreateMenuItem("Exit", $MenuItem2)
$MenuItem1 = GUICtrlCreateMenu("&Help")
$MenuItem6 = GUICtrlCreateMenuItem("About us", $MenuItem1)
;以上是菜单设计,包括下拉菜单
GUISetState(@SW_SHOW)
;显示窗口
#EndRegion ### END Koda GUI section ###



Global $snd="" ;存储MP3文件的变量,默认为空字符
Global $rpd=True; true - repeat song, false - dont repeat
Global $poss[21]
Global $len="a"
;自定义一些用到的全局变量
SoundSetWaveVolume(100)
;调整系统波形音量的百分比大小,在0-100之间,不是调整主音量,即使被设置为 0,也不表示静音
Global $fp="a"

;以下是消息模式的While循环判断,但我没看懂?
;_SoundPlay,_SoundClose我怎么在AutoIt帮助文档找不到帮助信息?
While 1
    $nMsg = GUIGetMsg()
    $pos=_SoundPos($snd, 2) ;什么意思?_SoundLength didn't work for me so here I check if the song has finished to play it again
    If $pos>0 And $pos==$len Then
      _SoundStop($snd)
      If $rpd==True Then
          _SoundPlay($snd)
        EndIf
    EndIf
;if判断是作什么的?
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ;按窗口X关闭主窗口
            _SoundClose($snd)
            Exit
        Case $MenuItem4 ;按Load file子菜单载入歌曲文件
            $fp=FileOpenDialog("Music file", @ScriptDir, "MP3 Files (*.mp3)");文件打开对话框,参数依次是“对话框标题”,“默认打开目录”,打开文件类型
            $snd=_SoundOpen($fp)
            GUICtrlSetData($Label2, "File selected: " & $fp);在$Label2内显示选种的MP3
        Case $Button1
            _SoundPlay($snd);播放MP3
            $len=_SoundLength($snd, 2);不知道起什么作用
        Case $Button2
            _SoundStop($snd);停止正在播放的MP3
        Case $Slider1
            $vm=GUICtrlRead($Slider1) ;读取指定控件的状态或相关数据
            SoundSetWaveVolume($vm) ;设置波形音量百分比
        Case $MenuItem5 ;停止并退出正在播放的MP3
            _SoundStop($snd)
            Exit
        Case $MenuItem6 ;显示“关于”信息
            MsgBox(0, "About us", "This is my first MP3 player yeeeeeey")
        Case $Button3
            $rpd=Not $rpd
        
    EndSwitch
WEnd

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2010-3-31 19:02:56 | 显示全部楼层
#include <GUIConstants.au3>
#include <Sound.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon;无托盘图标



#Region ### START Koda GUI section ### 
Global $Form1 = GUICreate("muh's mp3 player", 366, 315, 372, 229)
;设计播放器主窗口
GUISetBkColor(0xA6CAF0)
;设置主窗口背景颜色
$Button1 = GUICtrlCreateButton("Play", 192, 24, 57, 33, 0)
$Button2 = GUICtrlCreateButton("Stop", 296, 24, 57, 33, 0)
$Button3 = GUICtrlCreateButton("Player repeat mode", 224, 80, 89, 41, $BS_MULTILINE)
$Slider1 = GUICtrlCreateSlider(184, 168, 169, 25) ;创建调节音量大小的滑动条
;设计3个按钮用于 play ,stop ,player repeat mode 
GUICtrlsetBkColor($Slider1, 0xA6CAF0)
;设计 窗口中控件$Slider1颜色
GUICtrlSetData($Slider1, 100)
;修改$Slider1控件的值
;GUICtrlSetData ( 控件ID, 数据 [, 默认值] )
$Label1 = GUICtrlCreateLabel("Volume", 240, 144, 58, 24)
;Volume是Static1控件的标题
;创建声音的音量滚动条
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
;为控件设置字体,是为$Label1设置吗?
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\pic.jpg", 8, 8, 169, 241, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS));脚本同目录下的pic.jpg为背景图
;设置控件的背景图片,可我怎么没看到图片效果?难道需要我在同一目录放置一个pic.jpg,试试
;$Pic1怎么是 Static2控件的背景图片,我是否能让它自动换图片?
$Label2 = GUICtrlCreateLabel("File selected: ", 8, 264, 387, 20)
;$Label2是 Static3控件
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;为$Label2设置字体
$MenuItem3 = GUICtrlCreateMenu("&File")
$MenuItem4 = GUICtrlCreateMenuItem("Load file", $MenuItem3)
;创建子菜单,$MenuItem3表示它是子菜单的父菜单,以下类似
$MenuItem2 = GUICtrlCreateMenu("&Settings")
$MenuItem5 = GUICtrlCreateMenuItem("Exit", $MenuItem2)
$MenuItem1 = GUICtrlCreateMenu("&Help")
$MenuItem6 = GUICtrlCreateMenuItem("About us", $MenuItem1)
;以上是菜单设计,包括下拉菜单
GUISetState(@SW_SHOW)
;显示窗口
#EndRegion ### END Koda GUI section ###



Global $snd="" ;存储MP3文件的变量,默认为空字符
Global $rpd=True; true - 重复播放音乐 , false - 不重复播放
Global $poss[21]
Global $len="a"
;自定义一些用到的全局变量
SoundSetWaveVolume(100)
;调整系统波形音量的百分比大小,在0-100之间,不是调整主音量,即使被设置为 0,也不表示静音
Global $fp="a"

;以下是消息模式的While循环判断,但我没看懂?
;_SoundPlay,_SoundClose在UDF里面,Sound.au3
While 1;无限循环,一个死循环,但是可以在循环内部判断是否要跳出循环
    $nMsg = GUIGetMsg();获取窗口消息
    $pos=_SoundPos($snd, 2) ;获取当前播放的音乐时间
    If $pos>0 And $pos==$len Then;播放时间大于0且播放时间等于当前播放音乐长度时
                _SoundStop($snd);停止播放
                If $rpd==True Then;当变量$rpd为True时,重复播放
                        _SoundPlay($snd)
        EndIf
    EndIf
;上面的if是用来判断音乐是否播放完毕,以及是否要重复播放
    Switch $nMsg
        Case $GUI_EVENT_CLOSE ;按窗口X关闭主窗口
            _SoundClose($snd);关闭播放的音乐
            Exit;退出死循环
        Case $MenuItem4 ;按Load file子菜单载入歌曲文件
            $fp=FileOpenDialog("Music file", @ScriptDir, "MP3 Files (*.mp3)");文件打开对话框,参数依次是“对话框标题”,“默认打开目录”,打开文件类型
            $snd=_SoundOpen($fp);载入选择的mp3文件
            GUICtrlSetData($Label2, "File selected: " & $fp);在$Label2内显示选种的MP3
        Case $Button1
            _SoundPlay($snd);播放MP3
            $len=_SoundLength($snd, 2);取得播放文件的时间长度
        Case $Button2
            _SoundStop($snd);停止正在播放的MP3
        Case $Slider1
            $vm=GUICtrlRead($Slider1) ;读取指定控件的状态或相关数据 
            SoundSetWaveVolume($vm) ;设置波形音量百分比
        Case $MenuItem5 ;停止并退出正在播放的MP3
            _SoundStop($snd);停止音乐播放
            Exit;退出死循环
        Case $MenuItem6 ;显示“关于”信息
            MsgBox(0, "About us", "This is my first MP3 player yeeeeeey")
        Case $Button3
            $rpd=Not $rpd;修改$rpd的值,新的布尔值与旧的相反,既true改为false,false改为true        
    EndSwitch
WEnd

评分

参与人数 1威望 +2 金钱 +25 收起 理由
afan + 2 + 25

查看全部评分

 楼主| 发表于 2010-3-31 19:52:26 | 显示全部楼层
本帖最后由 newuser 于 2010-3-31 19:55 编辑

回复 2# 298311657
非常感谢阁下的回复!不过我还是不清楚以下代码:
    If $pos>0 And $pos==$len Then;播放时间大于0且播放时间等于当前播放音乐长度时
有歌曲播放,$pos肯定大于0为什么要关呀?
当$pos==$len,播放时间等于当前播放音乐长度时,又为什么停止播放呢?如果当前音乐播放到一半的长度 = $pos ?
另外,Global $len="a"  Global $fp="a",该它们赋值字符a又为什么呢 ?
哪里能获得类似 _SoundPos($snd, 2)   的帮助信息呢 ?我开始把 _SoundPos($snd, 2)当成声音的位置坐标呢?可是后来一想声音哪来的位置呢,呵呵!
发表于 2010-3-31 22:37:09 | 显示全部楼层
If $pos>0 And $pos==$len Then;播放时间大于0且播放时间等于当前播放音乐长度时

这里的意思是,音乐开始播放了,而且播放的时间等于音乐文件的时长
发表于 2010-3-31 23:11:36 | 显示全部楼层
谢谢分享代码......
发表于 2010-3-31 23:11:41 | 显示全部楼层
谢谢分享代码......
发表于 2010-4-1 05:55:01 | 显示全部楼层
谢谢分享!!
 楼主| 发表于 2010-4-1 16:30:46 | 显示全部楼层
回复 4# 298311657
再次请教以下代码:
If $pos>0 And $pos==$len Then
      _SoundStop($snd)
      If $rpd==True Then
          _SoundPlay($snd)
        EndIf
    EndIf
现在理解了这段代码的意思,就是歌曲播放完毕就停止,但是测试的时候该repeat重复播放按钮不进行重复播放啊,错误在哪?
发表于 2010-4-1 16:50:25 | 显示全部楼层
很好的代码...
发表于 2010-4-1 17:44:28 | 显示全部楼层
Case $Button3
            $rpd=Not $rpd;修改$rpd的值,新的布尔值与旧的相反,既true改为false,false改为true

这是重复播放选择按钮的代码,点击代码和重复播放的变量值将会进行改变。
也就是$rpd为true的时候,音乐播放完成会重复播放,为false的时候播放完毕就结束
从下面的代码可以看出上面的说法是否正确
If $pos>0 And $pos==$len Then;当音乐播放完毕时,if代码开始作用
      _SoundStop($snd);首先停止播放的音乐
      If $rpd==True Then;又一个if用来判断是否重复播放的变量的值,当值为true时,这个if的代码将被执行
          _SoundPlay($snd);if条件符合时需要做的动作,再次播放刚才结束的音乐
        EndIf
    EndIf
 楼主| 发表于 2010-4-2 16:01:36 | 显示全部楼层
回复 10# 298311657
谢谢,我也是按字面理解,但是没理解上去。
首先,我是这么测试的,播放了一首歌曲并直到歌曲播放结束,那么他应符合
if $pos>0 and $pos=$len
停止歌曲正确,那么此时我按$Button3 重复播放按钮,那么此刻歌曲就应该播放,对吧?
因为此时$rpd=true,但是没有播放歌曲啊?无论我按多少次,都不播放歌曲,说明$Button3根本没起作用?而此时的$rpd应该在true false之间切换无数遍了!
如果说if的上半段代码还能起到判定歌曲播放完毕,停止播放,那么他的下半部分代码就没用?
它的$Button3设置的还有什么意义。
我也不想专牛角尖,可是我是门还未入的人,读别人的代码就尽可能的希望读懂吸收。
谢谢!
发表于 2010-4-3 18:13:16 | 显示全部楼层
Global $rpd=True
变量最初的定义值就是true,你点击一次按钮,它的值就变成false了,如何会重复播放?
发表于 2010-4-3 18:19:14 | 显示全部楼层
#include <GUIConstants.au3>
#include <Sound.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon;无托盘图标



#Region ### START Koda GUI section ###
Global $Form1 = GUICreate("muh's mp3 player", 366, 315, 372, 229)
;设计播放器主窗口
GUISetBkColor(0xA6CAF0)
;设置主窗口背景颜色
$Button1 = GUICtrlCreateButton("Play", 192, 24, 57, 33, 0)
$Button2 = GUICtrlCreateButton("Stop", 296, 24, 57, 33, 0)
$Button3 = GUICtrlCreateButton("Player repeat mode", 224, 80, 89, 41, $BS_MULTILINE)
$Slider1 = GUICtrlCreateSlider(184, 168, 169, 25) ;创建调节音量大小的滑动条
;设计3个按钮用于 play ,stop ,player repeat mode
GUICtrlSetBkColor($Slider1, 0xA6CAF0)
;设计 窗口中控件$Slider1颜色
GUICtrlSetData($Slider1, 100)
;修改$Slider1控件的值
;GUICtrlSetData ( 控件ID, 数据 [, 默认值] )
$Label1 = GUICtrlCreateLabel("Volume", 240, 144, 58, 24)
;Volume是Static1控件的标题
;创建声音的音量滚动条
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
;为控件设置字体,是为$Label1设置吗?
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\pic.jpg", 8, 8, 169, 241, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS));脚本同目录下的pic.jpg为背景图
;设置控件的背景图片,可我怎么没看到图片效果?难道需要我在同一目录放置一个pic.jpg,试试
;$Pic1怎么是 Static2控件的背景图片,我是否能让它自动换图片?
$Label2 = GUICtrlCreateLabel("File selected: ", 8, 264, 387, 20)
;$Label2是 Static3控件
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
;为$Label2设置字体
$MenuItem3 = GUICtrlCreateMenu("&File")
$MenuItem4 = GUICtrlCreateMenuItem("Load file", $MenuItem3)
;创建子菜单,$MenuItem3表示它是子菜单的父菜单,以下类似
$MenuItem2 = GUICtrlCreateMenu("&Settings")
$MenuItem5 = GUICtrlCreateMenuItem("Exit", $MenuItem2)
$MenuItem1 = GUICtrlCreateMenu("&Help")
$MenuItem6 = GUICtrlCreateMenuItem("About us", $MenuItem1)
;以上是菜单设计,包括下拉菜单
GUISetState(@SW_SHOW)
;显示窗口
#EndRegion ### END Koda GUI section ###



Global $snd = "" ;存储MP3文件的变量,默认为空字符
Global $rpd = True; true - 重复播放音乐 , false - 不重复播放
Global $poss[21]
Global $len = "a"
;自定义一些用到的全局变量
SoundSetWaveVolume(100)
;调整系统波形音量的百分比大小,在0-100之间,不是调整主音量,即使被设置为 0,也不表示静音
Global $fp = "a"

$rpd_Label = GUICtrlCreateLabel("$rpd变量的值: " & $rpd, 8, 70, 387, 20);建立一个lable来显示重复播放标志变量的布尔值

;以下是消息模式的While循环判断,但我没看懂?
;_SoundPlay,_SoundClose在UDF里面,Sound.au3
While 1;无限循环,一个死循环,但是可以在循环内部判断是否要跳出循环
        $nMsg = GUIGetMsg();获取窗口消息
        $pos = _SoundPos($snd, 2) ;获取当前播放的音乐时间
        If $pos > 0 And $pos == $len Then;播放时间大于0且播放时间等于当前播放音乐长度时
                _SoundStop($snd);停止播放
                If $rpd == True Then;当变量$rpd为True时,重复播放
                        _SoundPlay($snd)
                EndIf
        EndIf
        ;上面的if是用来判断音乐是否播放完毕,以及是否要重复播放
        Switch $nMsg
                Case $GUI_EVENT_CLOSE ;按窗口X关闭主窗口
                        _SoundClose($snd);关闭播放的音乐
                        Exit;退出死循环
                Case $MenuItem4 ;按Load file子菜单载入歌曲文件
                        $fp = FileOpenDialog("Music file", @ScriptDir, "MP3 Files (*.mp3)");文件打开对话框,参数依次是“对话框标题”,“默认打开目录”,打开文件类型
                        $snd = _SoundOpen($fp);载入选择的mp3文件
                        GUICtrlSetData($Label2, "File selected: " & $fp);在$Label2内显示选种的MP3
                Case $Button1
                        _SoundPlay($snd);播放MP3
                        $len = _SoundLength($snd, 2);取得播放文件的时间长度
                Case $Button2
                        _SoundStop($snd);停止正在播放的MP3
                Case $Slider1
                        $vm = GUICtrlRead($Slider1) ;读取指定控件的状态或相关数据
                        SoundSetWaveVolume($vm) ;设置波形音量百分比
                Case $MenuItem5 ;停止并退出正在播放的MP3
                        _SoundStop($snd);停止音乐播放
                        Exit;退出死循环
                Case $MenuItem6 ;显示“关于”信息
                        MsgBox(0, "About us", "This is my first MP3 player yeeeeeey")
                Case $Button3
                        $rpd = Not $rpd;修改$rpd的值,新的布尔值与旧的相反,既true改为false,false改为true
                        GUICtrlSetData($rpd_Label, "$rpd变量的值: " & $rpd);修改$rpd_Label控件的显示数据
        EndSwitch
WEnd
看下这段吧
 楼主| 发表于 2010-4-4 12:31:59 | 显示全部楼层
回复 12# 298311657

在点第2次不就true了,可也波播放啊!
发表于 2016-4-13 06:13:26 | 显示全部楼层
解释很细致
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-29 04:15 , Processed in 0.092993 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表