ios - audioPlayerDidFinishPlaying 用法

标签 ios swift xcode

 class ViewController: UIViewController, AVAudioPlayerDelegate {    
 func audioPlayerDidFinishPlaying (_ player:AVAudioPlayer, successfully flag: Bool) {
    endcheck.text = "end"

}

@IBAction func play(_ sender: Any) {
    do
    {
        let url = Bundle.main.path (forResource: String(myIndex), ofType: "mp3")
        try player = AVAudioPlayer (contentsOf: NSURL(fileURLWithPath: url!) as URL)
    }
    catch{
    }

    player.play()


    let hasThePlayerEnded = audioPlayerDidFinishPlaying(_:player, successfully:true)

}
    super.viewDidLoad()
   self.player.delegate = self

}

就在我点击“播放”按钮的那一刻,我看到“结束”而不是声音播放完毕。我确定我做错了什么。我该如何解决?

最佳答案

play() 方法中删除以下语句

let hasThePlayerEnded = audioPlayerDidFinishPlaying(_:player, successfully:true) 

关于ios - audioPlayerDidFinishPlaying 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496244/

相关文章:

ios - 删除 NSLayoutConstraint 的正确方法

ios - Objective-C 无法使用 AVAudioPlayer 播放声音

objective-c - Swift - 如何更改仅获取属性的值

ios - 添加 UIViewController 作为 UIScrollView 的 subview

ios - 更新 MKMapkit View 注释不起作用?

ios - 从 View Controller Swift 更新 Collection View

ios - 为什么将排序键用作 filterExpression 时 AWSDynamoDBScanExpression 返回空值?

ios - NSLocationWhenInUseUsageDescription 错误一直显示

ios - 如何将 indexPath.row 设置为要在 NSTimer 选择器函数中使用的 TableView 函数之外的变量

ios - 用于 UITest 的简单 XCTest 总是失败?