4

Is it possible to continue playing sound after a route change (such as switching speakers or plugging in/unplugging headphones) without reloading the sample again in Sampler?

This is the only approach I have found that works:

    @objc dynamic private func audioRouteChangeListener(notification:NSNotification) {
                let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt
                switch audioRouteChangeReason {

                case AVAudioSession.RouteChangeReason.newDeviceAvailable.rawValue:
                    print("headphone plugged in")
                    DispatchQueue.global().async { [weak self] in

                    // Note the reload here
                    self?.conductor?.loadSamples(byIndex: 2)

                }

                case AVAudioSession.RouteChangeReason.oldDeviceUnavailable.rawValue:
                    print("headphone pulled out")
                    DispatchQueue.global().async { [weak self] in

                    // Note the reload here
                    self?.conductor?.loadSamples(byIndex: 2)
                }

                default:
                    break
                }
            }

And in the Conductor class, where I load .wv samples in .sfz:

    func loadSamples(byIndex: Int) {
        sampler1.unloadAllSamples()

        if byIndex < 0 || byIndex > 3 { return }

        let info = ProcessInfo.processInfo
        let begin = info.systemUptime

        let sfzFiles = [ "City Piano.sfz", "ShortPiano.sfz", "LongPiano.sfz" ]
        sampler1.loadSfzWithEmbeddedSpacesInSampleNames(folderPath: Bundle.main.bundleURL.appendingPathComponent("Sounds/sfz").path,
                                                       sfzFileName: sfzFiles[byIndex])

        let elapsedTime = info.systemUptime - begin
        AKLog("Time to load samples \(elapsedTime) seconds")

    }
B.T.
  • 610
  • 6
  • 20

0 Answers0