网上看到可以通过如下方式播放声音,
Private Declare Function sndPlaySound Lib "winmm" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Const SND_ASYNC = &H1
Dim wavString As StringsndPlaySound wavFilePath, SND_ASYNC但是我的声音文件比较小,就500bty, 所以能否把声音集成在程序中,不去调外部声音文件?

解决方案 »

  1.   

    做成资源文件,集成在EXE文件中,播放还是用sndPlaySound函数
      

  2.   

    下面是我以前写的一个文档:
    API函数PlaySound允许播放WAV格式的声音文件,它的API申明格式是这样的:
    Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    其中对参数lpszSound的说明是这样的:
    lpszSound - A string that specifies the sound to play. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.
    Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename.
    按照这段说明我们可以看到,PlaySound函数是可以播放内存中的WAV数据的,但是调用时的参数如何写?却是找不到什么资料。笔者通过很多试验,终于找到了正确的方法,下面介绍给大家。掌握了这种方法之后,你编写的VB应用程序在打包成EXE文件发布时,就可以不用再另外发布所需的WAV文件了。
    一、将WAV文件添加到工程的资源文件
    我们所要做的第一步就是要将WAV文件添加到VB工程的资源文件中去。
    1.打开“VB资源编辑器”;
    2.在VB资源编辑器中,选择“添加自定义资源”按钮;
    3.在“打开一个自定义资源”对话框中,选择你想要嵌入到工程的WAV文件。
    4.完成以上步骤之后,在VB资源编辑器的窗口中将出现”CUSTOM”这一用户自定义资源分支,其下有一个标识号为101的资源(注:标识号可以修改,此文中使用这个默认的标识号101)。
    二、播放资源文件中WAV数据
    假定工程中一个窗体Form1,窗体Form1中一个用于单击后播放WAV数据的命令按钮Command1,在窗体Form1中添加如下的代码:
    Option Explicit
    Private Const SND_ASYNC = &H1
    Private Const SND_MEMORY = &H4Private Declare Function PlaySoundMemory Lib "winmm.dll" Alias "PlaySoundA" _
            (ByVal pMemory As Long, ByVal hModule As Long, ByVal dwFlags As Long) As LongPrivate Sub Command1_Click()
        Dim bytWavData() As Byte
        bytWavData= LoadResData(101, "CUSTOM")
        PlaySoundMemory VarPtr(bytWavData(0)), ByVal 0&, SND_MEMORY Or SND_ASYNC
    End Sub
    说明:
    1.这里申明的API函数名称是PlaySoundMemory,其实就是前面提到的PlaySound,只是第一个参数有所不同,所以就用了一个不同的名称。
    2.在申明中,第一个参数写成了 ByVal pMemory As Long,在实际调用时,pMemory将获取内存中存放着WAV数据的数据块的首字节地址。
    3.从资源文件中获取WAV数据块的方法是:
        bytWavData= LoadResData(101, "CUSTOM")
    bytWavData是一个不定大小的字节数组,它的大小会被LoadResData函数自动调整。
    4.内存中存放着WAV数据的数据块的首字节地址(也就是字节数组bytWavData的地址)是通过 VarPtr函数来获取的:VarPtr(bytWavData(0))。三、编译生成EXE文件
    工程的资源文件将同时被打包在生成的EXE文件中,发布时只需发布EXE文件即可。