8 Replies - 397 Views - Last Post: 21 July 2014 - 08:50 AM Rate Topic: -----

#1 xD@t0m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 61
  • Joined: 13-May 14

Playing MP3's is acting weirdly(?)

Posted 15 July 2014 - 12:08 PM

Hi,

I've been trying to make a simple Launchpad simulator which I call VB.Launchpad in VB.Net. I got the (simple) lighting effects and detecting keypress, but now I've been trying to implement playing sounds in mp3 format. So I am using WMPLib:

Imports WMPLib


Here is the rest of the code:

Public Class Form1

#Region "Declarations of players"
    Dim Players(8) As WindowsMediaPlayer  'One player for each NumPad key (9 keys, 0-8)
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For k As Integer = 0 To Players.Length() - 1
            Players(k) = New WindowsMediaPlayer
        Next k
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad7  'Testing on this key, whose player is Players(0)
                launchpadButton00.Image = My.Resources.launchpadButtonGreen      'launchpadButton Objects are PictureBoxes
                Players(0).URL = launchpadButton00.Tag
                Players(0).controls.play()
            Case Keys.NumPad8
                launchpadButton01.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad9
                launchpadButton02.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad4
                launchpadButton10.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad5
                launchpadButton11.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad6
                launchpadButton12.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad1
                launchpadButton20.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad2
                launchpadButton21.Image = My.Resources.launchpadButtonGreen
            Case Keys.NumPad3
                launchpadButton22.Image = My.Resources.launchpadButtonGreen
        End Select
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyCode
            Case Keys.NumPad7
                launchpadButton00.Image = My.Resources.launchpadButtonWhite
                Players(0).controls.stop()
            Case Keys.NumPad8
                launchpadButton01.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad9
                launchpadButton02.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad4
                launchpadButton10.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad5
                launchpadButton11.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad6
                launchpadButton12.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad1
                launchpadButton20.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad2
                launchpadButton21.Image = My.Resources.launchpadButtonWhite
            Case Keys.NumPad3
                launchpadButton22.Image = My.Resources.launchpadButtonWhite
        End Select
    End Sub

    Private Sub launchpadButton00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles launchpadButton00.Click
        launchpadButton00.Tag = openSound(sender)
    End Sub

    Private Function openSound(ByVal button As Object)
        Dim returnSound As String = button.Tag
        If openSoundDialog.ShowDialog() = DialogResult.OK Then
            returnSound = openSoundDialog.FileName
        End If
        Return returnSound
    End Function
End Class


My problem is that the code doesn't throw an error, it compiles, but it is not doing what I want it to do.

I am trying to make a sound (preselected, and its filename stored in launchpadButton00.Tag) play as I press a key (in my test case, NumPad7). The PictureBox's Image changes, but the sound isn't played. Instead the sound is played only when NumPad7 is being held, and another key on the keyboard is pressed.

My attempt to solve it, was to just send a keypress as NumPad7 is being held:

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad7
                launchpadButton00.Image = My.Resources.launchpadButtonGreen
                Players(0).URL = launchpadButton00.Tag
                Players(0).controls.play()
                SendKeys.Send("k")
            '(...)
        End Select
    End Sub



Unfortunately that did nothing. So I am stuck looking for a solution. Can someone help?

Is This A Good Question/Topic? 0
  • +

Replies To: Playing MP3's is acting weirdly(?)

#2 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 282
  • View blog
  • Posts: 892
  • Joined: 01-February 13

Re: Playing MP3's is acting weirdly(?)

Posted 15 July 2014 - 03:20 PM

Hi,
The first thing i would recommend is to turn on Option Strict in the VB menu (Tools/Options) or you can type Option Strict On at the very top of your code. That will help you correct things like the line below.
Players(0).URL = launchpadButton00.Tag

The Url requires a String i believe and you are assigning it the Tag of the button which is an Object. The Tag should be converted to a string when assign it to the Url like this
Players(0).URL = launchpadButton00.Tag.ToString

There are other spots i see that will need correcting also.

Now, using the MediaPlayer is going to take a second to actually load the sound file and play it when the Form.KeyDown event is raised. However, i see you are stopping the MediaPlayer when the Form.KeyUp event is raised which means you are stopping the Player before it has time to load and play the sound file. You will not want to stop the player when the KeyUp event is raised. Let the player go after starting it. It should stop when it finishes playing the sound.
Was This Post Helpful? 1
  • +
  • -

#3 xD@t0m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 61
  • Joined: 13-May 14

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:20 AM

Never mind, I fixed it. Upon holding key, one KeyDown event is raised. But as the key is being held, multiple KeyDown events are raised, thus making the player "replay" every ~0.1 seconds. That was fixed like shown here:
Imports WMPLib

Public Class MainForm
    Dim Players(8) As WindowsMediaPlayer
    Public KeyHeld(8) As Boolean
    Public launchpadButtonColors(8) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For k As Integer = 0 To Players.Length() - 1
            Players(k) = New WindowsMediaPlayer
        Next k
        For k As Integer = 0 To launchpadButtonColors.Length() - 1
            launchpadButtonColors(k) = "Green"
        Next k
        '(...)
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad7
                If Not KeyHeld(0) Then
                    launchpadButton00.Image = Functions.ReturnImageResource(launchpadButtonColors(0))
                    Players(0).controls.play()
                    KeyHeld(0) = True
                End If
            Case Keys.NumPad8
                If Not KeyHeld(1) Then
                    launchpadButton01.Image = Functions.ReturnImageResource(launchpadButtonColors(1))
                    Players(1).controls.play()
                    KeyHeld(1) = True
                End If
            Case Keys.NumPad9
                If Not KeyHeld(2) Then
                    launchpadButton02.Image = Functions.ReturnImageResource(launchpadButtonColors(2))
                    Players(2).controls.play()
                    KeyHeld(2) = True
                End If
            Case Keys.NumPad4
                If Not KeyHeld(3) Then
                    launchpadButton10.Image = Functions.ReturnImageResource(launchpadButtonColors(3))
                    Players(3).controls.play()
                    KeyHeld(3) = True
                End If
            Case Keys.NumPad5
                If Not KeyHeld(4) Then
                    launchpadButton11.Image = Functions.ReturnImageResource(launchpadButtonColors(4))
                    Players(4).controls.play()
                    KeyHeld(4) = True
                End If
            Case Keys.NumPad6
                If Not KeyHeld(5) Then
                    launchpadButton12.Image = Functions.ReturnImageResource(launchpadButtonColors(5))
                    Players(5).controls.play()
                    KeyHeld(5) = True
                End If
            Case Keys.NumPad1
                If Not KeyHeld(6) Then
                    launchpadButton20.Image = Functions.ReturnImageResource(launchpadButtonColors(6))
                    Players(6).controls.play()
                    KeyHeld(6) = True
                End If
            Case Keys.NumPad2
                If Not KeyHeld(7) Then
                    launchpadButton21.Image = Functions.ReturnImageResource(launchpadButtonColors(7))
                    Players(7).controls.play()
                    KeyHeld(7) = True
                End If
            Case Keys.NumPad3
                If Not KeyHeld(8) Then
                    launchpadButton22.Image = Functions.ReturnImageResource(launchpadButtonColors(8))
                    Players(8).controls.play()
                    KeyHeld(8) = True
                End If

            Case Keys.V
                Functions.ChangeColorOfHeldLaunchpadButtons("Green")
            Case Keys.B
                Functions.ChangeColorOfHeldLaunchpadButtons("Yellow")
            Case Keys.N
                Functions.ChangeColorOfHeldLaunchpadButtons("Red")
            Case Keys.M
                Functions.ChangeColorOfHeldLaunchpadButtons("Next")
        End Select
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyCode
            Case Keys.NumPad7
                launchpadButton00.Image = My.Resources.launchpadButtonWhite
                Players(0).controls.stop()
                KeyHeld(0) = False
            Case Keys.NumPad8
                launchpadButton01.Image = My.Resources.launchpadButtonWhite
                Players(1).controls.stop()
                KeyHeld(1) = False
            Case Keys.NumPad9
                launchpadButton02.Image = My.Resources.launchpadButtonWhite
                Players(2).controls.stop()
                KeyHeld(2) = False
            Case Keys.NumPad4
                launchpadButton10.Image = My.Resources.launchpadButtonWhite
                Players(3).controls.stop()
                KeyHeld(3) = False
            Case Keys.NumPad5
                launchpadButton11.Image = My.Resources.launchpadButtonWhite
                Players(4).controls.stop()
                KeyHeld(4) = False
            Case Keys.NumPad6
                launchpadButton12.Image = My.Resources.launchpadButtonWhite
                Players(5).controls.stop()
                KeyHeld(5) = False
            Case Keys.NumPad1
                launchpadButton20.Image = My.Resources.launchpadButtonWhite
                Players(6).controls.stop()
                KeyHeld(6) = False
            Case Keys.NumPad2
                launchpadButton21.Image = My.Resources.launchpadButtonWhite
                Players(7).controls.stop()
                KeyHeld(7) = False
            Case Keys.NumPad3
                launchpadButton22.Image = My.Resources.launchpadButtonWhite
                Players(8).controls.stop()
                KeyHeld(8) = False
        End Select
    End Sub
    '(...)
End Class

To clear things up, an array of WindowsMediaPlayers has been declared, one for each button. This is to allow simultaneous play. The KeyHeld Boolean Array prevents replaying every ~0.1 seconds by ignoring keystrokes repeated while the key is held.

I've gotten completely rid of the Player.URL = launchpadButton.Tag, the URL is being set where the audio file is selected:
    Private Sub launchpadButton00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles launchpadButton00.Click
        Players(0).URL = openSound(Players(0).URL)
        Players(0).controls.stop()   'Stopped because when URL is set, the player loads and plays
    End Sub

    Private Function openSound(ByVal currentSound As String)
        Dim returnSound As String = currentSound
        If openSoundDialog.ShowDialog() = DialogResult.OK Then
            returnSound = openSoundDialog.FileName
        End If
        openSoundDialog.FileName = ""
        Return returnSound
    End Function

Setting URL when opening file allows it to load so it can instantly play. This provides a smoother experience.

I am sorry for my late response, I've fixed it few days ago but forgot to report. I just remembered I didn't report when I checked my mail.

EDIT: Forgot to mention -- the sound should play only while the key is held. When the key is let go, the sound stops.

This post has been edited by xD@t0m: 21 July 2014 - 08:27 AM

Was This Post Helpful? 1
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 282
  • View blog
  • Posts: 892
  • Joined: 01-February 13

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:30 AM

Hi xD@t0m,
Glad to here you got it working and that you shared your fix. Others may find it helpful. B)
Was This Post Helpful? 0
  • +
  • -

#5 xD@t0m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 61
  • Joined: 13-May 14

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:34 AM

View PostIronRazer, on 21 July 2014 - 08:30 AM, said:

Glad to hear you got it working and that you shared your fix. Others may find it helpful. B)/>


Anytime. If I would have any other problems in this project, should I start a new topic or go with this one?
Was This Post Helpful? 0
  • +
  • -

#6 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 70
  • View blog
  • Posts: 565
  • Joined: 05-December 13

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:37 AM

Probably best to start a new topic to save confusion. But if its related in some-other way to the above issues, then you could bump this one. :)
Was This Post Helpful? 1
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 282
  • View blog
  • Posts: 892
  • Joined: 01-February 13

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:39 AM

You should start a new question unless it has something to do with this same problem you had with the keys. You can always insert a link back to this thread in your new question if you think it adds some value to your new question. :)
Was This Post Helpful? 1
  • +
  • -

#8 xD@t0m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 61
  • Joined: 13-May 14

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:45 AM

Okay thanks.
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 282
  • View blog
  • Posts: 892
  • Joined: 01-February 13

Re: Playing MP3's is acting weirdly(?)

Posted 21 July 2014 - 08:50 AM

Your welcome. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1