MP3 files (cross-platform) - getting correct length / status
Posted: Mon Jan 27, 2020 10:30 pm
Hi,
I've already read several topics on this forum (sometimes >10 years), but didn't found a (simple) solution without using external libs or API...
and like the manual states there could be problems when using Movie commands for playing MP3 files like the anthems from here: http://www.nationalanthems.info.
While playing, stopping, resuming and even jumping by MovieSeek() seems to work fine, it looks like MoveLength() is always giving non-valid results... but the MP3 length I need to calculate simple jumping inside the MP3 file (e.g. 10% of the length back and forward).
Do you have an idea / simple solution, which works on Windows and MacOS too?
(maybe using OS API code only for getting the MP3 length, and using PB's movie lib for the rest...?)
Here is a test code (adapted from the Movie.pb example in PB's help) with some comments:
Thank you very much! 
I've already read several topics on this forum (sometimes >10 years), but didn't found a (simple) solution without using external libs or API...

While playing, stopping, resuming and even jumping by MovieSeek() seems to work fine, it looks like MoveLength() is always giving non-valid results... but the MP3 length I need to calculate simple jumping inside the MP3 file (e.g. 10% of the length back and forward).
Do you have an idea / simple solution, which works on Windows and MacOS too?
(maybe using OS API code only for getting the MP3 length, and using PB's movie lib for the rest...?)
Here is a test code (adapted from the Movie.pb example in PB's help) with some comments:
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Movie example file
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
Procedure Min(Value1, Value2)
If Value1 < Value2
ProcedureReturn Value2
Else
ProcedureReturn Value1
EndIf
EndProcedure
If InitMovie() = 0
MessageRequester("Error", "Can't initialize movie playback !", 0)
End
EndIf
MovieName$ = OpenFileRequester("Choose the movie to play", "", "MP3 files|*.mp3;*.mpg|All Files|*.*", 0)
If MovieName$
If LoadMovie(0, MovieName$)
Debug "MovieInfo (Frames per sec) = " + MovieInfo(0, 0) ; FIXME: MovieInfo() always return a negative/non-valid number, when trying to play a MP3 file...!?
OpenWindow(0, 100, 150, Min(220, MovieWidth(0)), MovieHeight(0)+60, "PureBasic - MP3 playing")
StringGadget(0, 10, WindowHeight(0)-50, WindowWidth(0)-20, 20, "") ; for printing the movie/MP3 length
StringGadget(1, 10, WindowHeight(0)-27, WindowWidth(0)-20, 20, "") ; for printing the movie/MP3 position
PlayMovie(0, WindowID(0))
SetGadgetText(0, "MovieLength (sec) = " + StrF(MovieLength(0)/10000000, 1)) ; FIXME: MovieLength() always return a negative/non-valid number, when trying to play a MP3 file...!?
Repeat
SetGadgetText(1, "MovieStatus (sec) = " + StrF(MovieStatus(0)/10000000, 1)) ; TODO: MovieStatus() seems to return a 10-million of a second => works, at least on Win10 here...
Until WaitWindowEvent(1000) = #PB_Event_CloseWindow
Else
MessageRequester("Error", "Can't load the movie...", 0)
EndIf
EndIf
