【问题标题】:How to automatically play audio when entering a screen进入屏幕时如何自动播放音频
【发布时间】:2021-03-12 05:22:35
【问题描述】:

我正在学习 Swift,我正在通过构建一个应用程序来强迫自己学习这门语言,从而让自己陷入困境。我想要实现的是,当我从屏幕 1 传输到屏幕 2 时,无需执行任何操作即可播放音频。音频是 A-14a。下面的代码是这样设置的,当我单击方向按钮时,它会播放音频,但我不知道如何立即执行。下面的图片有助于说明我的意思。

1st Screen2nd Screen

我的代码如下:

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()
    }
    
    
}

请告诉我有关如何执行此操作的任何建议

【问题讨论】:

    标签: swift xcode audio autoplay


    【解决方案1】:

    使用这个方法

     override func viewDidAppear(_ animated: Bool) {
             super.viewDidAppear(animated)
             audioPlayer.play()
        }
    

    【讨论】:

      【解决方案2】:

      您可以重写几个方法来了解视图控制器的状态并添加代码以在该状态发生时运行。

      正如阿米拉所说,您正在寻找的可能是viewDidAppearviewWillAppear

      也可以帮助您实现这一目标的另一种方法

      我将在下面提供大多数这些方法的代码以及它们的作用,以便您可以全部尝试并试验它们:

      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*
      }
      

      当您的视图控制器中的视图发生变化时,还会出现更多这些情况,例如 viewWillLayoutSubviewsviewDidLayoutSubviews

      使用所有这些来控制什么时候发生,希望这会有所帮助:)

      【讨论】:

        猜你喜欢
        • 2020-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多