CocoaMessage duration not working (SOLVED)

Mac OSX specific forum
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

CocoaMessage duration not working (SOLVED)

Post by codeit »

Hi was just wondering why this does not return anything can someone please shine a light on this for me.

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
Last edited by codeit on Thu Jul 20, 2017 9:53 am, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CocoaMessage duration not working

Post by wilbert »

Those two lines of code work fine when I try them.
It shows correctly the duration of the mp3.

Could there be anything wrong with your other code ?
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: CocoaMessage duration not working

Post by codeit »

I can't see anything strange here is my code so far

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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CocoaMessage duration not working

Post by wilbert »

When you add a file to the playlist, you are not opening it to get the duration as far as I can tell.
If I click play, the debug window outputs the correct duration on my computer.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: CocoaMessage duration not working

Post by codeit »

Can't understand it each file is opened when it is played
There is nothing about this on the net as i have searched and i can not sort it out.

Just noticed I am using PureBasic 5.60 (MacOS X - x64) wonder if that has anything to do with it?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CocoaMessage duration not working

Post by wilbert »

codeit wrote:Just noticed I am using PureBasic 5.60 (MacOS X - x64) wonder if that has anything to do with it?
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.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: CocoaMessage duration not working

Post by codeit »

No I don't get the duration at all it's as if CocoaMessage(@duration.d, mp3Object, "duration") is returning Nil all the time.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CocoaMessage duration not working

Post by wilbert »

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.
You could check if mp3Object is not 0.
The only other thing I can think about is to try a different mp3 file.
I'm using MacOS 10.12.5 by the way; don't know if that makes a difference.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: CocoaMessage duration not working

Post by codeit »

This is strange just wondered if there was a return value at all from CocoaMessage(@duration.d, mp3Object, "duration")
so i wrote it like this test = CocoaMessage(@duration.d, mp3Object, "duration") and in the Debug window it shows the duration :shock:

now i just need it to count down :mrgreen:
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: CocoaMessage duration not working

Post by fsw »

Loading a mp3 file doesn't show the duration in the list so I changed the code from this:

Code: Select all

            SetGadgetItemText(playlist, ListSize(mp3list()), FormatNumber(FileSize(mp3File$)) + " bytes", 1)
            SetGadgetItemText(playlist, ListSize(mp3list()), FormatDate("%hh:%ii:%ss", duration), 2)
to this:

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)
Also in order to read the passed time of the song while the song is played I used:

Code: Select all

        CocoaMessage(@current.d, mp3Object, "currentTime")
        Debug FormatDate("%hh:%ii:%ss", current)
It works for me.
Just code it...
[ small intentional pun :) ]

I am to provide the public with beneficial shocks.
Alfred Hitshock
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: CocoaMessage duration not working

Post by codeit »

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 :?

No sorry just found the problem duration was not defined as a double that's all
Thanks for your help brill ...
Last edited by codeit on Tue Jun 20, 2017 5:47 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CocoaMessage duration not working

Post by wilbert »

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 :?
Try @duration.d . It needs to be a double.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: CocoaMessage duration not working

Post by codeit »

I would like the elapsed time to be displayed in the playlist like this

Code: Select all

SetGadgetItemText(playlist, ListSize(mp3list()), FormatDate("%hh:%ii:%ss", current), 3)
But it wont show yet if i use Debug window it works fine :mrgreen:
Post Reply