MP3 files (cross-platform) - getting correct length / status

Just starting out? Need help? Post your questions and find answers here.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

MP3 files (cross-platform) - getting correct length / status

Post by Andre »

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:

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 
Thank you very much! :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: MP3 files (cross-platform) - getting correct length / st

Post by JHPJHP »

Removed; post ignored.
Last edited by JHPJHP on Tue Jan 28, 2020 11:24 pm, edited 3 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: MP3 files (cross-platform) - getting correct length / st

Post by wilbert »

Andre wrote: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...?)
On macOS you can only use the Movie library with macOS 10.14 or below.
On macOS 10.15 it is useless.
viewtopic.php?f=24&t=73941
Windows (x64)
Raspberry Pi OS (Arm64)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: MP3 files (cross-platform) - getting correct length / st

Post by RASHAD »

For Windows

Code: Select all

Filename$ = OpenFileRequester("","","ALL|*.*;*.mid|Wave|*.wav|mp3|*.mp3| OGG|*.OGG|MID|*.MID",0)
If Filename$
  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$)
  Debug Duration
  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")
  mciSendString_("close all ",0,0,0)
  Debug Duration$
 EndIf
Egypt my love
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: MP3 files (cross-platform) - getting correct length / st

Post by JHPJHP »

Removed; post ignored.
Last edited by JHPJHP on Tue Jan 28, 2020 11:24 pm, edited 2 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: MP3 files (cross-platform) - getting correct length / st

Post by Andre »

Hi guys,

thank you very much for your tipps and example codes for getting the MP3 length etc.

What wilbert wrote about the Movie library on newer MacOS versions seems to be a "No go" for further using the Movie lib (as I need it working on Windows and MacOS for my project).

I just tested converting MP3 files using 'VLC' to Ogg-Vorbis (.ogg) format. For this format the Sound library provide all functions for playing, stopping, pausing & resuming, getting length / position etc.
And the simple playing of .ogg files (background sound and multiple sound effects) I've already working on Windows and MacOS.

So I think this seems the way to go :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply