0

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.

Wyck
  • 10,311
  • 6
  • 39
  • 60
error404
  • 1
  • 2
  • A good starting point would be to add a `default` case that does something like print "Invalid selection". That way you will at least know if that's happening. – Tom Karzes Mar 19 '23 at 01:40
  • 1
    Regarding your problem, are you certain `mpv` isn't running asynchonously? In that case, the `system` call would return immediately, with `mpv` running in background. – Tom Karzes Mar 19 '23 at 01:41

1 Answers1

0

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.

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • Using `--idle=` with any of the three options does not change anything. – error404 Mar 19 '23 at 02:55
  • The [`system` library function](https://man7.org/linux/man-pages/man3/system.3.html) will wait until the process you launched has exited. This means that `mpv` is exiting before your video has completed playing. This isn't an issue with c or gcc. You could continue trying things by just typing your `mpv --no-video https://www.youtube.com/watch?v=GhAbBh3jwpg` command line with other options until you find the one that makes mpv refrain from exiting until your video has completed. – Wyck Mar 19 '23 at 22:56