【发布时间】:2014-07-18 16:53:34
【问题描述】:
我试图让我的 PyObjC 应用程序在用户单击按钮时录制音频。我正在尝试使用 AVAuidioRecorder 类。我的代码是:
@IBAction
def startRecording_(self, sender):
audioPath = '~/Desktop/recordTest.mp3'
audioPathStr = NSString.stringByExpandingTildeInPath(audioPath)
audioURL = NSURL.fileURLWithPath_(audioPathStr)
audioSettings = {'AVFormatIDKey': 'kAudioFormatAppleIMA4', 'AVSampleRateKey': 1600.0, 'AVNumberOfChannelsKey': 1 }
audioDict = NSDictionary.dictionaryWithDictionary_(audioSettings)
(recorder, error) = AVAudioRecorder.alloc().initWithURL_settings_error_(audioURL, audioDict, objc.nil)
recorder.record()
当我运行上面的代码时,我得到以下错误:
<type 'exceptions.TypeError'>: 'NoneType' object is not iterable
似乎initWithURL_settings_error_ 方法期望一个可迭代对象作为其第三个参数。但是,我认为当我使用调用错误参数的 PyObjC 方法时,我可以将 objc.nil 或 None 传递给该参数。
当我在 NSString 方法上使用类似的语法时:
(names, error) = NSString.stringWithContentsOfFile_encoding_error_(u"/usr/share/dict/propernames", NSASCIIStringEncoding, objc.nil)
代码运行。
为什么我对 AVAudioRecord 方法的调用不起作用?是不是因为方法调用 outError 而 NSString 方法调用 error?
【问题讨论】:
-
在语义上,您应该使用
None或objc.NULL作为该参数,但objc.nil应该可以工作。我不认为这是该异常的根源。您确定您创建的 URL 不是None,并且该文件存在于该路径中吗?另外,请注意,您不必像这样将 Python dict 转换为NSDictionary;您可以将 dict 作为参数传入,桥会为您处理它。 -
@JoshCaswell URL 不是
None,我认为该文件不应该存在于该路径中,因为这个 AVAudioRecorder 应该创建该文件,而不是读取它。
标签: python objective-c cocoa avaudiorecorder pyobjc