ios - Audio from merge not working -


i used reference: concatenate 2 audio files in swift , play them

i'm trying create alarm clock, due restrictive apple policy cannot execute code in bg past 10 minutes, if opt out of bg modes , leave app in foreground "sound" suspends if user clicks home button, need sound go on until user something. uilocalnotifs don't work due silent/do not disturb buttons. want run blank audio until alarm time , play sound.

so used following run it:

this happens when save button clicked (user has selected alarm time)

 let seconds = double(comp.second)     let notification = uilocalnotification()     notification.alertbody = "testbody"     notification.firedate = duedatepicker.date     notification.alerttitle = "testtitle"     println("seconds:\(seconds)")        var results:nsarray = managedobjectcontext!.executefetchrequest(request, error: &error)!       let audiourl1 = nsbundle.mainbundle().urlforresource("alarm", withextension: "m4a")!     let audiourl2 = nsbundle.mainbundle().urlforresource("music", withextension: "mp3")!     println(audiourl1)     println(audiourl2)     println(task.uuid)             mergeaudio2(audiourl1, audiourl2: audiourl2, time: seconds, uuid: task.uuid) 

and merge audio portion:

func mergeaudio2(audiourl: nsurl, audiourl2: nsurl, time:double, uuid:string) {     var error:nserror?      var ok1 = false     var ok2 = false       //var documentsdirectory:string = paths[0] as! string      //create avmutablecomposition object.this object hold our multiple avmutablecompositiontrack.     var composition = avmutablecomposition()     var compositionaudiotrack1:avmutablecompositiontrack = composition.addmutabletrackwithmediatype(avmediatypeaudio, preferredtrackid: cmpersistenttrackid())     var compositionaudiotrack2:avmutablecompositiontrack = composition.addmutabletrackwithmediatype(avmediatypeaudio, preferredtrackid: cmpersistenttrackid())      //create new file receive data     var documentdirectoryurl = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask).first! as! nsurl     var filedestinationurl = documentdirectoryurl.urlbyappendingpathcomponent("resultmerge.wav")     println(filedestinationurl)      var file = "resultmerge.m4a"     var dirs : [string] = (nssearchpathfordirectoriesindomains(nssearchpathdirectory.documentdirectory, nssearchpathdomainmask.alldomainsmask, true) as? [string])!     var dir = dirs[0] //documents directory     var path = dir.stringbyappendingpathcomponent(file)     var pathurlarray:array = (nsurl(fileurlwithpath: path)!).pathcomponents!     var pathurl:string = ""     var final = ""     var debut = ""      in 1...(pathurlarray.count-1) {         if == pathurlarray.count-1 {             final = ""         } else {             final = "/"         }         if == 1 {             debut = "/"         } else {             debut = ""         }         pathurl = debut + pathurl + (pathurlarray[i] as! string) + final     }      var checkvalidation = nsfilemanager.defaultmanager()     if checkvalidation.fileexistsatpath(pathurl) {         println("file exist")         if nsfilemanager.defaultmanager().removeitematurl(filedestinationurl, error: nil) {             println("delete")         }     } else {         println("no file")     }      var url1 = audiourl     var url2 = audiourl2       var avasset1 = avurlasset(url: url1, options: nil)     var avasset2 = avurlasset(url: url2, options: nil)      var tracks1 =  avasset1.trackswithmediatype(avmediatypeaudio)     var tracks2 =  avasset2.trackswithmediatype(avmediatypeaudio)      var assettrack1:avassettrack = tracks1[0] as! avassettrack     var assettrack2:avassettrack = tracks2[0] as! avassettrack       var duration1: cmtime = assettrack1.timerange.duration     var duration2: cmtime = assettrack2.timerange.duration      var timerange1 = cmtimerangemake(kcmtimezero, duration1)     var timerange2 = cmtimerangemake(duration1, duration2)       ok1 = compositionaudiotrack1.inserttimerange(timerange1, oftrack: assettrack1, attime: kcmtimezero, error: nil)     if ok1 {          ok2 = compositionaudiotrack2.inserttimerange(timerange2, oftrack: assettrack2, attime: duration1, error: nil)          if ok2 {             println("success")         }     }      //avassetexportpresetpassthrough => concatenation       var assetexport = avassetexportsession(asset: composition, presetname: avassetexportpresetapplem4a)     assetexport.outputfiletype = avfiletypeapplem4a     assetexport.outputurl = filedestinationurl     assetexport.exportasynchronouslywithcompletionhandler({         switch assetexport.status{         case  avassetexportsessionstatus.failed:             println("failed \(assetexport.error)")         case avassetexportsessionstatus.cancelled:             println("cancelled \(assetexport.error)")         default:             println("complete")             var audioplayer = avaudioplayer()             avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback, error: nil)             //audioplayer.delegate = self              audioplayer = avaudioplayer(contentsofurl: filedestinationurl, error: nil)             println(filedestinationurl)             audioplayer.preparetoplay()             audioplayer.play()         }      })   } 

the app loads , fine, println's of files , no file, success , completed print out meaning executes. no sound plays

but no sound plays

because audioplayer local variable , dies before has chance play anything. promote instance variable persists.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -