Thread: Implementing play/pause and stop commands using C++

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    5

    Implementing play/pause and stop commands using C++

    Hello guys,
    I wanted to have some help on how to implement video files using visual studio 2010.

    What I want to do is:
    - To open the file to play the video.
    - While the video is playing, I can use command prompt to type in a certain letter in order for it to play/pause, stop or terminate the routine.

    I tried implementing the pause command myself and the code accepts the letter but it doesn't do anything afterwards and it just asks the user again whether they would like to input a command.

    This is what I tried to do:
    Code:
    system("start C:\\Music_Videos\\Girls_Generation-hoot_SHORT.wmv");
    
                while (zgb_rx_check() == 0)
                {
                    // countinually check for data indicating routine has stopped
    
    
                    while (count == 0 || count == 1)
                    {
                        //Check_if_finished();
                        cout << "\n\n Would you like to input a command? \n"; // asks for a command (pause command)
                        scanf("%c", &letter);
                        gets(letter);
                        count = findletter(letter);
    
    
                        if (count = 0)
                        {
                        }
                        else if (count = 1)
                        {    
                            system("pause C:\\Music_Videos\\Girls_Generation-hoot_SHORT.wmv");
                        }
    
    
                    }
                }
    
    int findletter(char letter[])
    {
        int value;
    
    
    
    
        if (letter[0] == 'P' || letter[0] == 'p')
        {
            value = 1;
        }
    
    
        else if (letter[0] != 'P' || letter[0] != 'p')
        {
            value = 0;
        }
    
    
            return value;
    }
    If there are better ways to implement video controls let me know.
    I hope you have the time to help me further with this issue.
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Personally, I think you need to step back and learn C++ for a while, before typing in random bits of syntax and hoping something will work.

    > system("start C:\\Music_Videos\\Girls_Generation-hoot_SHORT.wmv");
    Do you know what actually happens here?
    Start - Start a program | SS64.com

    Windows looks up in the registry the preferred program for dealing with .wmv files. Having found that, it runs that program (say mplayer.exe) and passes your filename as a parameter to mplayer.exe. You then see your audio/visual experience happening.

    Your program on the other hand knows NOTHING about what is happening. It doesn't know what program was launched, nor does it know say the process ID of the instance of mplayer.exe rendering your file.

    > while (zgb_rx_check() == 0)
    Huh?
    zgb_rx_check

    > scanf("%c", &letter);
    > gets(letter);
    1. how is letter declared?
    2. why do you read it twice?
    3. why are you using C and not C++ ?
    4. why are you using the most evil function in the world -> SourceForge.net: Gets - cpwiki

    > if (count = 0)
    Until you know the difference between = and ==, stick to the learning phase.

    > system("pause C:\\Music_Videos\\Girls_Generation-hoot_SHORT.wmv");
    Again, nothing to do with media playback, this pauses the shell.
    Pause | SS64.com

    Oh, and to top it all off, it's cross-posted
    Is there a way to play/pause and stop vi - C++ Forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    5
    Yes I know that this code has got some major issues with it.

    system(start) - I know how it works and why people don't use it. It was just the easy way out for me.

    while (zgb_rx_check() == 0) - I'm also using a device to implement it. It's just to check to see whether the routine has finished or not.

    scanf and gets(letter) - ignore scanf since I forgot to comment/delete that section. It is declared with these initial values.
    Code:
    int count = 0; // count for reading true and false values
    char letter[STRING_LENGTH];
    the code should be count == 0 and count == 1. I made a mistake in that part as well.

    Basically I just used the ideas of what was learnt in my studies and tested it to see whether it worked or not. I also took some shortcuts.



    What would be an efficient way to make the program read the process of the media player?
    Last edited by TTNH03; 04-15-2014 at 06:13 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    IANA windows programmer - moved to windows forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by TTNH03 View Post
    system(start) - I know how it works and why people don't use it. It was just the easy way out for me.
    You missed the crucial piece of advice Salem gave you....

    You WANT to build a remote control; the media player is controlled by the console app.
    [think: remote control plane]

    You HAVE built a launcher; the console launches media player but has no control or knowledge of what media player is doing
    [think: paper plane, once thrown you have no control over it]

    You need to create a link to the media player.

    I do not use consoles for this type of thing, I consider it to be the wrong tool for the job, (and I have not coded much in WIN32 lately) so my experience is not overly applicable.

    I suggest...

    You could read up on using COM. In COM you create an 'interface' object; a contract on how 2 applications will exchange information. Not sure if the COM interface allows you to interact with the Media Player controls.

    Using the Windows Media Player Control in a Console Application (Windows)

    You could try MCI, a detail example is here;
    Media Player - CodeProject

    Do you know the difference between 'structured' and 'event driven' programming? You will struggle with design if you don't....
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    5
    Quote Originally Posted by novacain View Post

    Do you know the difference between 'structured' and 'event driven' programming? You will struggle with design if you don't....
    Not in good detail though. Is there any links which can give me more descriptive information about it?


    Thanks for the information on using COM interface object. I'm just trying to find other relative pieces of code on how to run it.


    I was wondering if you can use windows form application (C++ format) to run windows media player since you can create 'buttons' which can execute commands such as play, stop or open other videos.
    The 'button' in the code can then be used to execute events.
    I was also trying to experiment with this type of code before and was curious to know if this also works as well?

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    It depends on your motive; do you want to learn C++, Windows Programming or build a media player? Is programming a hobby or possible profession?

    It would be easier to build a media player in C# or VB.NET than C++ / MFC, and better to learn Windows Programming via C# (as MFC and VB.NET do not have as much info available).
    C# is also more in commercial demand now than VB.NET or C++ (in my experience).

    Quote Originally Posted by TTNH03 View Post
    Not in good detail though. Is there any links which can give me more descriptive information about it?
    Try Google or Wikipedia.

    Structured flows line by line, never stopping, so you always know which line will execute next.
    Event driven waits for an event, calls the handler for that event (which may generate other events), then waits again.

    Quote Originally Posted by TTNH03 View Post
    I was wondering if you can use windows form application (C++ format) to run windows media player since you can create 'buttons' which can execute commands such as play, stop or open other videos.
    The 'button' in the code can then be used to execute events.
    I was also trying to experiment with this type of code before and was curious to know if this also works as well?
    Yes. WIN32 / .NET is event driven, which is why it is a better tool (than a console) for this type of application.

    Read the MCI example I linked above.
    It uses MFC, a MS C++ wrapper for the WIN32 API to create a media player (with a horrible round UI). It could be converted to pure WIN32 C++.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Apr 2014
    Posts
    5
    Quote Originally Posted by novacain View Post
    It uses MFC, a MS C++ wrapper for the WIN32 API to create a media player (with a horrible round UI). It could be converted to pure WIN32 C++.
    When you mean convert do you need to implement a code to convert it or can you just cut/paste it into the C++ code.


    If I remember correctly C/C++ also uses WIN32 so it doesn't need a method for MFC to convert into WIN32. I might be wrong though.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    MFC is a wrapper around the WIN32 API, designed for RAD (rapid application development).

    This means you call the MFC version of a function, usually using a class type variable and the MFC code calls the WIN32 API, handles errors and returns a result.

    You cannot just cut and paste.

    MFC is C++, it uses classes to invoke/encapsulate WIN32 calls. MFC also writes many parts for you; callbacks, message pumps, etc

    Some parts are simple;

    Code:
    //get the desktops area we can draw on
    
    //MFC 
    CWnd* MFCDesktop = GetDesktopWindow();//create a CWnd object and fill it with the main desktops data
    
    //WIN32
    HWND WIN32Desktop = GetDesktopWindow(); //get a pointer to the main desktop (HWND == HANDLE == void*)
    
    //but then things become more complicated...
    //MFC
    CRect MFCClientRect;
    MFCDesktop.GetClientRect(&MFCClientRect);//use the object already filled with the main desktops data to get the client area
    
    //WIN32
    RECT WIN32ClientRect;
    GetClientRect(WIN32Desktop, &WIN32ClientRect);// call the WIN32 function to get client areas, tell it to find the main desktops client area by passing in the correct pointer (HWND == HANDLE == void*)
    Other parts will require you to write much more code, even creating whole functions (ie callbacks, message pumps) that MFC handles internally.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Apr 2014
    Posts
    5
    Thanks for the details,
    I'm thinking of using windows form application since I already have some of the base structure for it already.
    I was also wondering if there are some methods which involve converting unmanaged C++ code into managed ones since I also have to some C++(.cpp) code into it as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop/pause program after asking for input
    By mrppinzon in forum C Programming
    Replies: 1
    Last Post: 10-11-2012, 01:46 PM
  2. WMP11 Play/Pause Issue
    By carrotcake1029 in forum Windows Programming
    Replies: 0
    Last Post: 06-23-2008, 06:28 PM
  3. Stop button doesnt stop a function (using PostMessage)
    By scwizzo in forum Windows Programming
    Replies: 7
    Last Post: 03-07-2008, 07:54 PM
  4. how to play play and stop wav files
    By cnu_sree in forum Linux Programming
    Replies: 4
    Last Post: 08-14-2006, 11:11 PM
  5. Pause in OpenGL, but don't stop drawing... ?
    By Arker in forum Game Programming
    Replies: 4
    Last Post: 10-16-2003, 10:34 PM