Results 1 to 13 of 13

Thread: Program bypasses some code

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    13

    Program bypasses some code

    Hi,

    In a VB6 subroutine I have a few lines of code which opens Windows Media Player followed by some lines of code which starts a speech. What I want to do, is to open Media Player, listen to it then listen to the speech but VB6 does not launch Media Player, it executes immediately speech process. Probably, VB6 does not find enough time to deal with Media Player ! Does anybody have a solution ?

    Thanks
    Éngin

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Program bypasses some code

    Let's see your code where all this happens (doesn't happen).

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    13

    Re: Program bypasses some code

    Hello SamOscarBrown,

    My program has some more details but in its simplest form it happens that media player is not launched.

    Sub Oku()
    Private Sub Form_Load()

    WindowsMediaPlayer1.URL = ("C:\Song1.mp3")

    Set Konusma = New SpVoice
    Konusma.Speak "Hello World"
    Set Konusma = Nothing

    End Sub

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Program bypasses some code

    If you comment out those last three lines (in the example, of course, and whatever number of lines before using Konusma), does the player start?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    13

    Re: Program bypasses some code

    Yes it does

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Program bypasses some code

    Do you want to wait till the song has finished playing before the next statement is executed?
    Also you specify the URL but I don't see any code to start playing

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Program bypasses some code

    It starts playing automatically.
    You need to add a PlayStateChange event (or something like that, I'm not looking it up because I don't have my VB6 mediaplayer code on the machine I'm currently on) so that when you get the event and the state is stopped, then you can do your speech. You may need a boolean so you only do the speech on the first transition to stop and not every time it changes to stop.
    Last edited by passel; Sep 21st, 2017 at 06:57 AM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    13

    Re: Program bypasses some code

    Yes I want to wait until the song has finished playing before the speech is executed.
    Theoretically there is one more line to execute to start playing
    WindowsMediaPlayer1.URL = "C:\Song1.mp3"
    WindowsMediaPlayer1.Controls.play
    But VB6 does not object if you omit it.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Program bypasses some code

    Since I don't have much of my VB6 code on this machine I just did an example from scratch if you couldn't figure it out from my previous post.
    Code:
    Private Sub Form_Load()
      WindowsMediaPlayer1.URL = ("C:\c\Alarm01.wav")
    End Sub
    
    Private Sub WindowsMediaPlayer1_PlayStateChange(ByVal NewState As Long)
      If NewState = 1 Then  'Stopped
        MsgBox "Hello There"
      End If
      
    End Sub
    Of course, you would want to replace the MsgBox with your speech code.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    13

    Re: Program bypasses some code

    Hi Passel,

    This works well, thanks. Would you have an idea about the following :

    Let's say I have a playlist with 8 songs and I want to listen to them :

    When I write :

    For i=0 to 7
    Set Itm = WindowsMediaPlayer1.currentPlaylist.Item(i)
    WindowsMediaPlayer1.Controls.playItem Itm
    Next i

    the program plays the last song, it skips 0 to 7th. song.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Program bypasses some code

    It doesn't skip them, you are canceling them by starting the the next one before they have a chance to start.

    I do not use media player in code but I would think if you are using a play list then you would just need to start the first one and media player would play them all ?

    You could also use the event like shown already and each time the event fires start a different song.

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Program bypasses some code

    As DataMiser says, you can add songs to a MediaPlayer PlayList and it will play the songs itself.

    <edit> I added the below before looking at your code and realizing that you are adding songs apparently to the playlist. I would assume you just need to start playing the first item and it should continue on automatically. If you want to read how I did it without using the builtin playlist you can continue reading
    </edit>
    <edit2>
    I see that the above edit was was DataMiser surmised as well in his post. I should have read a little further before posting.
    </edit2>

    But, as my VB6 code was originally written for the earlier version of MediaPlayer (around 15 years ago) which didn't have a playlist, I never got around to working with the built in playlist with the newer version of MediaPlayer (i.e. one available for the last 12 years or so). I maintained my own list and based on using the PlayStateChanged event to detect when a song was fininished and starting the next song.
    I'm now on the machine with more of my old VB6 code, so I'll take a look at what I did.
    Code:
      Const PLAYSTATE_Stopped = 1
      Const PLAYSTATE_Paused = 2
      Const PLAYSTATE_Playing = 3
      Const PLAYSTATE_MediaEnded = 8
    
      'added for new WMP
      Dim DequeueASong As Boolean
    
    
    Private Sub MediaPlayer1_PlayStateChange(ByVal NewState As Long)
    'I had other states in the case, but aren't currently needed
        Select Case NewState
          Case PLAYSTATE_MediaEnded
            DequeueASong = True
        End Select
    End Sub
    
    Private Sub Timer1_Timer()
      If DequeueASong Then
        DequeueASong = False
        Play_Next_Song
      End If
    End Sub
    The above is the crux of the code. The Call to sub Play_Next_Song is where I chose the next song to be played and loaded it. There was a lot of code in there using various indexes and calling other subs so I didn't bother posting it because it depends on a user defined type and other mechanisms that would just complicate things. You should be able to just keep track of which song you played from your list and start the next one.

    The reason it sets a flag in the PlayStateChange event and has code in the timer to start the next song is because starting a new song in the PlayStateChange event causes a PlayStateChange event and that seemed to corrupt the process somehow and the new song ended up not starting. Setting the flag and leaving the event, and then starting the song at the next timer tick, prevented that failure.

    p.s. The code has the comment "added for new WMP" referring to the boolean because the original Windows Media Player had a SongEnded event, and you could start a new song in that event without problem. The "new" WMP doesn't have the event any longer, or doesn't trigger it, and couldn't start a new song within the new event, so the boolean was added.
    Last edited by passel; Sep 22nd, 2017 at 10:59 AM.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    13

    Re: Program bypasses some code

    Thanks for all your valuable information.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width