audiokitaksequencer

#AudioKit Play sequencer audios mp3


I am trying to play a list of audios mp3 with AudioKit library. I read about sequencer and I saw all code in the example of official AudioKit Doc. After calling the method sequencer.play() there is no sound.

  var audios:[String] = [
         "/Users/rubenpalma/Documents/mp3/hola.mp3",
         "/Users/rubenpalma/Documents/mp3/prueba.mp3",
         "/Users/rubenpalma/Documents/mp3/mundo.mp3",
         "/Users/rubenpalma/Documents/mp3/test.mp3",
         "/Users/rubenpalma/Documents/mp3/hi.mp3",
         "/Users/rubenpalma/Documents/mp3/adios.mp3"
         ]


        var path = playAudios(background: back, audiosPath: audios)

func playAudios(background:String,audiosPath:[String]) -> String {

    var durationBackground = 0.00
    let midi = AKMIDI()
    let sequencer = AKSequencer()
    let mixer = AKMixer()
    do{
        var i = 0
        for path in audiosPath {
            let fileAudioURL = NSURL(fileURLWithPath: path)
            if let audio = try? AKAudioFile(forReading: fileAudioURL as URL) {
                print("\(path) size:\(audio.duration)")
                let sampler = AKMIDISampler()
                try sampler.loadAudioFile(audio)
                sampler.enableMIDI(midi.client, name: "Sampler_\(i)")

                mixer.connect(input: sampler)

                let track = sequencer.newTrack("t_\(i)")
                track?.setMIDIOutput(sampler.midiIn)
                track?.add(noteNumber: MIDINoteNumber(i), velocity: 127, position: AKDuration(beats:Double(0)), duration: AKDuration(beats:audio.duration*2), channel:1)
                print("is empty? \(track!.isEmpty)")

                durationBackground += audio.duration
                i += 1
            }
        }
        AudioKit.output = mixer
        try AudioKit.start()
        sequencer.play()

        print("playing.. \(sequencer.isPlaying) ")
    }catch{
        print("Unexpected non-vending-machine-related error: \(error)")
    }
    return "duration  \(durationBackground)"
}

Solution

  • AKSequencer is to play midi files. To play mp3-files you'd use AKPlayer http://audiokit.io/docs/Classes/AKPlayer.html