I have this in my main loop whilst an MP3 is playing.
Code: Select all
CocoaMessage(@duration.d, mp3Object, "duration")
Debug FormatDate("%hh:%ii:%ss", duration)
But I just get 00:00:00 as a result.
Thanks
Code: Select all
CocoaMessage(@duration.d, mp3Object, "duration")
Debug FormatDate("%hh:%ii:%ss", duration)
Code: Select all
Enumeration playState
#stopped
#paused
#playing
EndEnumeration
NewList mp3list.s()
Global mp3Object
Procedure LoadMp3(File$)
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
mp3Object = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"), "initWithContentsOfFile:$", @File$, "byReference:", #YES)
CocoaMessage(0, mp3Object, "play")
; CocoaMessage(@currentTime.d, mp3Object, "currentTime")
CompilerElse
mp3Object = LoadSound(#PB_Any, File$)
PlaySound(mp3Object)
CompilerEndIf
EndProcedure
#FLAGS = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
InitSound()
CompilerEndIf
mainWin = OpenWindow(#PB_Any, 0, 0, 580, 380, "MP3 Player 0.1", #FLAGS)
SetWindowColor(mainWin, RGB(0,83,146))
AddWindowTimer(mainWin, 0, 1000)
loadBtn = ButtonGadget(#PB_Any, 20, 350, 100, 25, "Add")
stopBtn = ButtonGadget(#PB_Any, 130, 350, 100, 25, "Stop")
playBtn = ButtonGadget(#PB_Any, 240, 350, 100, 25, "Play")
pauseBtn = ButtonGadget(#PB_Any, 350, 350, 100, 25, "Pause")
exitBtn = ButtonGadget(#PB_Any, 460, 350, 100, 25, "Exit")
playlist = ListIconGadget(#PB_Any, 30, 10, 520, 300, "Song Title", 150, #PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(playlist, 1, "Size", 130)
AddGadgetColumn(playlist, 2, "Duration", 130)
;DisableGadget(playBtn, #True)
;DisableGadget(stopBtn, #True)
;DisableGadget(pauseBtn, #True)
; Start of main loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Timer
If mp3Object
CocoaMessage(@duration.d, mp3Object, "duration")
Debug FormatDate("%hh:%ii:%ss", duration)
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
If Not CocoaMessage(0, mp3Object, "isPlaying")
CompilerElse
If Not SoundStatus(mp3Object) = #PB_Sound_Playing
CompilerEndIf
If state = #playing
If Not NextElement(mp3list())
FirstElement(mp3list())
EndIf
LoadMp3(mp3list())
SetGadgetState(playlist, ListIndex(mp3list()))
EndIf
EndIf
EndIf
Case #PB_Event_Gadget
Select EventGadget()
Case loadBtn
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
mp3File$ = OpenFileRequester("Select MP3 Track:", "", "MP3|*.mp3", 0)
CompilerElse
mp3File$ = OpenFileRequester("Select WAV Track:", "", "WAV|*.wav", 0)
CompilerEndIf
If mp3File$ <> ""
AddGadgetItem(playlist, -1, GetFilePart(mp3File$, #PB_FileSystem_NoExtension))
SetGadgetItemText(playlist, ListSize(mp3list()), FormatNumber(FileSize(mp3File$)) + " bytes", 1)
SetGadgetItemText(playlist, ListSize(mp3list()), FormatDate("%hh:%ii:%ss", duration), 2)
AddElement(mp3list())
mp3list() = mp3File$
If ListSize(mp3list()) = 1
SetGadgetState(playlist, 0)
;DisableGadget(playBtn, #True)
;DisableGadget(stopBtn, #True)
;DisableGadget(pauseBtn, #True)
EndIf
EndIf
Case exitBtn
Quit = #True
Case stopBtn
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CocoaMessage(0, mp3Object, "stop")
CompilerElse
StopSound(mp3Object)
CompilerEndIf
state = #stopped
;DisableGadget(stopBtn, #True)
;DisableGadget(loadBtn, #False)
;DisableGadget(pauseBtn, #True)
;DisableGadget(playBtn, #False)
Case playBtn
Select state
Case #stopped
state = #playing
SelectElement(mp3list(), GetGadgetState(playlist))
LoadMp3(mp3list())
;DisableGadget(stopBtn, #False)
;DisableGadget(pauseBtn, #False)
;DisableGadget(playBtn, #True)
EndSelect
Case pauseBtn
Select state
Case #paused
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CocoaMessage(0, mp3Object, "resume")
CompilerElse
ResumeSound(mp3Object)
CompilerEndIf
state = #playing
Case #playing
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CocoaMessage(0, mp3Object, "pause")
CompilerElse
PauseSound(mp3Object)
CompilerEndIf
state = #paused
EndSelect
Case playlist
If CountGadgetItems(playlist) > 0
Select EventType()
Case #PB_EventType_LeftClick
;DisableGadget(playBtn, #False) : DisableGadget(stopBtn, #True)
;DisableGadget(loadBtn, #True) : DisableGadget(pauseBtn, #True)
EndSelect
EndIf
EndSelect
EndSelect
Until Quit
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CocoaMessage(0, mp3Object, "stop")
CocoaMessage(0, mp3Object, "release")
CompilerElse
If IsSound(mp3Object)
If SoundStatus(mp3Object, #PB_Sound_Playing)
StopSound(mp3Object)
EndIf
FreeSound(mp3Object)
EndIf
CompilerEndIf
The debug window shows the duration while the file plays (PB 5.60 x64, both mp3 and wav) but the list inside the window doesn't show the correct duration if that is what you mean.codeit wrote:Just noticed I am using PureBasic 5.60 (MacOS X - x64) wonder if that has anything to do with it?
You could check if mp3Object is not 0.codeit wrote:No I don't get the duration at all it's as if CocoaMessage(@duration.d, mp3Object, "duration") is returning Nil all the time.
Code: Select all
SetGadgetItemText(playlist, ListSize(mp3list()), FormatNumber(FileSize(mp3File$)) + " bytes", 1)
SetGadgetItemText(playlist, ListSize(mp3list()), FormatDate("%hh:%ii:%ss", duration), 2)
Code: Select all
SetGadgetItemText(playlist, ListSize(mp3list()), FormatNumber(FileSize(mp3File$)) + " bytes", 1)
; get the duration time and release
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
mp3Obj = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"), "initWithContentsOfFile:$", @mp3File$, "byReference:", #YES)
CocoaMessage(@duration, mp3Obj, "duration")
CocoaMessage(0, mp3Obj, "release")
CompilerEndIf
; now we publish the duration in the list
SetGadgetItemText(playlist, ListSize(mp3list()), FormatDate("%hh:%ii:%ss", duration), 2)
Code: Select all
CocoaMessage(@current.d, mp3Object, "currentTime")
Debug FormatDate("%hh:%ii:%ss", current)
Try @duration.d . It needs to be a double.codeit wrote:Thanks for that but it's returning the wrong duration as i have an mp3 that is 00:03:16 and is showing 11:32:50
Code: Select all
SetGadgetItemText(playlist, ListSize(mp3list()), FormatDate("%hh:%ii:%ss", current), 3)