Results 1 to 27 of 27

Thread: [RESOLVED] Playing music

  1. #1

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Resolved [RESOLVED] Playing music

    I am using FilgraphManager and IMediaEventEx. I have several panels on my form. Each panel is set up as an array. On each pane ther are some labels and a play image and a stop image. When the user selects a panel (music option), they click on the associated play image. The music plays. I make all the play images and the other stop image not enabled. I load a different image (greyed out image). when the user clicks on the associated stop image, the music stops and I enable all the play and stop images(replace them with black images)

    When the music stops automatically (the end of the selection is reached), I need to trap that event so I can enable all the images, etc. I have no idea how to tell, in code, when the music ends.

    Thanks for you input

  2. #2
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: Playing music

    Im not using ActiveMovie control anymore,
    but Im sure theres a property to see if the music is playing or not.
    use a timer to check that?

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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

    Re: Playing music

    I also, don't use...but, will this link help?

    https://docs.microsoft.com/en-us/win...ol-imediaevent
    Sam I am (as well as Confused at times).

  5. #5
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: Playing music

    I looked into my old projects, here what u can do:

    Code:
    Private Control     As IMediaControl
    Private EventEX     As IMediaEventEx
    Private WithEvents MediaControl As VB.CommandButton
    
    Private Sub LoadMP3(ByVal Filename$)
        Set Control = New FilgraphManager
        Control.RenderFile Filename
        Set EventEX = Control
        Set MediaControl = Controls.Add("VB.CommandButton", "mediacontroltemporarybutton", Me)
        EventEX.SetNotifyWindow MediaControl.hWnd, &H200&, 0
        Control.Run
    End Sub
    
    Private Sub MediaControl_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim EventCode&, Param1&, Param2&
    
        With EventEX
            .GetEvent EventCode, Param1, Param2, 0
            .FreeEventParams EventCode, Param1, Param2
            If EventCode = &H1& Then Me.Controls.Remove MediaControl: MsgBox "done"
        End With
    End Sub
    
    Private Sub Form_Load()
        LoadMP3 "FOUNTAIN.mp3"
    End Sub

  6. #6

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: Playing music

    baka,
    Your code is great. It works as presented. I need to know a list of event codes for this control. I placed a invisible command button on the control just so the code as point of start the click procedure.
    In the MediaControl_MouseMove routine, I need to determine when the event is fired. However, it fired when the music starts and when the music stops. It is during the stop I re-enable the controls. The controls are also re-enabling the start.

  7. #7

  8. #8

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: Playing music

    OK, I solved the problem. I was using the wrong error code. I was using 13, it should have 14. I still vave not correlated the vb constants with the numeric values, but that's ok.

    Thanks to all.

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,156

    Re: [RESOLVED] Playing music

    This is weird that exactly EC_PAUSED (14) is missing from wine's header file above.

    Linking evcode.h from DirectX 8.0 SDK which does contain EC_PAUSED: https://github.com/LiveMirror/freedo...clude/evcode.h

    Code:
    #define EC_PAUSED                           0x0E
    // ( HRESULT, void ) : application
    // Notify application the previous pause request has completed
    The original header file includes short explanation of the parameters as well.

    Here first parameter is an HRESULT and the second one is unused.

    cheers,
    </wqw>

  10. #10

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    I found that after I closed the program (still only in the IDE), and reopened, the event code was now 13, not 14. So, I longer check for an event code value. I placed a timer on the form set to 30 seconds. When the music starts I enable the timer. I count the number if time through the timer routine. If the value is greater thnan 1 and the event is initiated, I process the music end routine. The event also happens when the music starts. In this case, since the timer count is less than 1, I ignore the end music processing.

  11. #11
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,250

    Re: [RESOLVED] Playing music

    An easier method is to measure the track length of the music you wish to play. You do that manually using audacity. When you have the length in seconds you can add that to the filename.

    eg. 11Track01.wav - this track is 11 seconds long.

    All your tracks are checked for length and the filenames adjusted accordingly.

    When you play the .wav file you read the filename, extract the duration, set a timer to that length, in this case 11 secs. When the timer reaches 11 seconds your code causes some event to occur. Very straight forward, no complex code required. No need to determine when the sound has finished playing because you know already.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  12. #12

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    yereverluvinuncleber very interesting post. I will evaluate for my needs. Thanks

  13. #13

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    Post #11 was very interesting. I never heard of Audacity, but I already have VB6 code to get info about an .mp3 file. It is easy for me to strip out all the other info that I do not need for this.

    However, my .mp3 files are anywhere from 1 min 53 seconds long to 4 min 27 seconds long. A VB6 timer is only good for about 1 min max, and it is not designed to be very accurate. I don't think the poster of #11 is suggesting that I set the timer to 10 second (or thereabouts) intervals and use CPU cycles to iterate though the timer as many times as I need, rather than using the cpu to play the music.
    BUT, I like his concept.

  14. #14
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: [RESOLVED] Playing music

    AccessShell u can't get anything better than SetNotifyWindow. it will notify you when its done.
    Im sure if you look into it you can make it better.
    as you wrote: u need to know if its start or end
    well u can also check the current position of the song. the status etc.
    you can also use the .tag, when u load the mp3, u set the .tag = "", and when it first fire up, u set .tag = "playing"
    that way u just check .tag if = "" or "playing" to know if its first time or end of song.

  15. #15
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,250

    Re: [RESOLVED] Playing music

    Quote Originally Posted by AccessShell View Post
    I never heard of Audacity
    If the music files are known to the program already, ie. you provide those values hard coded within your program or within the filename as suggested. You can extract duration using an external audio program such as Audacity that allows you to manipulate audio, in this case you can ignore all else as you just want the duration.

    If the music files are from 'elsewhere' and you never know the length then use your VB6 code to analyse the .mp3 and derive the duration. If you know the recording frequency in khz then you can divide the file length by the frequency to derive the duration (if I remember correctly). I'll leave that to you.

    Quote Originally Posted by AccessShell View Post
    A VB6 timer is only good for about 1 min max, and it is not designed to be very accurate.
    You can use any timer method you want to achieve the accuracy you need. No need to use a VB6 timer with such a long duration. A search here on the forum will provide other timer alternatives.

    However, a much more simple solution, you might have a basic VB6 timer that goes off every 1-100ms and during that timer event you increment a global counter that when the particular counter value has been reached activates your chosen event. In this way you can achieve the accuracy you desire and you overcome the VB6 timer limitation.

    PS. If you need sample code for such a timer (it is easy stuff) - just say.
    Last edited by yereverluvinuncleber; Dec 3rd, 2021 at 07:28 AM. Reason: Added the postscript
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,156

    Re: [RESOLVED] Playing music

    Quote Originally Posted by yereverluvinuncleber View Post
    However, a more simple solution, you might have a basic VB6 timer that goes off every 1-100ms and during that timer event you increment a global counter that when the particular counter value has been reached activates your chosen event. In this way you can achieve the accuracy you desire and you overcome the VB6 timer limitation.
    You can set the timer to fire with whatever frequency you seem fit and simply measure elapsed time since the track started playing in the Timer event implementation.

    I would use QueryPerformanceCounter/Frequency just [strike]for the thrill of it[/strike]to be accurate to nanoseconds like this

    Code:
    Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
        Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
    
    Private Property Get TimerEx() As Double
        Dim cFreq           As Currency
        Dim cValue          As Currency
        
        Call QueryPerformanceFrequency(cFreq)
        Call QueryPerformanceCounter(cValue)
        TimerEx = cValue / cFreq
    End Property
    TimerEx is a better replacement of the built-in Timer but even it will suffice for the purpose of measuring elapsed time with 10 ms accuracy I guess.

    Of course all this is in vain provided that there is SetNotifyWindow method which is *the* way to implement this and everything else is just the kind of crutch you see done in VB-land (out of desperation).

    cheers,
    </wqw>

  17. #17
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,250

    Re: [RESOLVED] Playing music

    Quote Originally Posted by wqweto View Post
    Of course all this is in vain provided that there is SetNotifyWindow method which is *the* way to implement this and everything else is just the kind of crutch you see done in VB-land (out of desperation).
    I don't 100% agree. If you know the mp3/wav file duration and it isn't going to change, what's the point of all the complex code and API calls? The file duration is more or less a constant. You just need to know it and then you code it in as such.

    A VB6 timer can be as accurate as you are likely to need within a multi tasking o/s given this fairly simple requirement. Nothing more required.

    I have implemented more complex timers to achieve similar to the above but I did it for the fun of it and to learn how to implement timers in code - but for this, it isn't really necessary.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  18. #18
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,156

    Re: [RESOLVED] Playing music

    Quote Originally Posted by yereverluvinuncleber View Post
    I don't 100% agree. If you know the mp3/wav file duration and it isn't going to change, what's the point of all the complex code and API calls? The file duration is more or less a constant. You just need to know it and then you code it in as such.
    You don't always know the duration of the MP3. The file can be broken. The file can be in different format. The ID3 tag is broken. The ID3 tag reader is broken.

    There is no API involved -- it's just calling SetNotifyWindow method like in regular VB6 class and then receiving MouseMove event like in regular VB6 control. There is no API/subclassing required to be 100% accurate with players playback state.

    cheers,
    </wqw>

  19. #19
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,250

    Re: [RESOLVED] Playing music

    Quote Originally Posted by wqweto View Post
    You don't always know the duration of the MP3.
    Who is this person that doesn't know? Now, as far as I can tell the original poster DOES know the duration of his own mp3 files. He can easily determine the duration.

    My answer I believe is the easiest for resolving his requirement alone. I'm not suggesting it is the correct answer for ALL requirements. I am sure the other answers are equally valid, perhaps overkill, that is what I meant.

    Anyway, I am off now, my help is done here and I don't wish to argue or even cause an argument about the 'best' way of doing anything, that is subjective as always and the OP will choose the one that best suits his needs, probably the easiest...
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  20. #20
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: [RESOLVED] Playing music

    not sure why you would use a timer at all.
    the SetNotifyWindow is created for the purpose to get the status immediately.
    you could say that "SetNotifyWindow" gives you a precise timer, so you already have a timer already, part of the activemovie control type library.

    use what the library is giving you instead of adding your own hacks.

  21. #21

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    The SetNotifyWindow does indeed trigger the event. But it triggers it when the music starts and when it ends by itself. I use the timer to ignore the start notification. The timer is set to half a minute. I increment a counter in the timer routine. When the notification triggers the event, I ignore the processing if the counter is less than 2, otherwise I perform the end of music routine.

  22. #22
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: [RESOLVED] Playing music

    did u try the suggestions I wrote?

    also I dont get anything when I start the mp3, only when its done.
    are u sure u are using EC_COMPLETE?

    Const EC_COMPLETE = &H1&
    Last edited by baka; Dec 3rd, 2021 at 12:45 PM.

  23. #23

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    Yes I am


    Code:
    Private Function PlaySound(mstrSoundFile As String)
    
    
        Set FilgraphManager = New QuartzTypeLib.FilgraphManager
        Set IMediaEventEx = FilgraphManager
        Set MediaControl = Controls.Add("VB.CommandButton", "mediacontroltemporarybutton", Me)
        IMediaEventEx.SetNotifyWindow MediaControl.hWnd, &H200&, 0
        With FilgraphManager
            '.RenderFile "https://dl.espressif.com/dl/audio/gs-16b-2c-44100hz.mp3"
            .RenderFile mstrSoundFile
            .Run
        End With
    
    
    End Function
    
    Private Sub MediaControl_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)Dim EventCode&, Param1&, Param2&
       
            With IMediaEventEx          'EventEX
                .GetEvent EventCode, Param1, Param2, 0
                .FreeEventParams EventCode, Param1, Param2
                If EventCode = &H1& Then  
                    DO MY STUFF HERE                       
                    Me.Controls.Remove MediaControl                
                End If
            End With
         
    End Sub

  24. #24
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: [RESOLVED] Playing music

    Hmm. not sure why you get it two times. I only get it once.
    but if you change it like this it will work, even if its fired twice: (also remember to use Option Explicit)

    Code:
    Option Explicit
    
    Private FilgraphManager As IMediaControl
    Private IMediaEventEx   As IMediaEventEx
    Private IPosition       As IMediaPosition
    Private WithEvents MediaControl As VB.CommandButton
     
    Private Function PlaySound(mstrSoundFile As String)
        Set FilgraphManager = New QuartzTypeLib.FilgraphManager
        Set IMediaEventEx = FilgraphManager
        Set IPosition = FilgraphManager
        IMediaEventEx.SetNotifyWindow MediaControl.hWnd, &H200&, 0
        With FilgraphManager
            .RenderFile mstrSoundFile
            .Run
        End With
    End Function
    
    Private Sub MediaControl_MouseMove(button As Integer, Shift As Integer, X As Single, Y As Single)
        If IPosition.CurrentPosition = IPosition.Duration Then MsgBox "Done"
    End Sub
    
    Private Sub Form_Load()
        Set MediaControl = Controls.Add("VB.CommandButton", "mediacontroltemporarybutton", Me)
    End Sub
    Last edited by baka; Dec 3rd, 2021 at 03:14 PM.

  25. #25

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    I ALWAYS use OPTION EXPLICIT.

    I'll try your code

    Thanks

  26. #26

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    794

    Re: [RESOLVED] Playing music

    baka, your code is nice and slick. It works as promised. I took out all references to the timer.

    Thanks

  27. #27
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,730

    Re: [RESOLVED] Playing music

    I looked into it a bit more, I thought, why do we need mousemove? why not just click?
    so replace with:

    Code:
    IMediaEventEx.SetNotifyWindow MediaControl.hWnd, &HF5&, 0
    
    Private Sub MediaControl_Click()
        If IPosition.CurrentPosition = IPosition.Duration Then MsgBox "Done"
    End Sub

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