2016-11-21 9 views
-1

以下のような単純な修正が必要な場合は、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() 
    } 
} 
+0

変数の名前を指定する際に一貫した命名規則に従うべきです。つまり、「AudioPlayer」は「audioPlayer」である必要があります。 https://swift.org/documentation/api-design-guidelines/をご覧ください。 –

答えて

1

: 引数ラベル '(_ :,エラー:)' を実行 - 特別な引数「エラー'call

Objectiveエラーパラメータを含み、ブール値を返す関数です。 Swift 3に例外をスローする可能性のある関数としてマークされます。do..try..catch構造体を使用してエラーを処理できます。

あなたはここでエラー処理上のアップルのドキュメントをチェックすることができます https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

AudioPlayer変数がスコープの外にアクセスされているローカル変数であることに関連するその他のエラーを。

var AudioPlayer = AVAudioPlayer() 

// Declare alertSound at the instance level for use by other functions. 
let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "two", ofType: "mp3")!) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    print(alertSound) 

    do { 
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
     try AVAudioSession.sharedInstance().setActive(true) 
    } 
    catch { 
     print("ERROR: \(error.localizedDescription)") 
    } 
} 

@IBAction func n2(_ sender: UIButton) { 

    do { 
     AudioPlayer = try AVAudioPlayer(contentsOf: alertSound) 
     AudioPlayer.prepareToPlay() 
     AudioPlayer.play() 
    } 
    catch { 
     print("ERROR: \(error.localizedDescription)") 
    } 
} 
関連する問題