ios - Swift: play the audio recorded -
i tried record , find answer here: recording audio in swift
i make work. know how play recorded audio. recording have general var audiorecorder, , defined url path. tried audiorecorder.play() not work.
i suppose problem comes form fact global var audiorecorder instance of avaudiorecorder , play should instance of avaudioplayer?, how 2 things related?
i not want copy paste peace of code understand. why simplify code here. please explain why not work in specific code , how solve it.
(i have done lot of tutorials related. problem there code lost. question here understand how specific part works)
import avfoundation var audiorecorder:avaudiorecorder! @ibaction func record(sender: anyobject) { var audiosession:avaudiosession = avaudiosession.sharedinstance() audiosession.setcategory(avaudiosessioncategoryplayandrecord, error: nil) audiosession.setactive(true, error: nil) var documents: anyobject = nssearchpathfordirectoriesindomains( nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.userdomainmask, true)[0] var str = documents.stringbyappendingpathcomponent("recordtest.caf") var url = nsurl.fileurlwithpath(str string) println(url) audiorecorder = avaudiorecorder(url:url, settings: nil, error: nil) audiorecorder.record() } @ibaction func play(sender: anyobject) { // gives error 'avaudiorecorder' not have member named 'play' // audiorecorder.play() }
here complete working code record audio store file play it:
import uikit import avfoundation class viewcontroller: uiviewcontroller, avaudioplayerdelegate, avaudiorecorderdelegate { @iboutlet weak var recordbutton: uibutton! @iboutlet weak var stopbutton: uibutton! @iboutlet weak var playbutton: uibutton! var audioplayer : avaudioplayer? var audiorecorder : avaudiorecorder? override func viewdidload() { super.viewdidload() playbutton.enabled = false stopbutton.enabled = false // getting url path audio let dirpath = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true) let docdir = dirpath[0] as! string let soundfilepath = docdir.stringbyappendingpathcomponent("sound.caf") let soundfileurl = nsurl(fileurlwithpath: soundfilepath) println(soundfilepath) //setting recorder let recordsettings = [avencoderaudioqualitykey: avaudioquality.min.rawvalue, avencoderbitratekey: 16, avnumberofchannelskey : 2, avsampleratekey: 44100.0] var error : nserror? let audiosession = avaudiosession.sharedinstance() audiosession.setcategory(avaudiosessioncategoryplayandrecord, error: &error) if let err = error{ println("audiosession error: \(err.localizeddescription)") } audiorecorder = avaudiorecorder(url: soundfileurl, settings: recordsettings [nsobject : anyobject], error: &error) if let err = error{ println("audiosession error: \(err.localizeddescription)") }else{ audiorecorder?.preparetorecord() } } //record audio @ibaction func recordaudio(sender: anyobject) { if audiorecorder?.recording == false{ playbutton.enabled = false stopbutton.enabled = true audiorecorder?.record() } } //stop recording audio @ibaction func stopaudio(sender: anyobject) { stopbutton.enabled = false playbutton.enabled = true recordbutton.enabled = true if audiorecorder?.recording == true{ audiorecorder?.stop() }else{ audioplayer?.stop() } } //play recorded audio @ibaction func playaudio(sender: anyobject) { if audiorecorder?.recording == false{ stopbutton.enabled = true recordbutton.enabled = false var error : nserror? audioplayer = avaudioplayer(contentsofurl: audiorecorder?.url, error: &error) audioplayer?.delegate = self if let err = error{ println("audioplayer error: \(err.localizeddescription)") }else{ audioplayer?.play() } } } func audioplayerdidfinishplaying(player: avaudioplayer!, flag: bool) { recordbutton.enabled = true stopbutton.enabled = false } func audioplayerdecodeerrordidoccur(player: avaudioplayer!, error: nserror!) { println("audio play decode error") } func audiorecorderdidfinishrecording(recorder: avaudiorecorder!, flag: bool) { } func audiorecorderencodeerrordidoccur(recorder: avaudiorecorder!, error: nserror!) { println("audio record encode error") } }
check this sample project more info.
in code audiorecorder
used record audio , audioplayer
used play audio. thats why audiorecorder
doesn't have property play()
so can not use audiorecorder.play()
.
Comments
Post a Comment