【问题标题】:Incorrect argument label in call?调用中的参数标签不正确?
【发布时间】:2016-02-20 19:05:26
【问题描述】:

请帮帮我!!!我最近才开始使用 Swift 编程。这是我的第一个项目。我收到错误消息:“调用中的参数标签不正确”。这是我的代码:

import UIKit
import AVFoundation

class PlaySoundsViewController: UIViewController {

var audioPlayer:AVAudioPlayer!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    if var filePath = NSBundle.mainBundle().pathForResource("psl",ofType: "mp3"){
        var filePathUrl = NSURL.fileURLWithPath(filePath)
        audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)

    }else {
        print("the filepath is empty")
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func playSoundSlow(sender: UIButton) {
    audioPlayer.play()

我不确定这里出了什么问题是我的代码的另一张图片,所以你也可以看到错误消息。My Code

我正在尝试让它播放名为 psl.mp3 的 mp3。 请帮我!!!我刚开始,不知道该怎么办。

附:不是以英语为母语,如有错误请见谅。

【问题讨论】:

    标签: swift error-handling swift2


    【解决方案1】:

    Swift 2 添加了新的错误处理,这意味着您甚至不必传入错误参数:

    audioPlayer = try! AVAudioPlayer(contentsOfURL: filePathUrl)
    

    这个初始化器throws,意思是如果有错误,你可以在do-catch 语句中捕获它。但是,由于您为错误参数传递了nil,我假设您确定播放器在那里。这就是为什么我在 do-catch 中使用 try! 而不使用 do-catch 而不是 try

    了解新的错误处理here

    【讨论】:

    • @MeithV 既然成功了,你能接受我的回答吗? :)
    【解决方案2】:

    试试这个

        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL.fileURLWithPath(path))
            audioPlayer.delegate = self
            audioPlayer.prepareToPlay()
            audioPlayer.play()            
    
        } catch {
            print("Catch error in playUsingAudioPlayer")
       }
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 2018-03-05
      • 2017-11-25
      • 1970-01-01
      • 2018-09-03
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多