sync 2 windows mediaplayer objects

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
Hi there,

I'm trying to build a mediaplayer, which should be able to play video's on a beamer (presentation) with vb 2010.

For that I made 2 forms, first form is the main form with all the regular function (play, pause, stop, volume, open, etc) and the second form is the "presentation" screen and has a windows media player object on it (with modus set to none).

On the first form I also have a windows mediaplayer object (small preview screen).

The media is being loaded from an openfiledialog into a listbox and the url of both the mediaplayers are set to the listbox1.selecteditem.
This works and both of the players play the media selected, but there are not sync.

How to solve this?

Here is the code I use:
VB.NET:
Public Class MainForm

    Dim tinseconden As Integer

    Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click
        SecondForm.AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
        AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
        SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.play()
        AxWindowsMediaPlayer1.Ctlcontrols.play()
        AxWindowsMediaPlayer1.settings.volume = AxWindowsMediaPlayer1.settings.mute
        CheckBox1.Enabled = True
        Me.Select()
        Timer1.Start()
        ProgressBarX1.Value = SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
    End Sub

    Private Sub Pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pause.Click
        SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.pause()
        AxWindowsMediaPlayer1.Ctlcontrols.pause()
    End Sub

    Private Sub Stopbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stopbutton.Click
        Try
            SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.stop()
            AxWindowsMediaPlayer1.Ctlcontrols.stop()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Slider1_DecreaseButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slider1.DecreaseButtonClick
        SecondForm.AxWindowsMediaPlayer1.settings.volume = SecondForm.AxWindowsMediaPlayer1.settings.volume - 10
    End Sub

    Private Sub Slider1_IncreaseButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slider1.IncreaseButtonClick
        SecondForm.AxWindowsMediaPlayer1.settings.volume = SecondForm.AxWindowsMediaPlayer1.settings.volume + 10
    End Sub

    Private Sub Slider1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Slider1.MouseMove
        SecondForm.AxWindowsMediaPlayer1.settings.volume = Slider1.Value
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBarX1.Value = SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
        tinseconden = SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
    End Sub

    Private Sub ButtonItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem1.Click
        OpenFileDialog1.ShowDialog()
        For Each filename As String In OpenFileDialog1.FileNames
            ListBox1.Items.Add(filename)
            If ListBox1.Items.Count > 0 And ButtonItem2.Checked = True Then
                Play.Enabled = True
                Pause.Enabled = True
                Stopbutton.Enabled = True
            End If
        Next
    End Sub

    Private Sub ButtonItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem2.Click
        If ButtonItem2.Checked = True Then
            ButtonItem2.Checked = False
        Else
            ButtonItem2.Checked = True
        End If
    End Sub

    Private Sub ButtonItem2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem2.CheckedChanged
        If ButtonItem2.Checked = True Then
            Dim screen As Screen
            'Show second form on second screen
            screen = screen.AllScreens(1)
            SecondForm.StartPosition = FormStartPosition.Manual
            SecondForm.Location = screen.Bounds.Location + New Point(100, 100)
            SecondForm.Show()
            Me.Select()
            If ListBox1.Items.Count > 0 And ButtonItem2.Checked = True Then
                Play.Enabled = True
                Pause.Enabled = True
                Stopbutton.Enabled = True
            End If
        Else
            SecondForm.Close()
            ButtonItem2.Checked = False
            SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.stop()
            AxWindowsMediaPlayer1.Ctlcontrols.stop()
            Play.Enabled = False
            Pause.Enabled = False
            Stopbutton.Enabled = False
            If CheckBox1.Checked = True Then
                CheckBox1.Checked = False
                CheckBox1.Enabled = False
            End If
        End If
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If ListBox1.Items.Count = 0 And ButtonItem2.Checked = False Then
            Play.Enabled = False
            Pause.Enabled = False
            Stopbutton.Enabled = False
        End If
        CheckBox1.Enabled = False
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            SecondForm.AxWindowsMediaPlayer1.fullScreen = True
            Fullscreen.Checked = True
        Else
            SecondForm.AxWindowsMediaPlayer1.fullScreen = False
            Fullscreen.Checked = False
        End If
    End Sub

    Private Sub Playmenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Playmenu.Click
        Try
            SecondForm.AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
            AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
            SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.play()
            AxWindowsMediaPlayer1.Ctlcontrols.play()
            AxWindowsMediaPlayer1.settings.volume = AxWindowsMediaPlayer1.settings.mute
            CheckBox1.Enabled = True
            Me.Select()
            Timer1.Start()
            ProgressBarX1.Value = SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Pausemenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pausemenu.Click
        Try
            SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.pause()
            AxWindowsMediaPlayer1.Ctlcontrols.pause()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Stopmenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stopmenu.Click
        Try
            SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.stop()
            AxWindowsMediaPlayer1.Ctlcontrols.stop()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem6.Click
        AboutBox1.ShowDialog()
    End Sub

    Private Sub Fullscreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fullscreen.CheckedChanged
        If Fullscreen.Checked Then
            SecondForm.AxWindowsMediaPlayer1.fullScreen = True
            CheckBox1.Checked = True
        Else
            SecondForm.AxWindowsMediaPlayer1.fullScreen = False
            CheckBox1.Checked = False
        End If
    End Sub

    Private Sub ProgressBarX1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ProgressBarX1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            ProgressBarX1.Value = e.X
            SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = ProgressBarX1.Value
        End If
    End Sub
End Class
 
what if you right after setting the url stop each player and set currentPosition to 0, then when both players are 'ready' you call play for both?
 
I think that is already what I did.

I first load the video('s) in the listbox (through openfiledialog), then select the video I want to play and press the play button, with that the urls are being set at the same time (I think), and also start at the same time.

Correct me if I'm wrong.

If I am can you give me a sample?
 
Actually I can't see that you did that. Pseudo example of my idea:
VB.NET:
p1.url = path
p1.stop
p1.currentPosition=0

p2.url = path
p2.stop
p2.currentPosition=0

p1.play
p2.play
 
Actually I can't see that you did that. Pseudo example of my idea:
VB.NET:
p1.url = path
p1.stop
p1.currentPosition=0

p2.url = path
p2.stop
p2.currentPosition=0

p1.play
p2.play

Thanks, what I have now is the following:

video's loaded in listbox, when I select the video I want to play, the following code is called through "selectedindexchanged"
VB.NET:
SecondForm.AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.stop()
SecondForm.AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = 0
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
AxWindowsMediaPlayer1.Ctlcontrols.stop()
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = 0

And pressed the play button to start the 2 mediaplayers at the same time.

But.....

Sometime everything works fine and no prob with the sync.
And then at a strange time they are out of sync (sometimes a sec and sometimes half a sec).

Any idea?
 
Application Threading Im guessing.. I sometimes experience lag in Timers when i have a media player present.

EDIT:
What are you trying to do with both media players synced?
 
What are you trying to do with both media players synced?

What I want, is that I have my main screen and a second screen (projection), on the main screen I want to have a small screen of what is projected (running along with the second screen), in that way I can follow what is on the projection screen.

Hope you understand what I want?
 
Back
Top