osx - Mac - Swift 3 - queuing audio files and playing -


i write app in swift 3 in order play queued audio files without gap, crack or noise when passing 1 another.

my first try using avaudioplayer , avaudiodelegate (avaudioplayer using array queue audio files - swift), don't know how preload next song avoid gap. if know how it, not best way achieve goal. avqueueplayer seems better candidate job, made purpose, don't find example me out. maybe problem of preloading or buffering? bit lost in ocean of possibilities.

any suggestion welcomed.

it far perfect, specially if want twice or more ("file exist" error), can serve base.

what taking 2 files (mines aif samples of ap. 4 sec.), encode them in 1 file , play resulting files. if have hundreds of them, assembled aleatory or not, can make great fun.

all credits mergeaudiofiles function goes @peyman , @pigeon_39. concatenate 2 audio files in swift , play them

swift 3

import cocoa import avfoundation  var action = avaudioplayer() let path = bundle.main.path(forresource: "audiofile1.aif", oftype:nil)! let url = url(fileurlwithpath: path) let path2 = bundle.main.path(forresource: "audiofile2.aif", oftype:nil)! let url2 = url(fileurlwithpath: path2) let array1 = nsmutablearray(array: [url, url2])   class viewcontroller: nsviewcontroller, avaudioplayerdelegate {      @iboutlet weak var lancestop: nsbutton!      override func viewdidload()     {         super.viewdidload()     }     override var representedobject: any?     {         didset         {         // update view, if loaded.         }     }      @ibaction func lancer(_ sender: nsbutton)     {       mergeaudiofiles(audiofileurls: array1)         let url3 = nsurl(string: "/users/addusernamehere/documents/finalaudio.m4a")                  {             action = try avaudioplayer(contentsof: url3 as! url)             action.delegate = self             action.numberofloops = 0             action.preparetoplay()             action.volume = 1             action.play()         }         catch{print("error")}      }       func audioplayerdidfinishplaying(_ player: avaudioplayer, flag: bool)     {         if flag == true         {          }     }      var mergeaudiourl = nsurl()      func mergeaudiofiles(audiofileurls: nsarray) {         //audiofileurls.adding(url)         //audiofileurls.adding(url2)         let composition = avmutablecomposition()          in 0 ..< audiofileurls.count {              let compositionaudiotrack :avmutablecompositiontrack = composition.addmutabletrack(withmediatype: avmediatypeaudio, preferredtrackid: cmpersistenttrackid())              let asset = avurlasset(url: (audiofileurls[i] as! nsurl) url)              let track = asset.tracks(withmediatype: avmediatypeaudio)[0]              let timerange = cmtimerange(start: cmtimemake(0, 600), duration: track.timerange.duration)              try! compositionaudiotrack.inserttimerange(timerange, of: track, at: composition.duration)         }          let documentdirectoryurl = filemanager.default.urls(for: .documentdirectory, in: .userdomainmask).first! nsurl         self.mergeaudiourl = documentdirectoryurl.appendingpathcomponent("finalaudio.m4a")! url nsurl          let assetexport = avassetexportsession(asset: composition, presetname: avassetexportpresetapplem4a)         assetexport?.outputfiletype = avfiletypeapplem4a         assetexport?.outputurl = mergeaudiourl url         assetexport?.exportasynchronously(completionhandler:             {                 switch assetexport!.status                 {                 case avassetexportsessionstatus.failed:                     print("failed \(assetexport?.error)")                 case avassetexportsessionstatus.cancelled:                     print("cancelled \(assetexport?.error)")                 case avassetexportsessionstatus.unknown:                     print("unknown\(assetexport?.error)")                 case avassetexportsessionstatus.waiting:                     print("waiting\(assetexport?.error)")                 case avassetexportsessionstatus.exporting:                     print("exporting\(assetexport?.error)")                 default:                     print("audio concatenation complete")                 }         })     } } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -