On Tuesday, 16 February 2021 15:25:28 GMT Terry Coles wrote:
> So I'm kind of back where I started.

This morning I tried a variety of things to solve this; still to no avail.

I've attached two more code fragments to this message; these are copied from 
the latest version of the software on the Pis.  The file 
Minster_Bells_Software_Fragment.txt shows what I have on the Bells Pi to play 
Change Rings on demand.  Minster_Music_Software_Fragment2.txt shows the music 
player version of this; which exhibits this problem.

In order to get to the bottom of this I have:

1.  Reduced the number of MP3 files in the Playlist directory to 1.

2.  Re-written the code so that the line that uses glob to parse the files in 
the directory is replaced with the actual path and filename to the first file.

3.  Re-written the code to remove the while loop, to simplify things.

4.  Commented out the lines that invoked the mpg123 player and terminated it 
in the mp3_player_stop() function.

Items 1 to 3 made no difference whatsoever to the behaviour; I still got the 
freezing when I tried to restart the player/

With Item 4 the messages from the Webserver were responded to reliably but of 
course no music was played.

It would appear therefore that once the mpg123 player has been invoked and 
then terminated, invoking it a second time makes the overall program 
unresponsive.  This I cannot understand because the "Music Playing" message is 
being printed, so presumably the lock up occurs at the line containing:

  mp3_wait = mp3_player.wait()

I realise that the program will halt at that point until the mpg123 player 
terminates, but the commands coming in are in new threads so they should still 
be responded to.  (They have once.)

In any case, the Bells Pi code uses the same approach, so how come that works 
and the Music code doesn't?


                Terry Coles
def change_rings_start():                         # Play a file of change rings
    global c_player


    print ("Change rings stopped")

    c_file = 'Wimborne_Minster_StedmanCinques_16-10-2016.mp3' # Change rings.
    c_path = os.path.join(chr_subdir, c_file)
    blackhole = open(os.devnull, 'w')

    if c_player is None:
        c_player = subprocess.Popen(['mpg123', c_path], stdin=subprocess.PIPE, 
stdout=blackhole, stderr=blackhole)

    print ("Playing Changes - ", c_file, "to the Tower")

def change_rings_stop():                         # Play a file of change rings
    global c_player

    if c_player is None:

    c_wait = c_player.wait()
    if not c_wait:
        print ("Change Rings: mpg123 failed:%#x\n" % c_wait)
    c_player = None

    Status[12] = "Bells not Ringing"

def mp3_player_start():                          #  Setup and run the MP3 Player
    global playlists, playlist_index, mp3_player, Status

    playlist_index  = last_playlist_index   # stop_playback sets it ti zero.

    print("Entry into mp3_player_start()")
    print("Playlist Number = " +str(playlist_index))

    while playlist_index != 0:
        print ("Playlist Number = " + str(playlist_index))
        print ("Playlist Length = " + str(len(playlists)))

        Status[9] = str(playlist_index)                     # Current Playlist

        mp3_subdir = playlists[playlist_index]          # directory containing 
mp3 files for the current Playlist

        playlist = sorted(glob.glob(mp3_subdir + '/*.[Mm][Pp]3'))   # Create a 
list of files to be played in the current directory
        Status[10] = playlist

        mpg_list = ['mpg123']

        args = mpg_list + playlist

        print (args)

        if mp3_player == None:
            mp3_player = subprocess.Popen(args)

        print("Music Playing")

        mp3_wait = mp3_player.wait()

        if not mp3_wait:
            print('MP3 Player: mpg123 failed:%#x\n' % mp3_wait)

        if playlist_index == 0:
            print("Break from Playlist Loop")


def mp3_player_stop():                           #  Setup and run the MP3 Player
    global mp3_player, playlist_index, last_playlist_index, Status

    last_playlist_index = playlist_index

    playlist_index = 0                          # Not Playing Flag.

    if mp3_player is None:

    mp3_wait = mp3_player.wait()
    if not mp3_wait:
        print ('MP3 Player: mpg123 failed:%#x\n' % mp3_wait)
    mp3_player = None

    print("Music Stopped")

    Status[10] = "Not Playing"

  Next meeting: Online, Jitsi, Tuesday, 2021-03-02 20:00
  Check to whom you are replying
  Meetings, mailing list, IRC, ...  http://dorset.lug.org.uk
  New thread, don't hijack:  mailto:dorset@mailman.lug.org.uk

Reply via email to