gpt4 book ai didi

ios - 声音停止播放后如何执行 segue

转载 作者:行者123 更新时间:2023-11-29 00:08:21 24 4
gpt4 key购买 nike

我在 Main.Storyboard 中制作了一个自定义 LaunchScreen,使声音看起来实际上是从 LaunchScreen 发出的。声音效果很好,也可以转至下一个 View Controller 。唯一的问题是 segue 发生在声音停止播放之前。我希望在进行 segue 之前完成声音。从逻辑上讲,它应该可以工作,因为 performSegue 直接在 .play() 之后。但似乎两者同时发生。这是我的代码:

 super.viewDidLoad()

//PLAY SOUND CLIP//
let musicFile = Bundle.main.path(forResource: "fanfare", ofType: ".mp3")
do {
try musicSound = AVAudioPlayer(contentsOf: URL (fileURLWithPath: musicFile!))
} catch { print("ERROR PLAYING MUSIC")}

musicSound.play() //WORKS
//


DispatchQueue.main.async() {
self.performSegue(withIdentifier: "myLaunchSegue", sender: self) //WORKS
}

我尝试添加:

perform(Selector(("showNavController")), with: self, afterDelay: 3)

其中“showNavController”只是转场:

 func showNavController() {
performSegue(withIdentifier: "myLaunchSegue", sender: nil)
}

但程序崩溃并出现错误“未捕获的异常.... ....无法识别的选择器发送到实例”

我还尝试添加一个 bool 值来阻止程序在声音播放之前继续进行,但没有让它工作。有什么想法吗?

/////////////////////////更新:尝试 Russels 的回答,但有几个问题。将 AVAudioPlayer 设置为委托(delegate),是否意味着将它设置在类旁边,如下所示:

class MyLaunchViewController: UIViewController, AVAudioPlayer { ...

此外,如何调用 audioPlayerDidFinishPlaying 函数?像这样:

audioPlayerDidFinishPlaying(musicSound, successfully: true)

我将发布整个代码块。使它更容易理解...

import UIKit

import AVFoundation//FOR SOUND

类 MyLaunchViewController: UIViewController, AVAudioPlayer { var musicSound: AVAudioPlayer = AVAudioPlayer()//FOR SOUND

override func viewDidLoad() {
super.viewDidLoad()

//PLAY SOUND CLIP//
let musicFile = Bundle.main.path(forResource: "fanfare", ofType: ".mp3")
do {
try musicSound = AVAudioPlayer(contentsOf: URL (fileURLWithPath: musicFile!))
} catch { print("ERROR PLAYING MUSIC")}

musicSound.play() //WORKS
//
audioPlayerDidFinishPlaying(musicSound, successfully: true)
}

optional func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
DispatchQueue.main.async() {
self.performSegue(withIdentifier: "myLaunchSegue", sender: self)
}
}

我在类中编写 AVAudioPlayer 时出错(也许我误解了我应该做什么?)。它说我有多重继承。此外,它不希望我将新功能设置为可选功能,因为它仅适用于协议(protocol)成员。最后,如果我更正错误并运行程序,下一个 segue 在声音播放完之前运行...:( 悲伤的 Pandas 。

最佳答案

您需要将 LaunchScreen 设为 AVAudioPlayerDelegate,然后使用 audioPlayerDidFinishPlaying 回调。这是您在第一个 Controller 中所需要的一切

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate
{
var musicSound: AVAudioPlayer?

override func viewDidLoad() {
super.viewDidLoad()
//PLAY SOUND CLIP//
let musicFile = Bundle.main.path(forResource: "sound", ofType: ".wav")
do {
try musicSound = AVAudioPlayer(contentsOf: URL (fileURLWithPath: musicFile!))
} catch { print("ERROR PLAYING MUSIC")}

musicSound?.delegate = self
musicSound!.play() //WORKS
print("Next line after play")
}

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
DispatchQueue.main.async() {
print("audioPlayerDidFinishPlaying")
self.performSegue(withIdentifier: "myLaunchSegue", sender: self)
}
}
}

您可以在此处获得更多详细信息 https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate/1389160-audioplayerdidfinishplaying

关于ios - 声音停止播放后如何执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47332483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com