Solved

Stop embedded media player in VB form

Posted on 2011-02-27
5
717 Views
Last Modified: 2012-05-11
I have the following application I am trying to build. The alarm clock opens an audio file and it is played in an embedded media player. I am trying to create a snooze button and the first step is to stop or pause the media player. I just don't seem to be having any luck. It is not complete code so it is not commented correctly or in a finished state. Of particular interest to this problem are lines 72 and 284 to 287. Thank You.
Public Class Form1

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        If TimeOfDay.Hour > 12 AndAlso TimeOfDay.Hour <> 24 Then
            lbltimeofdayhours.Text = (TimeOfDay.Hour - 12)
            lblampm1.Text = "pm"
        ElseIf TimeOfDay.Hour = 12 Then
            lbltimeofdayhours.Text = "12"
            lblampm1.Text = "pm"
        Else
            lbltimeofdayhours.Text = TimeOfDay.Hour
        End If

        If TimeOfDay.Minute = 0 Then
            lbltimeofdayminutes.Text = "00"
        ElseIf TimeOfDay.Minute = 1 Then
            lbltimeofdayminutes.Text = "01"
        ElseIf TimeOfDay.Minute = 2 Then
            lbltimeofdayminutes.Text = "02"
        ElseIf TimeOfDay.Minute = 3 Then
            lbltimeofdayminutes.Text = "03"
        ElseIf TimeOfDay.Minute = 4 Then
            lbltimeofdayminutes.Text = "04"
        ElseIf TimeOfDay.Minute = 5 Then
            lbltimeofdayminutes.Text = "05"
        ElseIf TimeOfDay.Minute = 6 Then
            lbltimeofdayminutes.Text = "06"
        ElseIf TimeOfDay.Minute = 7 Then
            lbltimeofdayminutes.Text = "07"
        ElseIf TimeOfDay.Minute = 8 Then
            lbltimeofdayminutes.Text = "08"
        ElseIf TimeOfDay.Minute = 9 Then
            lbltimeofdayminutes.Text = "09"
        Else
            lbltimeofdayminutes.Text = TimeOfDay.Minute
        End If


        If TimeOfDay.Second = 0 Then
            lbltimeofdayseconds.Text = "00"
        ElseIf TimeOfDay.Second = 1 Then
            lbltimeofdayseconds.Text = "01"
        ElseIf TimeOfDay.Second = 2 Then
            lbltimeofdayseconds.Text = "02"
        ElseIf TimeOfDay.Second = 3 Then
            lbltimeofdayseconds.Text = "03"
        ElseIf TimeOfDay.Second = 4 Then
            lbltimeofdayseconds.Text = "04"
        ElseIf TimeOfDay.Second = 5 Then
            lbltimeofdayseconds.Text = "05"
        ElseIf TimeOfDay.Second = 6 Then
            lbltimeofdayseconds.Text = "06"
        ElseIf TimeOfDay.Second = 7 Then
            lbltimeofdayseconds.Text = "07"
        ElseIf TimeOfDay.Second = 8 Then
            lbltimeofdayseconds.Text = "08"
        ElseIf TimeOfDay.Second = 9 Then
            lbltimeofdayseconds.Text = "09"
        Else
            lbltimeofdayseconds.Text = TimeOfDay.Second
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


        If TimeOfDay.Hour = lblalarmsethours.Text AndAlso
            TimeOfDay.Minute = lblalarmsetminutes.Text AndAlso
            TimeOfDay.Second = lblalarmsetseconds.Text AndAlso
            lblampm1.Text = lblampm2.Text Then
            AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName.ToString()
            AxWindowsMediaPlayer1.settings.volume = 100
        End If
    End Sub




    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'open the file dialog box to open a file

        OpenFileDialog1.Title = "Please Select a File"
        OpenFileDialog1.InitialDirectory = "C:temp"
        OpenFileDialog1.ShowDialog()
        TextBox6.Text = OpenFileDialog1.FileName.ToString()
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        'swap pic1 and pic2 to create impression of being pressed
        PictureBox1.Visible = False
        PictureBox2.Visible = True

        'show snooze button and label
        picSnooze1.Visible = True
        lblSnooze.Visible = True

        'format user input to system time
        If txtsethours.Text = "1" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "13"
        ElseIf txtsethours.Text = "2" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "14"
        ElseIf txtsethours.Text = "3" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "15"
        ElseIf txtsethours.Text = "4" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "16"
        ElseIf txtsethours.Text = "5" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "17"
        ElseIf txtsethours.Text = "6" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "18"
        ElseIf txtsethours.Text = "7" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "19"
        ElseIf txtsethours.Text = "8" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "20"
        ElseIf txtsethours.Text = "9" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "21"
        ElseIf txtsethours.Text = "10" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "22"
        ElseIf txtsethours.Text = "11" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "23"
        ElseIf txtsethours.Text = "12" Or "00" AndAlso txtampm.Text = "am" Then
            lblalarmsethours.Text = "0"
        ElseIf txtsethours.Text = "12" AndAlso txtampm.Text = "pm" Then
            lblalarmsethours.Text = "12"
        Else
            lblalarmsethours.Text = txtsethours.Text
        End If

        'send user input to the actual alarm counter
        lblalarmsetminutes.Text = txtsetminutes.Text
        lblalarmsetseconds.Text = txtsetseconds.Text
        lblampm2.Text = txtampm.Text
        lblshowsethours.Text = txtsethours.Text

        'start the timer
        Timer1.Start()
    End Sub


    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        'swap pic1 and pic2 to create impression of being pressed
        PictureBox2.Visible = False
        PictureBox1.Visible = True

        'turn on alarm operation indication
        picOnPulse.Visible = True

        'show the media player
        AxWindowsMediaPlayer1.Visible = True
    End Sub

    Private Sub PictureBox3_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.MouseDown
        PictureBox3.Visible = False
        PictureBox4.Visible = True

        'hide snooze button and label
        picSnooze1.Visible = False
        lblSnooze.Visible = False

        'hide the media play
        AxWindowsMediaPlayer1.Visible = False

        lblalarmsetminutes.Text = "00"
        lblalarmsetseconds.Text = "00"
        lblampm2.Text = "am"
        lblshowsethours.Text = "00"
    End Sub

    Private Sub PictureBox3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseUp
        PictureBox4.Visible = False
        PictureBox3.Visible = True
        picOnPulse.Visible = False
    End Sub


    Private Sub picClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles picClose.Click
        Me.Close()
    End Sub

    Private Sub picClose_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles picClose.MouseHover
        lblClose.Visible = True
    End Sub

    Private Sub picClose_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles picClose.MouseLeave
        lblClose.Visible = False
    End Sub

    Private Sub TransparentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TransparentToolStripMenuItem.Click
        'sets the background to aliceblue
        'then sets aliceblue as transparent
        Me.BackColor = Color.AliceBlue
        Me.TransparencyKey = System.Drawing.Color.AliceBlue

    End Sub


    Private Sub txtsethours_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtsethours.KeyPress, txtsetminutes.KeyPress, txtsetseconds.KeyPress
        ' allows only numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub


    Private Sub tsmFormBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmFormBackColor.Click
        ColorDialog1.ShowDialog()
        Me.BackColor = ColorDialog1.Color
        txtsethours.BackColor = ColorDialog1.Color
        txtsetminutes.BackColor = ColorDialog1.Color
        txtsetseconds.BackColor = ColorDialog1.Color
        txtampm.BackColor = ColorDialog1.Color
        TextBox4.BackColor = ColorDialog1.Color
        TextBox5.BackColor = ColorDialog1.Color
    End Sub

    Private Sub picSnooze1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picSnooze1.MouseDown
        'swap picSnooze files to emulate button being pressed
        picSnooze1.Visible = False
        picSnooze2.Visible = True
    End Sub

    Private Sub picSnooze1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picSnooze1.MouseUp
        'swap picSnooze files back to complete button pressing animation
        picSnooze2.Visible = False
        picSnooze1.Visible = True
    End Sub

    Private Sub tsmTextBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmTextBackColor.Click
        'sets the foreground font color of clock and set alarm
        'to the value selected by the user from the menu

        ColorDialog2.ShowDialog()
        lbltimeofdayhours.ForeColor = ColorDialog2.Color
        lbltimeofdayminutes.ForeColor = ColorDialog2.Color
        lbltimeofdayseconds.ForeColor = ColorDialog2.Color
        lblampm1.ForeColor = ColorDialog2.Color
        Label5.ForeColor = ColorDialog2.Color
        Label10.ForeColor = ColorDialog2.Color
        Label6.ForeColor = ColorDialog2.Color
        Label9.ForeColor = ColorDialog2.Color
        lblshowsethours.ForeColor = ColorDialog2.Color
        lblalarmsetminutes.ForeColor = ColorDialog2.Color
        lblalarmsetseconds.ForeColor = ColorDialog2.Color
        lblampm2.ForeColor = ColorDialog2.Color
        txtsethours.ForeColor = ColorDialog2.Color
        txtsetminutes.ForeColor = ColorDialog2.Color
        txtsetseconds.ForeColor = ColorDialog2.Color
        TextBox4.ForeColor = ColorDialog2.Color
        TextBox5.ForeColor = ColorDialog2.Color
        txtampm.ForeColor = ColorDialog2.Color
        lblClose.ForeColor = ColorDialog2.Color
        lblCancel.ForeColor = ColorDialog2.Color
        lblSet.ForeColor = ColorDialog2.Color
        lblSnooze.ForeColor = ColorDialog2.Color
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        'show information about the application
        MessageBox.Show("Coded by Robert Kraft." & vbCrLf & _
                        "This is my first application outside of what I have coded for school." & vbCrLf & _
                        "I hope you enjoy my work!",
                        "About Alarming")

    End Sub

    Private Sub CreditsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditsToolStripMenuItem.Click
        'give credit to those that inspired ideas for this application
        'or images used
        MessageBox.Show("Original idea inspired by 10Iceman01 at http://www.youtube.com/watch?v=C1jVh4SIB7c&NR=1" & vbCrLf & _
                        "Green button image from fbouly at http://fbouly.deviantart.com/art/Round-glossy-green-button-114514061" & vbCrLf & _
                        "Blue button from http://registry.gimp.org/taxonomy/term/312" & vbCrLf & _
                        "Broken clock by FloydT at http://www.dirtyharrysplace.com/dailyheadlines/",
                        "Credit")
    End Sub


    Private Sub AdjustSnoozeTimeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdjustSnoozeTimeToolStripMenuItem.Click
        

    End Sub

    Private Sub picSnooze1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picSnooze1.Click
        If WMPLib.WMPPlayState.wmppsPlaying Then
            Me.AxWindowsMediaPlayer1.Ctlcontrols.stop()
        End If

End Sub
End Class

Open in new window

0
Comment
Question by:psychokraft
  • 4
5 Comments
 
LVL 10

Accepted Solution

by:
kdwood earned 500 total points
ID: 34994079
psychokraft,

Your code to stop the media player looks fine and works when I test it.    I'm assuming from your description that you are able to get the audio file to play, but the snooze button is not stopping the audio play.  Is that correct?   If so, do you get any errors?

Can you verify that you are making it inside the if statement below:

Private Sub picSnooze1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picSnooze1.Click
     
        If WMPLib.WMPPlayState.wmppsPlaying Then

                                                              <--------------   are you making it to this point?

            Me.AxWindowsMediaPlayer1.Ctlcontrols.stop()

        End If

End Sub



Regards,

Keith
0
 
LVL 2

Author Comment

by:psychokraft
ID: 34994274
Yep, everything works, but the snooze button has no effect when I click it. I will attach the solution.
0
 
LVL 2

Author Comment

by:psychokraft
ID: 34994276
Cannot add  solution as ZIP files are not allowed.
0
 
LVL 2

Author Comment

by:psychokraft
ID: 34994279
OK, ZIPs are allowed but it kicked mine off complaining about an mp3 file. ???? WindowsApplication1.exe
0
 
LVL 2

Author Comment

by:psychokraft
ID: 34994536
Figured it out....I had a mouse down event in with the application code and since that takes priority over the mouse click once I made the picSnooze1 invisible it lost the ability to act upon the click event.  Works great now, thanks for the help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now