none
如何用VB.NET和两个声卡实现播放不同的音频文件 RRS feed

  • 问题

  • 请教各位老师:

          如何用电脑的两个声卡,用VB.NET编程实现不同的声卡播放不同的音频文件,例如,声卡1播放歌曲,声卡2播放相声。请不惜赐教,非常感谢!

    2014年4月3日 7:03

答案

  • 请教各位老师:

          如何用电脑的两个声卡,用VB.NET编程实现不同的声卡播放不同的音频文件,例如,声卡1播放歌曲,声卡2播放相声。请不惜赐教,非常感谢!

    Hi,

    首先VB.NET中如何播放音频文件, 你可以参考FAQs: 在VB.NET中如何播放音频/视频文件?

    该链接提供了一个SDK:DirectX SDK, 因此要实现这个功能需要先安装这个SDK。

    具体下载和需要调用的方法你可以参考这个相关的帖子:

    c# 中如何实现多个声卡同时播放不同的音乐(分区广播形式)我可以可以发现主要使用DevicesCollection Class 来获取声卡集合,具体控制播放需要使用BufferDescription Class

    这里有一个VB.Net使用多声卡播放音频文件的例子可供参考:Choose between different sound cards for playback in VB.net (英文版的,只需要少量的翻译就可以理解)

    只需要参考以下引用出来的例子。

    1.下载SDK

    2.添加引用

    Ok here it is. You will have to add a reference to DirectX to your
    application, then at these Imports statements:

    Code:
    Imports Microsoft.DirectX
    Imports Microsoft.DirectX.DirectSound

    Then add the following
    code:

    3.获取声卡集合

    Code:
     Private myDevices As DevicesCollection = Nothing
        Private Structure myDeviceDescription
            Public info As DeviceInformation
            Public Overrides Function ToString() As String
                Return info.Description
            End Function
            Public Sub New(ByVal di As DeviceInformation)
                info = di
            End Sub
        End Structure
    
    Dim DsDevice as Directsound.Device
    Dim TheSoundBuffer as MemoryStream

      4. 遍历声卡集合并添加各声卡至ListBox控件

    To enumerate your devices, use
    this code. This example populates a listbox with all your system playback
    devices:

    Code:
    Public Sub PopulateDevices()
    
            ' Retrieve the available DirectSound devices
            myDevices = New DevicesCollection()
    
            'Clear the list box
            ListBoxAudioDevices.ClearSelected()
    
            'Step through each device and add it's description to the listbox
            For Each dev As DeviceInformation In myDevices
                ListBoxAudioDevices.Items.Add(dev.Description)
            Next
    
    End Sub

    5.选择某个声卡 

    Now this code will select the device, in this example I have
    selected the 3rd device in my listbox (index 2)

    Code:
    DsDevice = New Device(myDevices(2).DriverGuid)
    DsDevice.SetCooperativeLevel(Me, CooperativeLevel.Priority)
    
    TheSoundBuffer = New MemoryStream

    6.以下代码实现控制音频文件的各种属性,以及播放音频文件 

    Also, I added the following code
    because I wanted to enable the ability to control volume, pan, frequency, and
    also to have the sound play even if the program doesn't have focus (since by
    default the sound will stop when your app loses focus)

    Code:
        Dim bufferDesc As New Microsoft.DirectX.DirectSound.BufferDescription
            With bufferDesc
                .GlobalFocus = True
                .ControlVolume = True
                .ControlPan = True
                .ControlFrequency = True
            End With
    
    'Create the secondary sound buffer
    TheSoundBuffer = New SecondaryBuffer("c:\test.wav", bufferDesc, DsDevice)
    
    TheSoundBuffer.Play(0, BufferPlayFlags.Default)

    Now also in the
    TheSoundBuffer.Play function, you can also use BufferPlayFlags.Looping to have
    the audio loop over and over again until you call TheSoundBuffer.Stop

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月4日 2:52
    版主

全部回复

  • 请教各位老师:

          如何用电脑的两个声卡,用VB.NET编程实现不同的声卡播放不同的音频文件,例如,声卡1播放歌曲,声卡2播放相声。请不惜赐教,非常感谢!

    Hi,

    首先VB.NET中如何播放音频文件, 你可以参考FAQs: 在VB.NET中如何播放音频/视频文件?

    该链接提供了一个SDK:DirectX SDK, 因此要实现这个功能需要先安装这个SDK。

    具体下载和需要调用的方法你可以参考这个相关的帖子:

    c# 中如何实现多个声卡同时播放不同的音乐(分区广播形式)我可以可以发现主要使用DevicesCollection Class 来获取声卡集合,具体控制播放需要使用BufferDescription Class

    这里有一个VB.Net使用多声卡播放音频文件的例子可供参考:Choose between different sound cards for playback in VB.net (英文版的,只需要少量的翻译就可以理解)

    只需要参考以下引用出来的例子。

    1.下载SDK

    2.添加引用

    Ok here it is. You will have to add a reference to DirectX to your
    application, then at these Imports statements:

    Code:
    Imports Microsoft.DirectX
    Imports Microsoft.DirectX.DirectSound

    Then add the following
    code:

    3.获取声卡集合

    Code:
     Private myDevices As DevicesCollection = Nothing
        Private Structure myDeviceDescription
            Public info As DeviceInformation
            Public Overrides Function ToString() As String
                Return info.Description
            End Function
            Public Sub New(ByVal di As DeviceInformation)
                info = di
            End Sub
        End Structure
    
    Dim DsDevice as Directsound.Device
    Dim TheSoundBuffer as MemoryStream

      4. 遍历声卡集合并添加各声卡至ListBox控件

    To enumerate your devices, use
    this code. This example populates a listbox with all your system playback
    devices:

    Code:
    Public Sub PopulateDevices()
    
            ' Retrieve the available DirectSound devices
            myDevices = New DevicesCollection()
    
            'Clear the list box
            ListBoxAudioDevices.ClearSelected()
    
            'Step through each device and add it's description to the listbox
            For Each dev As DeviceInformation In myDevices
                ListBoxAudioDevices.Items.Add(dev.Description)
            Next
    
    End Sub

    5.选择某个声卡 

    Now this code will select the device, in this example I have
    selected the 3rd device in my listbox (index 2)

    Code:
    DsDevice = New Device(myDevices(2).DriverGuid)
    DsDevice.SetCooperativeLevel(Me, CooperativeLevel.Priority)
    
    TheSoundBuffer = New MemoryStream

    6.以下代码实现控制音频文件的各种属性,以及播放音频文件 

    Also, I added the following code
    because I wanted to enable the ability to control volume, pan, frequency, and
    also to have the sound play even if the program doesn't have focus (since by
    default the sound will stop when your app loses focus)

    Code:
        Dim bufferDesc As New Microsoft.DirectX.DirectSound.BufferDescription
            With bufferDesc
                .GlobalFocus = True
                .ControlVolume = True
                .ControlPan = True
                .ControlFrequency = True
            End With
    
    'Create the secondary sound buffer
    TheSoundBuffer = New SecondaryBuffer("c:\test.wav", bufferDesc, DsDevice)
    
    TheSoundBuffer.Play(0, BufferPlayFlags.Default)

    Now also in the
    TheSoundBuffer.Play function, you can also use BufferPlayFlags.Looping to have
    the audio loop over and over again until you call TheSoundBuffer.Stop

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年4月4日 2:52
    版主
  • Thanks.
    2014年4月4日 9:40