-
Sep 21st, 2017, 01:56 AM
#1
Thread Starter
New Member
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
-
Sep 21st, 2017, 05:20 AM
#2
Re: Program bypasses some code
Let's see your code where all this happens (doesn't happen).
-
Sep 21st, 2017, 05:41 AM
#3
Thread Starter
New Member
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
-
Sep 21st, 2017, 06:04 AM
#4
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?
-
Sep 21st, 2017, 06:14 AM
#5
Thread Starter
New Member
Re: Program bypasses some code
-
Sep 21st, 2017, 06:37 AM
#6
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
-
Sep 21st, 2017, 06:53 AM
#7
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.
-
Sep 21st, 2017, 06:55 AM
#8
Thread Starter
New Member
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.
-
Sep 21st, 2017, 07:12 AM
#9
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.
-
Sep 21st, 2017, 07:56 AM
#10
Thread Starter
New Member
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.
-
Sep 21st, 2017, 08:08 AM
#11
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.
-
Sep 21st, 2017, 09:48 AM
#12
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.
-
Sep 22nd, 2017, 03:23 AM
#13
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|