extension may not contain stored properties and no member problems in swift -
i not able understand why getting these errors. please me. here source code
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 app recording microphone. check settings" static let recodingfailedtitle = "recording failed" static let recordingfailedmessage = "something went wrong 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 sender tags) enum playingstate { case playing, notplaying } // mark: audio functions func setupaudio() { // initialize (recording) audio file { audiofile = try avaudiofile(forreading: recordedaudiourl 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 playing audio audioplayernode = avaudioplayernode() audioengine.attach(audioplayernode) // node adjusting rate/pitch let changeratepitchnode = avaudiounittimepitch() if let pitch = pitch { changeratepitchnode.pitch = pitch } if let rate = rate { changeratepitchnode.rate = rate } audioengine.attach(changeratepitchnode) // node echo let echonode = avaudiounitdistortion() echonode.loadfactorypreset(.multiecho1) audioengine.attach(echonode) // node 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 play , start 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 stop timer 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) } { try audioengine.start() } catch { showalert(alerts.audioengineerror, message: string(describing: error)) return } // play 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...) { 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) } }
the screenshot of error in link below.
you can not declare var audioengine = avaudioengine()
inside extension. declare inside playsoundsviewcontroller
class instead.
extensions meant augment behaviours, not fundamentally change class.
Comments
Post a Comment