Play Music continued...

Post Reply
Tffsnyder
Posts: 7
Joined: Mon Nov 27, 2017 8:23 pm
My devices: iPad pro
Flag: United States of America

Play Music continued...

Post by Tffsnyder »

This is a continuation of the post I put into programs by mistake.

I have made corrections and I am now running the following program which no longer returns an error message but there is no sound.

BEEP ' to see if beep comes through speaker

M$ = "tune"
MUSIC M$ LOAD "Personk.wav"

MUSIC M$ VOLUME 1
MUSIC M$ PLAY

PRINT "Music playing?"; MUSIC_PLAYING (M$)
PRINT "Music length?"; MUSIC_LENGTH (M$)

-------- and this is the resulting output (with no music but with a beep) ————-

Music playing? 1
Music length? 8.54932

Thanks in advance.

Tom

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Play Music continued...

Post by rbytes »

Your program ends too soon for the sound to be heard. Try adding a PAUSE 10 at the end of your code, which should give it time to play the complete sound. Or you could make the pause line PAUSE MUSIC_LENGTH (M$) or PAUSE 8.54932, since that is the length of the sound.

Another little trick is to add a wait loop at the end. "wait" is not a special command, just a label name. But with it and the following two lines, you create a closed loop that isn't broken until you touch the screen. When you do, tx then changes to the x coordinate of the point you touched. Since tx is no longer -1, the loop is broken and your program ends. SLOWDOWN slows down the loop execution so it doesn't run the processor at full speed.

wait: SLOWDOWN
GET TOUCH 0 as tx,ty
IF tx=-1 then wait
END
The only thing that gets me down is gravity...

Tffsnyder
Posts: 7
Joined: Mon Nov 27, 2017 8:23 pm
My devices: iPad pro
Flag: United States of America

Re: Play Music continued...

Post by Tffsnyder »

Brilliant. I couldn't be more grateful. Of course the program needs to stay on!

I am working with developers at Harvard School of Ed on prototyping some exciting news designs. We are finding Smart Basic to be the only useful tool for quick experiments.

Tom

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Play Music continued...

Post by rbytes »

It is quite amazing what can be programmed with it. Too bad Apple doesn't value this developer as much as we who have discovered the power of Smart Basic.

Good luck with your projects. Let me know if I can be of further help.
The only thing that gets me down is gravity...

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Play Music continued...

Post by GeorgeMcGinn »

Tom, rbytes,

You could also use the statement: MUSIC_PLAYING (M$)

You can use rbytes loop, but instead of on even in addition to, add:
IF MUSIC_PLAYING(M$) THEN wait

MUSIC_PLAYING returns 1 if musical composition [m$] is currently playing. Otherwise returns 0. So all you need is to test if true, or not = 0.

So the loop would look something like this:
wait: SLOWDOWN
GET TOUCH 0 as tx,ty
IF tx=-1 AND MUSIC_PLAYING(M$) THEN wait
END
It looks like if you touch the screen, that stops the music file from playing, So you need to make sure both test true to go back to the top of your loop. There may be a more elegant way of coding this, but I think you get the message. If either or both a touch or the music ends will exit the loop.

However, if the MUSIC is anything like the SAY statement, I would add a MUSIC_STOP(M$) before the END statement, just to make sure the song actually stops playing. On rare occasions my weather app will continue saying the forecast, even if I put in a PAUSE or the program ends.

Hope this helps,
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Play Music continued...

Post by rbytes »

That is a big improvement, because it lets you end the music at any time. If you used a pause the length of the song, you couldn't do that. Good call, George.
The only thing that gets me down is gravity...

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Play Music continued...

Post by matt7 »

Haven't been able to do much coding with Smart Basic in several months, but I check in on this forum from time to time.

I will often have a debug exit condition similar to the code above except I like to use TOUCH 1 (instead of TOUCH 0). This means you must touch the screen with two fingers to trigger a debug exit, which helps if you need to touch the screen for normal interaction with the program. My main program loop would look something like this:

Code: Select all

' setup code

menuExit = 0
debugExit = 0
DO

  ' processing
  
  IF TOUCH_X(1) > -1 THEN debugExit = 1
  
UNTIL menuExit OR debugExit

' tear down code
I think this keeps program flow very readable and allows anyone (mainly me in the future!) to quickly look at the code and figure out what is going on.

But for a simple situation like the original question about playing music, I would probably just do this:

Code: Select all

DO
  SLOWDOWN
UNTIL MUSIC_PLAYING(M$) = 0 OR TOUCH_X(1) > -1

Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

M$ = tune.

Post by Ken »

I got these suggested lines to work.

M$ = "tune"

MUSIC m$ LOAD "Bass Drum 1.wav"
MUSIC m$ PLAY

But then I found all I needed was this

MUSIC 1 LOAD "Bass Drum 1.wav"
MUSIC 1 PLAY

Post Reply