Play mp3 from python on Mac

This site has a few nice little PyObjC samples.
One of them shows how to play a sound using AppKit’s NSSound. This is exactly what I was trying to do today.

And since PyObjC is built-in in 10.5, no addtional software is required.

Python is already batteries included, with PyObjC, you’ve got batteries galore.

ipython output:

In [1]: from AppKit import NSSound
In [2]: sound = NSSound.alloc()
In [3]: sound.initWithContentsOfFile_byReference_('/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/TransitionSection.bundle/Contents/Resources/intro-sound.mp3', True)
Out[3]:
In [4]: sound.play()
Out[4]: True
In [5]: sound.stop()
Out[5]: True

About these ads

4 Comments on “Play mp3 from python on Mac”

  1. Michael Unger says:

    Hello,

    I tried your code but I can’t play a system sound on my G5 with Tiger. Does it actually work with Tiger. I just want to run a little program that plays a system sound when a web-site is successfully loaded. Can you help me?

    Regards

    M. Unger

  2. Kyle Crawford says:

    Michael, I haven’t tested this, but you probably need to install PyObjC. It comes pre-installed on Leopard.

  3. e-ane says:

    Thanks,

    Works fine for me!

  4. larryy says:

    Thanks! Nice and easy, which I was glad to see after having failed with snack (and Tkinter; even once everything ran without error I got no sound). This worked first try. Thanks for the tip!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.