2017-04-07 46 views
0

我無法理解爲什麼我會收到這些錯誤。有人請幫助我。以下是源代碼擴展名可能不包含存儲的屬性,並且不會在swift中出現任何成員問題

import UIKit 
import AVFoundation 

// MARK: - PlaySoundsViewController: AVAudioPlayerDelegate 

extension PlaySoundsViewController: AVAudioPlayerDelegate { 

    var audioEngine = AVAudioEngine() 


    // MARK: Alerts 

    struct Alerts { 
    static let DismissAlert = "Dismiss" 
    static let RecordingDisabledTitle = "Recording disabled" 
    static let RecordginDisabledMessage = "Youve disabled this app from recording your microphone. Check settings" 
    static let RecodingFailedTitle = "Recording failed" 
    static let RecordingFailedMessage = "Something went wrong with the recording" 
    static let AudioRecordedError = "Audio Recorder Error" 
    static let AudioSessionError = "Audio Session Error" 
    static let AudioRecordingError = "Audio Recording Error" 
    static let AudioFileError = "Audio File Error" 
    static let AudioEngineError = "Audio Engine Error" 
    } 

    // MARK: PlayingState (raw values correspond to sender tags) 

    enum PlayingState { case playing, notPlaying } 

    // MARK: Audio Functions 

    func setupAudio() { 
     // initialize (recording) audio file 
     do { 
      audioFile = try AVAudioFile(forReading: recordedAudioURL as URL) 
     } catch { 
      showAlert(Alerts.AudioFileError, message: String(describing: error)) 
     }   
    } 

    func playSound(rate: Float? = nil, pitch: Float? = nil, echo: Bool = false, reverb: Bool = false) { 

     // initialize audio engine components 
     audioEngine = AVAudioEngine() 

     // node for playing audio 
     audioPlayerNode = AVAudioPlayerNode() 
     audioEngine.attach(audioPlayerNode) 

     // node for adjusting rate/pitch 
     let changeRatePitchNode = AVAudioUnitTimePitch() 
     if let pitch = pitch { 
      changeRatePitchNode.pitch = pitch 
     } 
     if let rate = rate { 
      changeRatePitchNode.rate = rate 
     } 
     audioEngine.attach(changeRatePitchNode) 

     // node for echo 
     let echoNode = AVAudioUnitDistortion() 
     echoNode.loadFactoryPreset(.multiEcho1) 
     audioEngine.attach(echoNode) 

     // node for reverb 
     let reverbNode = AVAudioUnitReverb() 
     reverbNode.loadFactoryPreset(.cathedral) 
     reverbNode.wetDryMix = 50 
     audioEngine.attach(reverbNode) 

     // connect nodes 
     if echo == true && reverb == true { 
      connectAudioNodes(audioPlayerNode, changeRatePitchNode, echoNode, reverbNode, audioEngine.outputNode) 
     } else if echo == true { 
      connectAudioNodes(audioPlayerNode, changeRatePitchNode, echoNode, audioEngine.outputNode) 
     } else if reverb == true { 
      connectAudioNodes(audioPlayerNode, changeRatePitchNode, reverbNode, audioEngine.outputNode) 
     } else { 
      connectAudioNodes(audioPlayerNode, changeRatePitchNode, audioEngine.outputNode) 
     } 

     // schedule to play and start the engine! 
     audioPlayerNode.stop() 
     audioPlayerNode.scheduleFile(audioFile, at: nil) { 

      var delayInSeconds: Double = 0 

      if let lastRenderTime = self.audioPlayerNode.lastRenderTime, let playerTime = self.audioPlayerNode.playerTime(forNodeTime: lastRenderTime) { 

       if let rate = rate { 
        delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime)/Double(self.audioFile.processingFormat.sampleRate)/Double(rate) 
       } else { 
        delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime)/Double(self.audioFile.processingFormat.sampleRate) 
       } 
      } 

      // schedule a stop timer for when audio finishes playing 
      self.stopTimer = Timer(timeInterval: delayInSeconds, target: self, selector: #selector(PlaySoundsViewController.stopAudio), userInfo: nil, repeats: false) 
      RunLoop.main.add(self.stopTimer!, forMode: RunLoopMode.defaultRunLoopMode) 
     } 

     do { 
      try audioEngine.start() 
     } catch { 
      showAlert(Alerts.AudioEngineError, message: String(describing: error)) 
      return 
     } 

     // play the recording! 
     audioPlayerNode.play() 
    } 

    func stopAudio() { 

     if let audioPlayerNode = audioPlayerNode { 
      audioPlayerNode.stop() 
     } 

     if let stopTimer = stopTimer { 
      stopTimer.invalidate() 
     } 

     configureUI(.notPlaying) 

     if let audioEngine = audioEngine { 
      audioEngine.stop() 
      audioEngine.reset() 
     } 
    } 

    // MARK: Connect List of Audio Nodes 

    func connectAudioNodes(_ nodes: AVAudioNode...) { 
     for x in 0..<nodes.count-1 { 
      audioEngine.connect(nodes[x], to: nodes[x+1], format: audioFile.processingFormat) 
     } 
    } 

    // MARK: UI Functions 

    func configureUI(_ playState: PlayingState) { 
     switch(playState) { 
     case .playing: 
      setPlayButtonsEnabled(false) 
      stopButton.isEnabled = true 
     case .notPlaying: 
      setPlayButtonsEnabled(true) 
      stopButton.isEnabled = false 
     } 
    } 

    func setPlayButtonsEnabled(_ enabled: Bool) { 
     snailButton.isEnabled = enabled 
     chipmunkButton.isEnabled = enabled 
     rabbitButton.isEnabled = enabled 
     vaderButton.isEnabled = enabled 
     echoButton.isEnabled = enabled 
     reverbButton.isEnabled = enabled 
    } 

    func showAlert(_ title: String, message: String) { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     alert.addAction(UIAlertAction(title: Alerts.DismissAlert, style: .default, handler: nil)) 
     self.present(alert, animated: true, completion: nil) 
    } 
} 

錯誤的屏幕截圖位於以下鏈接中。

Screenshot

回答

0

您不能在擴展中聲明var audioEngine = AVAudioEngine()。代之以在PlaySoundsViewController類中聲明它。

擴展是爲了增加行爲,而不是從根本上改變一個類。

@source:Why can't you add stored properties to extensions?

+0

更多信息:http://stackoverflow.com/questions/36337509/extensions-may-not-contain-stored-properties –

+0

非常感謝你的幫助。我已經在PlaySoundsViewController中聲明瞭它,但我仍然有同樣的問題 – SAM

+0

現在有什麼錯誤? –

相關問題