PyObjC AVKit Framework
PyObjC is a bridge between Python and Objective-C, enabling Python scripts to use and extend macOS Cocoa libraries. `pyobjc-framework-avkit` provides Python wrappers for Apple's AVKit framework on macOS, allowing developers to integrate video playback and related UI elements into Python applications. It is part of the larger PyObjC project, which is actively maintained with frequent updates to support new macOS SDKs and Python versions. The current version is 12.1.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. Ensure your Python version is 3.10 or newer.
- breaking PyObjC 11.0 dropped support for Python 3.8. Ensure your Python version is 3.9 or newer.
- gotcha PyObjC 11.1 aligned initializer method behavior with `clang`'s documentation for automatic reference counting. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference, which might change behavior for custom initializers or subclasses.
- gotcha When subclassing Objective-C classes in Python and implementing both `__new__` and `__init__`, PyObjC 10.3 initially broke the ability to use `__init__`. While partially re-enabled in 10.3.1 for user-defined `__new__`, code relying on PyObjC's provided `__new__` still cannot use `__init__`.
- gotcha PyObjC is a macOS-specific library and will not run on other operating systems.
- gotcha Objective-C method names containing colons (`:`) are translated to Python with underscores (`_`) replacing colons, and an additional trailing underscore for each argument. E.g., `method:arg1 withOtherArgs:arg2` becomes `method_withOtherArgs_(arg1, arg2)`.
Install
-
pip install pyobjc-framework-avkit -
pip install pyobjc
Imports
- AVPlayer
from AVKit import AVPlayer
- AVPlayerViewController
from AVKit import AVPlayerViewController
- NSApplication
from AppKit import NSApplication
- NSURL
from Foundation import NSURL
Quickstart
import objc
from Foundation import NSURL
from AppKit import NSApplication, NSWindow, NSView, NSNotificationCenter, NSApplicationDidFinishLaunchingNotification, NSObject, NSRect
from AVKit import AVPlayer, AVPlayerViewController
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
print("Application finished launching.")
self.mainWindow = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
NSRect(100, 100, 640, 480),
4|8|16|1, # Titled | Closable | Miniaturizable | Resizable
2, # Backing store buffered
False # Not deferred
)
self.mainWindow.setTitle_("PyObjC AVKit Demo")
# Create an AVPlayer with a sample video URL
video_url = NSURL.URLWithString_("https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")
self.player = AVPlayer.playerWithURL_(video_url)
# Create an AVPlayerViewController
self.playerViewController = AVPlayerViewController.alloc().init()
self.playerViewController.setPlayer_(self.player)
# Add the player view controller's view to the window's content view
playerView = self.playerViewController.view()
playerView.setFrame_(self.mainWindow.contentView().bounds())
playerView.setAutoresizingMask_(NSView.ViewWidthSizable | NSView.ViewHeightSizable)
self.mainWindow.contentView().addSubview_(playerView)
self.mainWindow.makeKeyAndOrderFront_(self)
self.player.play()
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
NSApplication.sharedApplication().run()
if __name__ == '__main__':
main()