Created
December 16, 2015 22:29
-
-
Save rfdickerson/fbeb73f6113e7c05eb9d to your computer and use it in GitHub Desktop.
Example of using AVAudioRecorder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func testRecording() { | |
| let recordExpectation = expectationWithDescription("Record") | |
| let recordSettings = [ | |
| // AVFormatIDKey: NSNumber(unsignedInt:kAudioFormatLinearPCM), | |
| AVNumberOfChannelsKey: 1, | |
| AVSampleRateKey : 16000.0 | |
| ] | |
| let dirsPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) | |
| let docsDir = dirsPaths[0] as String | |
| let soundFilePath = docsDir + "/test.wav" | |
| print("Saving recorded audio file in \(soundFilePath)") | |
| AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool) -> Void | |
| in | |
| if granted { | |
| let soundFileURL = NSURL(string: soundFilePath) | |
| if let soundFileURL = soundFileURL { | |
| do { | |
| self.recorder = try AVAudioRecorder(URL: soundFileURL, settings: recordSettings) | |
| self.recorder.meteringEnabled = true | |
| // self.recorder.prepareToRecord() | |
| self.recorder.recordForDuration(3.0) | |
| sleep(5) | |
| print(self.recorder.recording) | |
| self.recorder.stop() | |
| recordExpectation.fulfill() | |
| } catch { | |
| XCTAssertTrue(false, "Could not create audio recorder") | |
| } | |
| } | |
| } | |
| }) | |
| waitForExpectationsWithTimeout(timeout) { | |
| error in XCTAssertNil(error, "Timeout") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment