Playlist Bugs (SOLVED)

Just starting out? Need help? Post your questions and find answers here.
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Playlist Bugs (SOLVED)

Post by codeit »

Hi guys
Have spent time trying to get my Mp3 Player to work right but just can't over come the bugs in it.
It should play all songs in the listview one after the other unless the user press the stop button.
but it does not work right, In my player you have to select the first song to play before you can play any
and when it is finished playing the highlighted song will change.

Code: Select all

Enumeration playState
  #stopped
  #paused
  #playing
EndEnumeration

Dim mp3list.s(0)
Global mp3Object
Global Count.i = -1
Global Index.i = 0

#FLAGS = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered

mainWin = OpenWindow(#PB_Any, 0, 0, 600, 400, "MP3 Player 0.1", #FLAGS)
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 = ListViewGadget(#PB_Any, 30, 10, 270, 325, #PB_ListView_MultiSelect)

  Procedure LoadMp3(File$)
    mp3Object = CocoaMessage(0, CocoaMessage(0, 0, "NSSound alloc"),
    "initWithContentsOfFile:$", @File$, "byReference:", #YES)
    CocoaMessage(0, mp3Object, "play")
  EndProcedure

  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
        If CocoaMessage(0, mp3Object, "isPlaying")
        Else
          If state = #playing And Index <= Count
            LoadMp3(mp3list(Index))
            SetGadgetState(playlist, Index)
          Else
            state = #stopped
            SetGadgetState(playlist, -1)
            DisableGadget(playBtn, #False) : DisableGadget(stopBtn, #True)
            DisableGadget(loadBtn, #False) : DisableGadget(pauseBtn, #True)
          EndIf
        EndIf      
      EndIf
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case loadBtn
          mp3File$ = OpenFileRequester("Select MP3 Track:", "", "MP3|*.mp3;", 0)
          If mp3Object
            CocoaMessage(0, mp3Object, "release")
            mp3Object = 0
          EndIf
          If mp3File$ <> ""
            AddGadgetItem(playlist, -1, GetFilePart(mp3File$))
            Count = (CountGadgetItems(playlist) -1)
            ReDim mp3list(Count)
            mp3list(Count) = mp3File$
          EndIf
          
        Case exitBtn
          Quit = #True
        Case stopBtn
          CocoaMessage(0, mp3Object, "stop")
          state = #stopped
          DisableGadget(stopBtn, #True)
          DisableGadget(loadBtn, #False)
          DisableGadget(pauseBtn, #True)
          DisableGadget(playBtn, #True)
        Case playBtn
          Select state
            Case #stopped
              state = #playing
              LoadMp3(mp3list(Index))
              DisableGadget(stopBtn, #False)
              DisableGadget(pauseBtn, #False)
          EndSelect
          
        Case pauseBtn
          Select state      
            Case #paused
              CocoaMessage(0, mp3Object, "resume")
              state = #playing
            Case #playing
              CocoaMessage(0, mp3Object, "pause")   
              state = #paused
          EndSelect
          
        Case playlist
          If Count > -1
            Select EventType()
              Case #PB_EventType_LeftClick
                Index = GetGadgetState(playlist)
                DisableGadget(playBtn, #False) : DisableGadget(stopBtn, #True)
                DisableGadget(loadBtn, #True) : DisableGadget(pauseBtn, #True)
            EndSelect
          EndIf
      EndSelect
  EndSelect
 
Until Quit

CocoaMessage(0, mp3Object, "stop")
CocoaMessage(0, mp3Object, "release")
If someone could look over this code and point out where I am going wrong that would be great.
Last edited by codeit on Thu Jul 20, 2017 9:54 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Playlist Bugs

Post by infratec »

Hi,

works on windows:

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")
  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, 600, 400, "MP3 Player 0.1", #FLAGS)
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, 270, 325, "Title", 250, #PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)

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
        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$))
              AddElement(mp3list())
              mp3list() = mp3File$
              If ListSize(mp3list()) = 1
                SetGadgetState(playlist, 0)
                DisableGadget(playBtn, #False)
                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 Count > -1
              Select EventType()
                Case #PB_EventType_LeftClick
                  Index = GetGadgetState(playlist)
                  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
Should work still on Mac OSX.

Bernd
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Playlist Bugs

Post by codeit »

Thank you all most a re write but there are a couple of new commands in there that I have never seen before
I will get to know them now and study your code to see what I was doing wrong so thanks again for speedy help

Regards
Dave
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Playlist Bugs

Post by infratec »

Hi,

I only replaced the array of mp3list() with a real list :mrgreen:
I think this makes more sense here.
I don't like to redim always the array.

But you don't tell if it works now (nearly) like you want it.

Since I don't own a Mac, I had to add the stuff for windows.

Bernd
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Playlist Bugs

Post by codeit »

It works fine just had to change a button disable to #True and that was it I would like to add more function like a progress bar as it is playing but I guess
That would have to be done with memory fiddling and I am not good at that the only other thing to hand is volume adjustment but rely needed here
Thanks for your time again.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Playlist Bugs

Post by wilbert »

codeit wrote:I would like to add more function like a progress bar as it is playing but I guess
That would have to be done with memory fiddling and I am not good at that the only other thing to hand is volume adjustment but rely needed here
The NSSound class has duration , currentTime and volume properties so on OSX it shouldn't be that hard to add those things.
If you need more advanced things like panning, volume meters or changing playback speed, you can use AVAudioPlayer instead of NSSound but that requires OSX 10.7 or higher.
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Playlist Bugs

Post by codeit »

I see so thinking about it then would the duration command look like this Result = CocoaMessage(0, mp3Object, "duration")
or is there more to it?

Thanks
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Playlist Bugs

Post by wilbert »

codeit wrote:I see so thinking about it then would the duration command look like this Result = CocoaMessage(0, mp3Object, "duration")
or is there more to it?
Both duration and currentTime are double precision values.
For that reason the approach is a bit different.

Code: Select all

CocoaMessage(@duration.d, mp3Object, "duration")
CocoaMessage(@currentTime.d, mp3Object, "currentTime")

Debug currentTime
Debug duration
Windows (x64)
Raspberry Pi OS (Arm64)
codeit
User
User
Posts: 62
Joined: Sat Apr 15, 2017 5:53 pm
Location: Leicestershire

Re: Playlist Bugs

Post by codeit »

I see any idea on the formula to display them and the best place for me to insert them say like the Timer event or something.

Thanks for all your help as I am enjoying this programming again.
Post Reply