Search code examples
swiftxcodeaudiokit

Play sound with AKAudioPlayer - iOS


How to declare AKAudioPlayer?

I'm using AudioKit Lib and I just need help to play .wav file with change file button.

    import UIKit
    import AudioKit

    class ViewController: UIViewController {

            let file   = NSBundle.mainBundle().pathForResource("song", ofType: "wav")
            let song  = AKAudioPlayer(file!) // <--- ERROR = instance member 'file' cannot be used on type 

            override func viewDidLoad() {
                super.viewDidLoad()

                AudioKit.output = song
                AudioKit.start()

                song.play()
            }


            @IBAction func btn(sender: AnyObject) {

                song.replaceFile("NewFile")
                song.play()

            }

        }

Solution

  • take look on AudioKit playground

    update

    AKAudioPlayer is deprecated, use AudioPlayer insted and check AudioKit v5 Migration Guide