skip to Main Content

I am learning Swift, and I am throwing myself in the deep end by building an app to force myself to learn the language. Something I want to implement is that when I transfer from screen 1 to screen 2, audio is played without needing to do anything. The audio is A-14a. The code below is set up so that when I click the Directions button, it plays the audio, but I don’t know how to do it right away. Images are below to help show what I mean.

1st Screen 2nd Screen

The code I have is below:

import UIKit
import AVFoundation

class Intervention_Numerals1: UIViewController {


    @IBOutlet weak var Directions: UIButton!
    @IBOutlet weak var Done: UIButton!
    var audioPlayer = AVAudioPlayer()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpElements()
        //Audio Test
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "A-N14a", ofType:"mp3")!))
            audioPlayer.prepareToPlay()
        } catch {
            print(error)
        }
    }
    
    func setUpElements() {
        // Style the elements
        Utilities.styleFilledButton(Directions)
        Utilities.styleFilledButton(Done)
    }

    @IBAction func Play(_ sender: Any) {
        audioPlayer.play()
    }
    
    
}

Please let me know any advice on how to do this

2

Answers


  1. Use this method

     override func viewDidAppear(_ animated: Bool) {
             super.viewDidAppear(animated)
             audioPlayer.play()
        }
    
    Login or Signup to reply.
  2. There are a couple of methods you can override to know the state of the view controller and add code to run when that state occurs.

    As mentioned by Amila what you are looking for is probably viewDidAppear,
    Another method that might also help you achieve this is viewWillAppear

    I will provide the code of most of these methods and what they do below so you can try them all and experiment with them:

    override func viewDidLoad() {
        //This is what you already use every time, its called when the VC's view is loaded into memory
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //This happens when the view is *about* to appear, it happens before users see anything from the view, great for updating UI 
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //This happens when the view actually appears on screen and when all the animations of loading the View Controller are over. 
        //not so good for updating UI since users will see a glimpse of previous view data.
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //This happens when the view is *about* to disappear, great example for this is when you begin swiping to go back in a navigation controller.
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        //This happens when view is no longer on screen for user to see, like when that swiping back animation in using navigation controller is *finished*
    }
    

    There are a few more of these such as viewWillLayoutSubviews and viewDidLayoutSubviews that occur when views inside your View Controller change.

    Use all these to control when what happens, hope this helps 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search