Player won't stop playing file, with 'wait' seconds

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
smelly
Posts: 12
Joined: Sat Jan 09, 2021 11:36 pm

Player won't stop playing file, with 'wait' seconds

Post by smelly » Sun Jan 10, 2021 12:29 am

I'm creating an LC program which allows me to listen to a sequence of songs or podcasts in a folder, and when I hear an audio file playing that I wish to save as a favorite, I can click a button to have it moved into a separate folder of favorites. I usually know within the first 20 seconds of play whether I want to save it as a favorite.

The user can select the number of seconds to play before moving on to the next audio file. Occasionally, I want to skip the current song playing prior to chosen the chosen length of play and immediately begin playing the next song.

The problem I'm having with my LC scripts is that I cannot get it to skip to the next tune without playing the full chosen length. My 'stop' or 'skip' button is disabled by the player object unless I use the statement "wait gPlaySeconds seconds with messages". Although the "wait" statement does enable a button with stop or pause scripts, the audio file continues playing to the end of the chosen 'wait' length, such as 20 seconds, before going on to the next audio file.

Play button script, excerpt—
start player "tPlayer"
wait gPlaySeconds seconds with messages


Skip/Stop button script—
stop player "tPlayer"
exit to top

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5745
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Player won't stop playing file, with 'wait' seconds

Post by richmond62 » Sun Jan 10, 2021 10:21 am

Try

Code: Select all

without waiting

smelly
Posts: 12
Joined: Sat Jan 09, 2021 11:36 pm

Re: Player won't stop playing file, with 'wait' seconds

Post by smelly » Mon May 24, 2021 12:51 pm

Thanks, but "without waiting" only works with the move and send to program commands according to the LC Dictionary. In this case I'm dealing with an audio player.

smelly
Posts: 12
Joined: Sat Jan 09, 2021 11:36 pm

Re: Player won't stop playing file, with 'wait' seconds

Post by smelly » Mon May 24, 2021 2:37 pm

Simply put, is there a way to Stop or Pause and then Resume an audio player (not an audioclip) using a standard LC 'button'? It seems I must use the "wait x seconds with messages" to enable the stop/pause/play/resume buttons—otherwise the user interface buttons are deliberately frozen; it's not asynchronous. My LC program plays a list of audio files in a folder. With the "wait x seconds with messages" the audio continues playing until the x seconds have been reached, then continues with the next audio file in the folder. I cannot immediately stop the audio playing nor resume.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5745
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Player won't stop playing file, with 'wait' seconds

Post by richmond62 » Mon May 24, 2021 3:07 pm

Maybe I'm a bit stupid, but over "here" on Mac OS 11.5 beta 1 . . .
-
SShot 2021-05-24 at 17.04.46.png
-
Button "STOP":

Code: Select all

on mouseUp
   stop player "MOOZIK"
end mouseUp
Button "RESUME":

Code: Select all

on mouseUp
   start player "MOOZIK"
end mouseUp
Everything works rather nicely.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5745
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Player won't stop playing file, with 'wait' seconds

Post by richmond62 » Mon May 24, 2021 3:14 pm

apple_keyboard_vintage_A1048_02.jpeg
-
Of course if you want to control things via the keyboard it is time to start
getting into rawKeyUp . . .

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5745
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Player won't stop playing file, with 'wait' seconds

Post by richmond62 » Mon May 24, 2021 3:36 pm

SShot 2021-05-24 at 17.34.37.png
SShot 2021-05-24 at 17.34.37.png (11.03 KiB) Viewed 1194 times
-
On my (antiquated) Apple keyboard the volume control keys 'talk' directly to the Macintosh system
and LiveCode is unable to trap any key signals from them.
Attachments
KK.livecode.zip
Here's the stack.
(929 Bytes) Downloaded 24 times

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6036
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Player won't stop playing file, with 'wait' seconds

Post by jacque » Tue May 25, 2021 9:52 pm

smelly wrote:
Sun Jan 10, 2021 12:29 am
The problem I'm having with my LC scripts is that I cannot get it to skip to the next tune without playing the full chosen length. My 'stop' or 'skip' button is disabled by the player object unless I use the statement "wait gPlaySeconds seconds with messages". Although the "wait" statement does enable a button with stop or pause scripts, the audio file continues playing to the end of the chosen 'wait' length, such as 20 seconds, before going on to the next audio file.
Avoid "wait" and do this in the button script:

Code: Select all

on mouseUp
  start player tPlayer
  send "stopPlayer" to me in 20 seconds
end mouseUp

on stopPlayer
  stop player tPlayer
end stopPlayer
If your player really is named "tPlayer" then use the quotes as you had them originally. If it's a variable, then omit the quotes. These handlers will either stop the player after the given number of seconds, or do nothing if the user has already stopped it.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

smelly
Posts: 12
Joined: Sat Jan 09, 2021 11:36 pm

Re: Player won't stop playing file, with 'wait' seconds

Post by smelly » Mon May 31, 2021 5:45 pm

jacque: Thanks for the help. Although your sample code works well with a simple audio player which I coded using your example, it doesn't work with a far more complex stack like mine, where I play a sequence of tunes loaded into a field from a list of files on the hard drive, for a specified length, e.g. 10 seconds, but want to be able to stop, pause, continue to the next tune, et al.

When I begin playing the tunes all the buttons on the card are disabled until it has finished the 10 seconds of play and then automatically begins the next tune in a repeat loop. As I understand from the LC Dictionary this is normal unless code similar to "wait 10 seconds with messages" is used. With my stack the tune waits for the 10 seconds to complete and then goes on to play the next tune. I can stop the play using wait 10 seconds with messages because the stop button is then enabled, not blocked, but then I hear dead silence for the remainder of time until the 10 seconds has elapsed. If I set the length to 30 seconds and stop the play at 10 seconds I have to wait another 20 seconds to hear the next tune. I want to skip to the next tune immediately when I click the stop or continue or whatever button. I also want to use pause to actually pause the play and continue from where I paused by clicking another button. All that functionality seems to require the wait seconds with messages or else all the buttons on the card are disabled.

I guess it just boils down to me figuring out the proper algorithm, now that I can enable the card buttons by using that 'wait seconds with messages' command. Such are the limitations of using a programming language that has little or no concurrent (multithreaded) functionality.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6036
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Player won't stop playing file, with 'wait' seconds

Post by jacque » Mon May 31, 2021 6:30 pm

Using wait inside a repeat loop isn't efficient or reliable; sending a message is LC's way of emulating multithreading and is the best way to go. When you say the buttons are disabled, do you mean buttons you've placed on the card or do you mean the player controller is disabled? The latter shouldn't happen so I assume you're using card buttons. I wonder why you disable the buttons at all?

When I get back to my computer I'll give you a sample script. You can do what you need using the send command, it's just a modification of the short example I posted.

Meanwhile, let me know what buttons you have on the card and what they do.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6036
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Player won't stop playing file, with 'wait' seconds

Post by jacque » Mon May 31, 2021 8:50 pm

Here is a skeletal outline of the basics you described. I have four buttons: Play Next, Pause, Resume, Stop. I do not disable any of them. The primary working handler is in the card script:

Code: Select all

constant kPlaySecs = 10 -- default number of seconds to play

on playNext
  stop player 1
  put line lineoffset(the filename of player 1,fld "playList") + 1 of fld "playList" into tNextFile
  if tNextFile = "" then exit playNext
  set the filename of player 1 to tNextFile
  send "playNext" to me in kPlaySecs seconds
  start player 1
end playNext

on playStopped
  if the currentTime of player 1 = the duration of player 1 then -- song has ended
    playNext
  end if
end playStopped
The four buttons have these simple scripts:

Code: Select all

Button Play Next: 
on mouseUp
 playNext
end mouseUp

Button Pause: 
on mouseUp
  stop player 1
end mouseUp

Button Resume: 
on mouseUp
  start player 1
end mouseUp

Button Stop (stops all automatic playback): 
on mouseUp
  stop player 1
  repeat for each line l in the pendingMessages -- remove any pending messages that would trigger the next song
    if l contains "playNext" then cancel item 1 of l
  end repeat
  set the filename of player 1 to empty
end mouseUp
If you want to be able to click on a line in the list to play a particular song, put this in the list field:

Code: Select all

on mouseUp
  set the filename of player 1 to the text of the clickline
  start player 1
end mouseUp
This doesn't trigger the "Play Next" handler so if you want that, you'll either need to click the Play Next button, or let the song play through to the end, or revise the script.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

PaulDaMacMan
Posts: 446
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Player won't stop playing file, with 'wait' seconds

Post by PaulDaMacMan » Wed Jun 16, 2021 1:52 am

You're probably going to want to flush the pendingMessages queue so none of the callback check loop messages wind up overlapping.
Should take look at flushEvents() in the LC dictionary too, particularly like if using keyDown / arrowKeys to jump between playback tracks.
My LCB Repos: https://github.com/PaulMcClernan/
My LiveCode YouTube Videos: PlayList

Post Reply

Return to “Multimedia”