以下のような単純な修正が必要な場合は、Xcodeの最新情報に謝罪します。 「サポートファイル」ディレクトリの下にmp3ファイルをインポートした、別のプロジェクトのテストとして簡単なボタンを作成しました。下のコードは、私が従ったチュートリアルのためにいくつかのエラーを出しています。 。AVFoundationを使用してサウンドを再生しようとしています
AVFoundationもプロジェクトに追加されました。
エラー:
Argument labels '(_:, error:)' do -- Extra argument 'error' in call Use of unresolved identifier 'alertSound'
コード:最初のエラーのために
import UIKit
import AVFoundation
class ViewController: UIViewController {
var AudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let alertSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "two", ofType: "mp3")!)
print(alertSound)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func n2(_ sender: UIButton) {
var error:NSError?
AudioPlayer = AVAudioPlayer(contentsOfUrl: alertSound, error: &error)
AudioPlayer.prepareToPlay()
AudioPlayer.play()
}
}
変数の名前を指定する際に一貫した命名規則に従うべきです。つまり、「AudioPlayer」は「audioPlayer」である必要があります。 https://swift.org/documentation/api-design-guidelines/をご覧ください。 –