Swift and Audio

SwiftAudio1

Many people are now developing applications for iOS.

//
//  ViewController.swift
//

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {
    
    var audioPlayer:AVAudioPlayer!
    
    @IBOutlet var button:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myfile", ofType: "mp3")!)
        audioPlayer = try!AVAudioPlayer(contentsOfURL: audioPath, fileTypeHint: nil)
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.play()
        
    }
    
}

SwiftAudio2

The storyboard is just a dummy, but you can hear the sound with the Simulator.

Leave a Reply

Your email address will not be published. Required fields are marked *