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"