Playing Audio Files With PyObjC
Well... I have been developing a Cocoa application using PyObjC in which I have to convert some text to audio and then play it. After a little research a found that there is an object called NSSound used to load and play audio files. Well this is how it works:
import objc track = NSSound.alloc() track.initWithContentsOfFile_byReference_("audio.mp3", True)
As you can see track is an instance of NSSound, which I use to handle an audio file called "audio.mp3" ( I have also tried this with ".aiff" files and it works perfectly). Now, to play it:
Play the audio file:
track.play()
Stop the audio file:
track.stop()
Pause the audio file:
track.pause()
These are the three basic functions but there is another one used when the audio file is paused. To continue playing the audio file from where you paused it, simply do:
track.resume()
I hope this will be useful for you. If you are wondering about how I converted the text to audio, check out this snippet:
import os os.system("say -f foo.txt -o audio.mp3")
Well this code is pretty simple. I'm just using the MacOS X say command to convert the text in the file "foo.txt" to an audio file called "audio.mp3". This can take some time if the text is too long. If you want to be quicker, you can use another format try using "audio.aiff" instead of "audio.mp3".
So... that's it.











