Page 1 of 1

mp3 Music Player

Posted: Sun Oct 10, 2021 12:56 am
by chris319
Here is a player which plays music files as movies. I have tested it with mp3 and wav files. It may also play other file formats which I have not tested.

PureBasic will not play mp3 files natively using the Sound library, so they must be played as movies instead. The program waits until the song (movie) has finished playing before it exits. Special thanks to Rashad for the code that calculates the duration of the movie.

Code: Select all

InitMovie()
filename$ = "mySong.mp3"
;filename$ = "mySong.wav"
LoadMovie(1,filename$)

If IsMovie(1) <> 0: Debug "Is movie"
  mciSendString_("OPEN "+Chr(34)+Filename$+Chr(34)+" Type MPEGVideo ALIAS "+Str(0),0,0,0)
  Length$ = Space(#MAX_PATH)
  mciSendString_("Status 0 length",@Length$,#MAX_PATH,0)
  Duration.q = ValD(length$)
  ms = Duration % 1000
  S = Int(Duration / 1000) : While S > 59:S-60:Wend 
  M = Int(Duration / 1000 / 60) : While M > 59:M-60:Wend 
  H = Int(Duration / 1000 / 60 / 60) : While H > 59:H-60:Wend
  Duration$ =RSet(StrU(H,#PB_Quad),2,"0")+":"+RSet(StrU(M,#PB_Quad),2, "0")+":"+RSet(StrU(S,#PB_Quad),2,"0")+":"+RSet(StrU(ms, #PB_Quad),3,"0")
  Debug Duration$
  mciSendString_("close all ",0,0,0)
  Debug Str(H) + "  Hours"
  Debug Str(M) + "  Minutes"
  Debug Str(S) + "  Seconds"
  Debug Str(ms) + "  milliseconds"
  songLength = (((M * 60) + S) * 1000) + ms
  Debug Str(songLength) + "  milliseconds"
  Debug Str(songLength / 1000) + "  seconds"
Else
  MessageRequester("ERROR","Selected file is not a movie.")
End
EndIf

OpenWindow(1,0,0,300,225,"")
PlayMovie(1,1)
Delay(songLength)
Debug "Done"

Re: mp3 Music Player

Posted: Sun Oct 10, 2021 12:45 pm
by chris319
It turns out that the above technique returns inaccurate durations. The durations are a few seconds longer than actual.

Below is an alternate method of playing an mp3 all the way through:

Code: Select all

InitMovie()
filename$ = "mySong.mp3"
;filename$ = "mySong.wav"
LoadMovie(1,filename$)

If IsMovie(1) <> 0
  Debug "Is movie"
Else
  MessageRequester("ERROR","Selected file is not a movie.")
  End
EndIf

 OpenWindow(1,0,0,300,225,"")
 PlayMovie(1,1)
 Repeat:Until MovieStatus(1) > 0 ;WAIT FOR MOVIE TO START PLAYING
 While MovieStatus(1) > 0: Wend ;WAIT FOR MOVIE TO FINISH PLAYING
 StopMovie(1)

Debug "Done"

Re: mp3 Music Player

Posted: Sun Oct 10, 2021 4:22 pm
by HeX0R
Your alternative method burns one of the CPU cores, you should add a delay at least.

Re: mp3 Music Player

Posted: Mon Oct 11, 2021 4:09 am
by chris319
HeX0R wrote: Sun Oct 10, 2021 4:22 pm Your alternative method burns one of the CPU cores, you should add a delay at least.
Like so?

Code: Select all

 While MovieStatus(1) > 0: Delay (500): Wend ;WAIT FOR MOVIE TO FINISH PLAYING

Re: mp3 Music Player

Posted: Mon Oct 11, 2021 7:27 am
by Caronte3D
On this example doesn't matter because the loop haven't nothing more, but ever is better to wait only if not events.

Somethin like:

Code: Select all

While MovieStatus(1) > 0  ;WAIT FOR MOVIE TO FINISH PLAYING
  if Not WindowEvent() : Delay(1) : EndIf
Wend