locked
Multiple sounds are not working RRS feed

  • Question

  • Hi,

    I am working on a Game in VB .NET and I want to play bg music and other game sounds at the same time but I am not able to do this. Please suggest me the possible solution.

    Thanks!

    Friday, January 30, 2015 10:06 AM

Answers

  • Hi,

    I am working on a Game in VB .NET and I want to play bg music and other game sounds at the same time but I am not able to do this. Please suggest me the possible solution.

    Thanks!

    Are the sounds from embedded resources or files on disk? If they're from embedded resources they will need to be written to disk in order to play multiple sounds at the same time as far as I know. Since My.Computer.Audio can only play one sound at a time and then only .Wav files. Other players like the Windows Media Player control, Media Player or VLC Media Player need to access files from disk to my knowledge (or the net maybe).

    You can also create the WMP control dynamically, not display it and play music with it. Multiple instances at the same time.

    Option Strict On
    
    'Add reference to Assemblies, Com, Windows Media Player
    
    Imports WMPLib
    Imports System.IO
    
    Public Class Form1
    
        Dim Player1 As New WMPLib.WindowsMediaPlayer()
        Dim Song1 As WMPLib.IWMPMedia
        Dim Player2 As New WMPLib.WindowsMediaPlayer()
        Dim Song2 As WMPLib.IWMPMedia
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
    
            Dim SW1 As New StreamWriter("C:\Users\John\Desktop\Tick.Wav")
            My.Resources.Tick.CopyTo(SW1.BaseStream)
            SW1.Close()
    
            Song1 = Player1.newMedia("C:\Users\John\Desktop\Tick.Wav")
            Player1.currentPlaylist.appendItem(Song1)
    
            Dim SW2 As New StreamWriter("C:\Users\John\Desktop\Rooster.Wav")
            My.Resources.Rooster.CopyTo(SW2.BaseStream)
    
            SW2.Write(My.Resources.Rooster)
            SW2.Close()
    
            Song2 = Player2.newMedia("C:\Users\John\Desktop\Rooster.Wav")
            Player2.currentPlaylist.appendItem(Song2)
    
            Player1.controls.playItem(Song1)
            Player2.controls.playItem(Song2)
        End Sub
    
    End Class
    


    La vida loca

    • Marked as answer by Dr Kad Tuesday, February 3, 2015 7:00 AM
    Friday, January 30, 2015 5:06 PM

All replies

  • You can borrow System.Windows.Media.MediaPlayer from WPF by adding references to PresentationCore and WindowBase to the project.  Then you can create multiple instance of MediaPlayer to play your various sounds.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Crazypennie Friday, January 30, 2015 3:53 PM
    Friday, January 30, 2015 3:41 PM
  • Hi,

    I am working on a Game in VB .NET and I want to play bg music and other game sounds at the same time but I am not able to do this. Please suggest me the possible solution.

    Thanks!

    Are the sounds from embedded resources or files on disk? If they're from embedded resources they will need to be written to disk in order to play multiple sounds at the same time as far as I know. Since My.Computer.Audio can only play one sound at a time and then only .Wav files. Other players like the Windows Media Player control, Media Player or VLC Media Player need to access files from disk to my knowledge (or the net maybe).

    You can also create the WMP control dynamically, not display it and play music with it. Multiple instances at the same time.

    Option Strict On
    
    'Add reference to Assemblies, Com, Windows Media Player
    
    Imports WMPLib
    Imports System.IO
    
    Public Class Form1
    
        Dim Player1 As New WMPLib.WindowsMediaPlayer()
        Dim Song1 As WMPLib.IWMPMedia
        Dim Player2 As New WMPLib.WindowsMediaPlayer()
        Dim Song2 As WMPLib.IWMPMedia
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
    
            Dim SW1 As New StreamWriter("C:\Users\John\Desktop\Tick.Wav")
            My.Resources.Tick.CopyTo(SW1.BaseStream)
            SW1.Close()
    
            Song1 = Player1.newMedia("C:\Users\John\Desktop\Tick.Wav")
            Player1.currentPlaylist.appendItem(Song1)
    
            Dim SW2 As New StreamWriter("C:\Users\John\Desktop\Rooster.Wav")
            My.Resources.Rooster.CopyTo(SW2.BaseStream)
    
            SW2.Write(My.Resources.Rooster)
            SW2.Close()
    
            Song2 = Player2.newMedia("C:\Users\John\Desktop\Rooster.Wav")
            Player2.currentPlaylist.appendItem(Song2)
    
            Player1.controls.playItem(Song1)
            Player2.controls.playItem(Song2)
        End Sub
    
    End Class
    


    La vida loca

    • Marked as answer by Dr Kad Tuesday, February 3, 2015 7:00 AM
    Friday, January 30, 2015 5:06 PM