Wait for mpv command to finish before returning to main in C

244 views Asked by At

I'm writing a small program in C on Linux and my intentions with it is to do the following:

  1. Display a menu
  2. Ask a user to enter a value
  3. Read the value and run the corresponding command
  4. Wait for the command to finish before breaking (returning to main menu)
  5. Repeat until the user enters the value corresponding to "Exit/Quit"

So far, this is the code:

#include <stdio.h>
#include <stdlib.h>


int main()
{
    printf("\n\n\t\tListen to music on CLI\n\n\n");
    int choice;
    
    while(1)
    {
        printf("1. Jazz\n");
        printf("2. Hip-Hop\n");
        printf("3. Pop\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);
        
        switch(choice)
        {
            case 1:
                system("mpv --no-video https://www.youtube.com/watch?v=GhAbBh3jwpg");
                break;
        
            case 2:
                //same as case 1
                break;
                
            case 3:
                //same as case 1
                break;
        
            case 4:
                printf("\n\n\t\t\tCoding is Fun !\n\n\n");
                exit(0);    // terminates the complete program execution
        }
    }
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

The above code works but with the following issues:

  1. if you enter 1 (or 2, 3, etc), the program displays the menu again then executes the statement
  2. once mpv finishes playing the audio the program does not return to the main menu

Those are the two issues I am having so far.

Please see the above statements.

1

There are 1 answers

2
Wyck On

I had a quick scan of the man page for mpv and I think setting --idle=once may be helpful. It makes the process exit once the first playlist has finished playing back.

--idle=<no|yes|once>

Makes mpv wait idly instead of quitting when there is no file to play. Mostly useful in input mode, where mpv can be controlled through input commands. (Default: no) once will only idle at start and let the player close once the first playlist has finished playing back.