omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Objcblock can get self argument

    Pythonista
    3
    4
    3105
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wolf71
      wolf71 last edited by wolf71

      The objc code :

                  [playerNode scheduleBuffer:buffer atTime:[AVAudioTime timeWithSampleTime:sampleTime atRate:sampleRate] options:AVAudioPlayerNodeBufferInterrupts completionHandler:^{
                      [playerNode stop];
                      [playerNode reset];
                      [self.audioEngine disconnectNodeOutput:playerNode];
                  }];
      
      

      pythonista code:

           def playa():
      
      	player.stop()     #  !!!! ERROR: can't found player 
              player.reset()   #
      
           c_playa = ObjCBlock(playa) 
      
         player.scheduleBuffer_atTime_options_completionHandler_(PCMBuff,None,0,c_playa)
      
      
      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        @wolf71 There is nothing special about playerNode here, it's just a normal variable. self is special in Objective-C because it's a hidden parameter of every method, but it works just like any other parameter. Blocks are a bit special in Objective-C because they can access variables in the function around them. Python functions already do this by default, so your playa function can access the playerNode variable from outside already. I don't see a self in your Python code, but if there is one, you can of course use that in the block too.

        wolf71 1 Reply Last reply Reply Quote 0
        • wolf71
          wolf71 @dgelessus last edited by

          @dgelessus sorry, the error info:

          Traceback (most recent call last):
          File "_ctypes/callbacks.c", line 314, in 'calling callback function'
          File "/private/var/mobile/Containers/Shared/AppGroup/A2331A94-5917-4220-83BB-A1CC1ED4E5A7/Pythonista3/Documents/Research/MCSrv.py", line 71, in playa
          print player.hash()
          UnboundLocalError: local variable 'player' referenced before assignment

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            The code you copied this from was in the context of another Object, like perhaps the application's main delegate object, etc. in your case, just thing of it as a place to store your audioengine,.. global is probably fine, depending on what you are doing.

            (in real objc, while blocks do have a hidden self parameter, I don't think you can normally access it without introspection... )

            your problem seems to be you have not created your AVAudioEngine or AVAudioPlayerNode.
            Your code is using those as globals, which is fine here, but those globals better exist!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors