Page 1 of 1

Music Player 2

Posted: Sat Mar 19, 2016 9:56 pm
by Ramzan
This is the second version of my music player. I was going to give up but have decided to persevere. Thanks to everyone for their help. I want to create a music player that will look the same on EVERY iPhone and iPad regardless of screen resolution. Hopefully this updated code will work.

Code: Select all

SET UNDERGROUND ON
SET ORIENTATION PORTRAIT
SET TOOLBAR OFF
SET OUTPUT BACK COLOR 0,0,0
SET OUTPUT FONT SIZE 32
SET OUTPUT FONT COLOR 0,0,1
SET BUTTONS FONT SIZE 24
DRAW COLOR 1,1,0
FILL COLOR 0,0,0
FILL AlPHA 0
SET BUTTONS CUSTOM
p=0
sw= SCREEN_WIDTH()
sh=SCREEN_HEIGHT()

PRINT "MUSIC"

BUTTON "1" TEXT "1" AT 0.24 * sw, 0.16 * sh
BUTTON "2" TEXT "2" AT 0.49 * sw, 0.16 * sh
BUTTON "3" TEXT "3" AT 0.74 * sw, 0.16 * sh
BUTTON "4" TEXT "4" AT 0.24 * sw, 0.32 * sh
BUTTON "5" TEXT "5" AT 0.49 * sw, 0.32 * sh
BUTTON "6" TEXT "6" AT 0.74 * sw, 0.32 * sh
BUTTON "7" TEXT "7" AT 0.24 * sw, 0.48 * sh
BUTTON "8" TEXT "8" AT 0.49 * sw, 0.48 * sh
BUTTON "9" TEXT "9" AT 0.74 * sw, 0.48 * sh
BUTTON "10" TEXT "10" AT 0.24 * sw, 0.64 * sh
BUTTON "11" TEXT "11" AT 0.49 * sw, 0.64 * sh
BUTTON "12" TEXT "12" AT 0.74 * sw, 0.64 * sh
BUTTON "13" TEXT "Stop" AT 0.47 * sw, 0.8 * sh


LOOP:

IF BUTTON_PRESSED("1") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("2") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("3") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("4") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("5") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("6") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("7") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("8") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("9") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("10") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("11") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("12") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
MUSIC 1 LOAD "files/musictrack1.wav"
MUSIC 1 PLAY
p=1
END IF

IF BUTTON_PRESSED("13") THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
p=0
END IF

GOTO LOOP

Re: Music Player 2

Posted: Sun Mar 20, 2016 8:01 am
by Henko
Hi, i did not really test your program, if it plays music, but i noticed some things:
1. Each of the 12 buttons load the same nr "1" musicpiece.
2. You use 12 times a piece of code which differs only by a very small amount. You should use a for next loop there.

The following code is essentially your program, but much, much smaller, and at the same time much more flexible. Try changing the values of the number of buttons (the variables nhor and nver as indicated in the comments). They are now 3 and 4, resulting in the original app.

My (last) advice to you is: try out all instructions of sB in simple programs and learn the language very well before taking on a large "appstore" project. If you continue that right now, you will certainly "give up" soon a second time.

Code: Select all

SET UNDERGROUND ON ! SET ORIENTATION PORTRAIT ! SET TOOLBAR OFF
SET OUTPUT BACK COLOR 0,0,0
SET OUTPUT FONT SIZE 32 ! SET OUTPUT FONT COLOR 0,0,1
SET BUTTONS FONT SIZE 24
DRAW COLOR 1,1,0 ! FILL COLOR 0,0,0 ! FILL AlPHA 0
SET BUTTONS CUSTOM
sw= SCREEN_WIDTH() ! sh=SCREEN_HEIGHT()

nhor=3                     ' horizontal number of buttons
nver=4                     ' vertical number of button
dx=.72/nhor ! dy=.64/nver

PRINT "MUSIC"

for y=1 to nver
  for x=1 to nhor
    but$=nhor*(y-1)+x
    button but$ text but$ at dx*x*sw,dy*y*sh
    next x
  next y

BUTTON nhor*nver+1 TEXT "Stop" AT 0.47 * sw, 0.8 * sh

LOOP:

for but=1 to nhor*nver
  but$=but ! song$="files/musictrack" & but$ & ".wav"
  IF BUTTON_PRESSED(but$) THEN 
    IF p=1 THEN
      MUSIC 1 STOP
      MUSIC 1 DELETE
      END IF
    MUSIC 1 LOAD song$
    MUSIC 1 PLAY
    p=1
    END IF
  next but

but$=nhor*nver+1
IF BUTTON_PRESSED(but$) THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
p=0
END IF

GOTO LOOP

Re: Music Player 2

Posted: Sun Mar 20, 2016 4:59 pm
by rbytes
Henko has really improved and tightened up the coding.
Here are my suggestions. I concentrated more on the layout and interface.

1. Changed to GRAPHICS Mode, where more design features are available.
2. Move title to center, enlarged and surrounded by a rectangle to make it stand out more.
2a. Changed title to match your name for the program. You might even come up with a catchier name.
3. Spread buttons out vertically to make use of full vertical area.
4. To fine-tune button positions, I added offx and offy variables
5. Users expect buttons to look like buttons. I restored the button shape, while keeping your preferred color.
5a. I enlarged the buttons to give users a better target area when pressing them.
6. There was no way to quit the program. I added a quit button that looks like the smart BASIC quit button
7. Temporarily commented out your song$ definition and added one for testing
8. Added a header showing you as the author, what the program does, and a version number.
9. Added comments throughout to make it easier to understand the operation.
9. Supplied a test file so other forum members can test your program.

Code: Select all

/*
Music Player v1.4
by Ramzan
March 2016
plays any 12 songs
*/

SET UNDERGROUND ON ! SET ORIENTATION PORTRAIT
SET TOOLBAR OFF ! OPTION TEXT POS CENTRAL
GRAPHICS
GRAPHICS CLEAR .5,.5,.5
GOSUB init

LOOP:

'check music buttons
FOR but=1 TO nhor*nver
  but$=but
  song$="Cartoon-bird.mp3"      'for testing only
  'song$="files/musictrack" & but$ & ".wav"
  IF BUTTON_PRESSED(but$) THEN 
    IF p=1 THEN
      MUSIC 1 STOP
      MUSIC 1 DELETE
      END IF
    MUSIC 1 LOAD song$
    MUSIC 1 PLAY
    p=1
    END IF
  NEXT but

'stop button pressed
but$=nhor*nver+1
IF BUTTON_PRESSED(but$) THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
p=0
END IF

'quit button pressed
IF BUTTON_PRESSED("quit") THEN END

GOTO LOOP

init:
DRAW FONT SIZE 40
SET BUTTONS FONT SIZE 24
DRAW COLOR 1,1,0 ! FILL COLOR .5,.5,.5 ! FILL ALPHA 1
SET BUTTONS CUSTOM
sw= SCREEN_WIDTH() ! sh=SCREEN_HEIGHT()
nhor=3                     ' horizontal number of buttons
nver=4                     ' vertical number of button
DX=.72/nhor ! DY=.56/nver ! offx=12 ! offy=140 
DRAW TEXT "MUSIC PLAYER" AT sw/2,130
DRAW RECT sw/2,130 SIZE 160,30

'create buttons
FOR y=1 TO nver
  FOR x=1 TO nhor
    but$=nhor*(y-1)+x
    BUTTON but$ TEXT but$ AT DX*x*sw-offx,DY*y*sh+offy SIZE 60,40
    NEXT x
  NEXT y

BUTTON nhor*nver+1 TEXT "Stop" AT 0.48 * sw-offx, 0.84 * sh SIZE 60,40

BUTTON "quit" TEXT "X" AT sw-50,10 SIZE 40,40

RETURN

Re: Music Player 2

Posted: Sun Mar 20, 2016 8:05 pm
by Henko
Uhh, Ricardo,
Using a absolute y-offset of +140, did you check if the program still fits on every iPhone screen? (Just asking ;) )

Re: Music Player 2

Posted: Sun Mar 20, 2016 8:08 pm
by rbytes
Yes. This looks pretty good on both my iPad and iPhone. It uses ratiox and ratioy variables to adjust the font sizes and offsets.
iPhone version is shown below (in a large frame so it stays at correct relative size to the iPad image above)

I should also point out that there is much more that could be done to give this program a professional look. I suggest, Ramzan, that you look at other music players available on the App Store to give you some ideas. You could put an image filling the screen behind and use Sprites with button images instead of sB buttons, just for starters.

Code: Select all

/*
Music Player v1.4
by Ramzan
March 2016
plays any 12 songs
*/

SET UNDERGROUND ON ! SET ORIENTATION PORTRAIT
SET TOOLBAR OFF ! OPTION TEXT POS CENTRAL
GRAPHICS
GRAPHICS CLEAR .5,.5,.5
GOSUB init

LOOP:

'check music buttons
FOR but=1 TO nhor*nver
  but$=but
  song$="Cartoon-bird.mp3"      'for testing only
  'song$="files/musictrack" & but$ & ".wav"
  IF BUTTON_PRESSED(but$) THEN 
    IF p=1 THEN
      MUSIC 1 STOP
      MUSIC 1 DELETE
      END IF
    MUSIC 1 LOAD song$
    MUSIC 1 PLAY
    p=1
    END IF
  NEXT but

'stop button pressed
but$=nhor*nver+1
IF BUTTON_PRESSED(but$) THEN 
IF p=1 THEN
MUSIC 1 STOP
MUSIC 1 DELETE
END IF
p=0
END IF

'quit button pressed
IF BUTTON_PRESSED("quit") THEN END

GOTO LOOP

init:
sw= SCREEN_WIDTH() ! sh=SCREEN_HEIGHT()
dev$=DEVICE_TYPE$ ()
'correction factor
IF dev$="iPad" THEN corr=25 ELSE corr=8
ratiox=sw/768 ! ratioy=sh/1024
DRAW FONT SIZE 50*ratiox
SET BUTTONS FONT SIZE 40*ratiox
DRAW COLOR 1,1,0 ! FILL COLOR .5,.5,.5 ! FILL ALPHA 1
SET BUTTONS CUSTOM
nhor=3                     ' horizontal number of buttons
nver=4                     ' vertical number of button
DX=.72/nhor ! DY=.56/nver ! offx=12*ratiox ! offy=90*ratioy
DRAW TEXT "MUSIC PLAYER" AT sw/2,100*ratioy
DRAW RECT sw/2,100*ratioy SIZE 200*ratiox,30*ratioy

'create buttons
FOR y=1 TO nver
  FOR x=1 TO nhor
    but$=nhor*(y-1)+x
    BUTTON but$ TEXT but$ AT DX*x*sw-offx-corr,DY*y*sh+offy SIZE 100*ratiox,70*ratioy
    NEXT x
  NEXT y

BUTTON nhor*nver+1 TEXT "Stop" AT 0.472 * sw-offx-corr, 0.8 * sh SIZE 120*ratiox,70*ratioy

BUTTON "quit" TEXT "X" AT sw-90*ratiox,10*ratioy SIZE 80*ratiox,70*ratioy

RETURN