Link to home
Start Free TrialLog in
Avatar of wayneray
waynerayFlag for United States of America

asked on

Showing multiple MPEG files

I have a VB 6.0 survey app I am trying to reconfigure to show multiple MPEG files.  The app reads a text file to display the questions and when it reaches the que for the video it currently shows one MPEG fine.  I want to show one master MPEG then show 15 small scenes randomly during the same session. I am using the windows media control and calling the video using the shellexecute command. My problem:  I can't get it to read the master MPEG first then enter a random series to show the 15 short clips.  It either shows only the master or starts the random series. Can this be done in VB 6? What I believe I need is a random number generator that always produces 1 first then the other 15 numbers are random.
Avatar of unknown_routine
unknown_routine
Flag of United States of America image

If you are trying to show more than one mpeg using VB6 i suppose this is not possible(except maybe by some very complicated method).

this is because VB6 does not support multithreading.
Avatar of wayneray

ASKER

I am not trying to show multiple mpegs at the same time.  I have 16 mpegs assigned to a variable array. I use a command button to randomly call the mpegs in my program and it will show them randomly each time I push the button but I cannot get mpeg #1 to show first followed by the other 15.  If there is a way to generate random numbers where the first number is always 1, that is what I need.  
you want the 1st random number to be 1?? that is not a random number.

if you  need to show the first member always then it should not created as a random number


if you can paste your code here I can help you more.


ASKER CERTIFIED SOLUTION
Avatar of nickaskew
nickaskew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the current code I am using as a workaround.  I commented out the random number piece of code and used "IF" statements to call each video.  I have another sub that calls this sub.  I am going to try Nick's idea. I think he is on to something.

Sub playvideo(q1 As String)


Dim tempAr(2 To 16)
Dim n As Integer
'n = 1
'Do
'      Randomize Timer
'      n = Rnd * 10 + 5
'   Loop While tempAr(n)
'   tempAr(n) = 1
'   If n = 1 Then
If cmdButton(n).Caption = "The Stepford Wives" Then
ShellExecute Me.hWnd, "open", videoversions(1), vbNull, vbNull, 1
End If
If cmdButton(n).Caption = "Segment A" Then
ShellExecute Me.hWnd, "open", videoversions(3), vbNull, vbNull, 1
  End If
If cmdButton(n).Caption = "Segment B" Then
ShellExecute Me.hWnd, "open", videoversions(2), vbNull, vbNull, 1
  End If
If cmdButton(n).Caption = "Segment C" Then
ShellExecute Me.hWnd, "open", videoversions(5), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment D" Then
ShellExecute Me.hWnd, "open", videoversions(7), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment E" Then
ShellExecute Me.hWnd, "open", videoversions(4), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment F" Then
ShellExecute Me.hWnd, "open", videoversions(6), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment G" Then
ShellExecute Me.hWnd, "open", videoversions(16), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment H" Then
ShellExecute Me.hWnd, "open", videoversions(12), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment I" Then
ShellExecute Me.hWnd, "open", videoversions(13), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment J" Then
ShellExecute Me.hWnd, "open", videoversions(9), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment K" Then
ShellExecute Me.hWnd, "open", videoversions(14), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment L" Then
ShellExecute Me.hWnd, "open", videoversions(8), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment M" Then
ShellExecute Me.hWnd, "open", videoversions(15), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment N" Then
ShellExecute Me.hWnd, "open", videoversions(11), vbNull, vbNull, 1
  End If
  If cmdButton(n).Caption = "Segment O" Then
ShellExecute Me.hWnd, "open", videoversions(10), vbNull, vbNull, 1
  End If



'ShellExecute Me.hWnd, "open", videoversions(n), vbNull, vbNull, 1

   
   


End Sub
>> If you are trying to show more than one mpeg using VB6 i
>> suppose this is not possible(except maybe by some very
>> complicated method).

>> this is because VB6 does not support multithreading.


It should definitely be possible to show more than one video simultaneously without multithreading, this is what the MSTASK.TSK file is for.

Also VB6 does support multithreading via API, see some examples here:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=14479&lngWId=1

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconCreatingMultithreadedTestApplication.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconapartmentmodelmultithreadinginvisualbasic.asp

-Burbble
Hehe,
 Thanks for commenting on my comments!

I guess  wayneray Answered me that he does not want to watch 2 mpegs at the same time.

But admitingly those links you supplied are interesting, but I really don't think they belong to

beginer or even mid level VB(as they are mentioned as advanced by their authors).  And the most important thing is that all varieties are highly unstable,

1st and second example were crashed several times on my computer. This is a known problem when you try to simluate multithreading in VB.

Ooh, I misunderstood what wayneray was trying to accomplish here.

>> I am not trying to show multiple mpegs at the same time.

lol, I must have been asleep while I was reading this.

Just ignore me, I tend to ramble sometimes :D

-Burbble
Nick your suggestion works fine.  I have a small problem with duplicate numbers being generated but that is fixable.  Thanks for your suggestion.
Avatar of nickaskew
nickaskew

Thanks Wayneray,

If you would like to post the code you eventually stuck with for generating the random numbers and selecting the videos, I wouldn't mind tidying it up for you - that way others viewing this question might get further help and you may end up with a solution you are totally happy with :)

Regards

Nick
This works great.  Just duplicates show up.

Sub playvideo(q1 As String)


Dim tempAr(2 To 16)
Dim n As Integer



If (bMainVidShown = True) Then
 ' We have already shown the main video, calculate a random number between 2-16 and show the appropriate vid.
 
  Do
      Randomize Timer
      n = Rnd * 10 + 5
   Loop While tempAr(n)
   tempAr(n) = 1
   
ShellExecute Me.hWnd, "open", videoversions(n), vbNull, vbNull, 1

 Else
 ' bMainVidShown is false, which means this is the first time this piece of code has run..
 ShellExecute Me.hWnd, "open", videoversions(1), vbNull, vbNull, 1
 bMainVidShown = True

End If
   
   
   


End Sub
Hi Wayneray,

I have modified your original sub slightly and written a new function for you to use called UnusedRandom() which returns a random sequence of numbers without repeating (so it isn't exactly random.. but, ya know..)


Sub playvideo(q1 As String)
    Dim tempAr(2 To 16)
    Dim n As Integer
    '
    If (bMainVidShown = True) Then
        ' We have already shown the main video, calculate a random number between 2-16 and show the appropriate vid.
'        Do
'            Randomize Timer
'            n = Rnd * 10 + 5
'        Loop While tempAr(n)
'        tempAr(n) = 1
        '
        ShellExecute Me.hwnd, "open", videoversions(UnusedRandom(15) + 1), vbNull, vbNull, 1
        '
    Else
        ' bMainVidShown is false, which means this is the first time this piece of code has run..
        ' ShellExecute Me.hwnd, "open", videoversions(1), vbNull, vbNull, 1
        bMainVidShown = True
    End If
End Sub


' This function will return a random number between 1 and 'iKey'
' Providing iKey is the same from call to call, every number from 1 to iKey
' will be returned without repetition until the entire sequence of numbers
' have been returned, at which point the function resets itself and it begins
' dishing out 1 to iKey randomly again...
'
' Example:
' Dim iResult As Integer
' iResult = UnusedRandom(5)
'
' Possible Values for iResult = 1, 2, 3, 4 or 5.  In a random order that doesn't repeat until
' every number in the sequence has been returned.
'
Private Function UnusedRandom(ByVal iKey As Integer) As Integer
    Dim iTmpLoop As Integer
    Dim iTmpRandomNo As Integer
    Static iKeySize As Integer
    Static bUsedVals() As Boolean

    If (iKey < 1) Then
        MsgBox "Function UnusedRandom: Cannot accept a number less than 1, attempted to use [" & iKey & "]"
        UnusedRandom = -1
        Exit Function
    End If
    If (iKeySize <> iKey) Then
        '
        ' Either first time this function has been called, or random number
        ' maximum has been changed.
        '
        ' Allocate enough boolean vals to account for the maximum random
        ' number we have been asked to return.
        ReDim bUsedVals(iKey)
        iKeySize = iKey
        For iTmpLoop = 1 To iKeySize
            bUsedVals(iTmpLoop) = False
        Next
        iTmpRandomNo = Int((iKeySize * Rnd) + 1)
        bUsedVals(iTmpRandomNo) = True
        UnusedRandom = iTmpRandomNo
        Exit Function
    Else
        ' We have already created an array of size iKey, we have several
        ' things to check now:
        ' 1) first we see if we have already handed out every unique number
        '    we could, is so, we start the random number handout again.
        ' 2) we generate a random number and check whether we have handed it
        '    out already, if we have, we generate another and check.
        '
        ' Iterate through every member in the array, stop on the first one
        ' which is false - this proves we havn't handed out all the possible
        ' values yet, so we then go onto generate the number.
        For iTmpLoop = 1 To iKey
            If (bUsedVals(iTmpLoop) = False) Then
                ' Generate another number
                While True
                    iTmpRandomNo = Int((iKeySize * Rnd) + 1)
                    If (bUsedVals(iTmpRandomNo) = False) Then
                        ' We havn't used this number yet..
                        bUsedVals(iTmpRandomNo) = True
                        UnusedRandom = iTmpRandomNo
                        Exit Function
                    End If
                Wend
            End If
        Next
        '
        ' If we got this far, we must have already handed out every possible
        ' random number, lets erase the array and start again.
        '
        For iTmpLoop = 1 To iKeySize
            bUsedVals(iTmpLoop) = False
        Next
        iTmpRandomNo = Int((iKeySize * Rnd) + 1)
        bUsedVals(iTmpRandomNo) = True
        UnusedRandom = iTmpRandomNo
        Exit Function
    End If
End Function


Best Regards

Nick
I tested your suggestion twice.  Both times it began duplicates after the first 8 numbers were generated.
Nick,
 I really appreciate your help.  If I understand your function correctly, it should randomly generate numbers 1 through 15 and hand them out.  However, it is starting over after handing out the first 8.  Also, the last video is videoversions(16).  If I change the argument to 16, I get an out of range error.  The main video videoversions(1) can't be in the mix of random generated numbers.

Wayne
Hi

I tripple checked the function, it definately presents the first batch of numbers randomly, without repeating, until it has run out, it then starts over.

The function generates random numbers from 1 to X, so if you want random numbers from 2-16, you should use UnusedRandom(15)+1, which would give you 2-16..

Make sure you always use the same KEY when you use UnusedRandom, the moment you change the key the existing list is erased and the random list starts over again.

This example source code uses the function, it shows you the numbers do not repeat until the entire series has been displayed.

http://www.nickaskew.com/ee/randomvids.zip

Regards

Nick
Nick,
The problem maybe my program calls the video then goes back to another sub and asks three or four questions then comes back to playvideo().  I don't disagree with what you are saying.  I am just trying to discover why my program consistently only pulls 8 numbers then begins repeating.
Nick,
I put a breakpoint in the UnusedRandom function and sure enough after 8 numbers it is resetting.

Wayne
Wayne,

Did you download the ZIP I gave as a link above? - would you mind downloading it and opening it within your VB (it is source code only - 1 form) - Does it reset after 8? - I cannot replicate the problem you are having, I am really baffled!

Nick
I did download it and it works fine.  Thanks for all your help. I have been put on another task for the moment.  If I don't get it fixed soon, I will give you a link my program and you can play with it.  I will have to remove some of my company stuff first.  It will probably be tomorrow before I can get to it.  Thanks again.
Just wanted to let everyone know I fixed the repeating problem.  It was a VB thing.  The video array started with "1" instead of "0".  Once I changed that, it worked fine.  Thanks again for your help.