Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import WatchKit
- import Foundation
- import AVFoundation
- class InterfaceController: WKInterfaceController, AVAudioRecorderDelegate{
- @IBOutlet weak var btn: WKInterfaceButton!
- var recordingSession : AVAudioSession!
- var audioRecorder : AVAudioRecorder!
- var settings = [String : Any]()
- override func awake(withContext context: Any?) {
- super.awake(withContext: context)
- recordingSession = AVAudioSession.sharedInstance()
- do{
- try recordingSession.setCategory(AVAudioSession.Category.playAndRecord)
- try recordingSession.setActive(true)
- recordingSession.requestRecordPermission(){[unowned self] allowed in
- DispatchQueue.main.async {
- if allowed{
- print("Allow")
- } else{
- print("Don't Allow")
- }
- }
- }
- }
- catch{
- print("failed to record!")
- }
- // Configure interface objects here.
- // Audio Settings
- settings = [
- AVFormatIDKey:Int(kAudioFormatLinearPCM),
- AVSampleRateKey:44100.0,
- AVNumberOfChannelsKey:1,
- AVLinearPCMBitDepthKey:8,
- AVLinearPCMIsFloatKey:false,
- AVLinearPCMIsBigEndianKey:false,
- AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue
- ]
- }
- override func willActivate() {
- // This method is called when watch view controller is about to be visible to user
- super.willActivate()
- }
- override func didDeactivate() {
- // This method is called when watch view controller is no longer visible
- super.didDeactivate()
- }
- func directoryURL() -> NSURL? {
- let fileManager = FileManager.default
- let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
- let documentDirectory = urls[0] as NSURL
- let soundUrl = documentDirectory.appendingPathComponent("sound.wav")
- print(soundUrl)
- return soundUrl as NSURL?
- }
- func startRecording(){
- let audioSession = AVAudioSession.sharedInstance()
- do{
- audioRecorder = try AVAudioRecorder(url: self.directoryURL()! as URL,
- settings: settings)
- audioRecorder.delegate = self
- audioRecorder.prepareToRecord()
- audioRecorder.record(forDuration: 5.0)
- }
- catch {
- finishRecording(success: false)
- }
- do {
- try audioSession.setActive(true)
- audioRecorder.record()
- } catch {
- }
- }
- func finishRecording(success: Bool) {
- audioRecorder.stop()
- if success {
- print(success)
- } else {
- audioRecorder = nil
- print("Somthing Wrong.")
- }
- }
- @IBAction func recordAudio() {
- if audioRecorder == nil {
- print("Pressed")
- self.btn.setTitle("Stop")
- self.btn.setBackgroundColor(UIColor(red: 119.0/255.0, green: 119.0/255.0, blue: 119.0/255.0, alpha: 1.0))
- self.startRecording()
- } else {
- self.btn.setTitle("Record")
- print("Pressed2")
- self.btn.setBackgroundColor(UIColor(red: 221.0/255.0, green: 27.0/255.0, blue: 50.0/255.0, alpha: 1.0))
- self.finishRecording(success: true)
- }
- }
- func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
- if !flag {
- finishRecording(success: false)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement