Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import WatchKit
  2. import Foundation
  3. import AVFoundation
  4.  
  5. class InterfaceController: WKInterfaceController, AVAudioRecorderDelegate{
  6. @IBOutlet weak var btn: WKInterfaceButton!
  7. var recordingSession : AVAudioSession!
  8. var audioRecorder : AVAudioRecorder!
  9. var settings = [String : Any]()
  10.  
  11. override func awake(withContext context: Any?) {
  12. super.awake(withContext: context)
  13. recordingSession = AVAudioSession.sharedInstance()
  14.  
  15. do{
  16. try recordingSession.setCategory(AVAudioSession.Category.playAndRecord)
  17. try recordingSession.setActive(true)
  18. recordingSession.requestRecordPermission(){[unowned self] allowed in
  19. DispatchQueue.main.async {
  20. if allowed{
  21. print("Allow")
  22. } else{
  23. print("Don't Allow")
  24. }
  25. }
  26. }
  27. }
  28. catch{
  29. print("failed to record!")
  30. }
  31. // Configure interface objects here.
  32.  
  33. // Audio Settings
  34.  
  35. settings = [
  36. AVFormatIDKey:Int(kAudioFormatLinearPCM),
  37. AVSampleRateKey:44100.0,
  38. AVNumberOfChannelsKey:1,
  39. AVLinearPCMBitDepthKey:8,
  40. AVLinearPCMIsFloatKey:false,
  41. AVLinearPCMIsBigEndianKey:false,
  42. AVEncoderAudioQualityKey:AVAudioQuality.max.rawValue
  43.  
  44. ]
  45.  
  46. }
  47.  
  48. override func willActivate() {
  49. // This method is called when watch view controller is about to be visible to user
  50. super.willActivate()
  51. }
  52.  
  53. override func didDeactivate() {
  54. // This method is called when watch view controller is no longer visible
  55. super.didDeactivate()
  56. }
  57.  
  58.  
  59. func directoryURL() -> NSURL? {
  60. let fileManager = FileManager.default
  61. let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
  62. let documentDirectory = urls[0] as NSURL
  63. let soundUrl = documentDirectory.appendingPathComponent("sound.wav")
  64. print(soundUrl)
  65. return soundUrl as NSURL?
  66.  
  67. }
  68.  
  69. func startRecording(){
  70. let audioSession = AVAudioSession.sharedInstance()
  71.  
  72. do{
  73. audioRecorder = try AVAudioRecorder(url: self.directoryURL()! as URL,
  74. settings: settings)
  75. audioRecorder.delegate = self
  76. audioRecorder.prepareToRecord()
  77. audioRecorder.record(forDuration: 5.0)
  78.  
  79. }
  80. catch {
  81. finishRecording(success: false)
  82. }
  83.  
  84. do {
  85. try audioSession.setActive(true)
  86. audioRecorder.record()
  87. } catch {
  88. }
  89. }
  90.  
  91. func finishRecording(success: Bool) {
  92. audioRecorder.stop()
  93. if success {
  94. print(success)
  95. } else {
  96. audioRecorder = nil
  97. print("Somthing Wrong.")
  98. }
  99. }
  100.  
  101.  
  102. @IBAction func recordAudio() {
  103. if audioRecorder == nil {
  104. print("Pressed")
  105. self.btn.setTitle("Stop")
  106. self.btn.setBackgroundColor(UIColor(red: 119.0/255.0, green: 119.0/255.0, blue: 119.0/255.0, alpha: 1.0))
  107. self.startRecording()
  108.  
  109.  
  110. } else {
  111. self.btn.setTitle("Record")
  112. print("Pressed2")
  113. self.btn.setBackgroundColor(UIColor(red: 221.0/255.0, green: 27.0/255.0, blue: 50.0/255.0, alpha: 1.0))
  114. self.finishRecording(success: true)
  115.  
  116. }
  117. }
  118.  
  119. func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
  120. if !flag {
  121. finishRecording(success: false)
  122. }
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement