2016-05-20 48 views
2

我正在嘗試將聲音添加到我正在編寫的iOS應用程序中。我不斷收到錯誤消息將聲音添加到iOS應用程序

"Incorrect argument label in call (have 'contentsOfURL:error:', expected "contentsOfURL:fileTypeHint:')".

我試過幾種方式修補它,並且我無法將它構建。這裏是我的代碼:

import UIKit import AVFoundation 

class ViewController: UIViewController { 

    var Setup = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("DMSetup", ofType: "mp3")!) 
    var audioPlayer = AVAudioPlayer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     audioPlayer = AVAudioPlayer(contentsOfURL: Setup, error: nil) 
     audioPlayer.prepareToPlay() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func PlayKeySetup(sender: UIButton) { 
     audioPlayer.play() 
    } 


} 

有什麼建議嗎?我是這個新手,所以我肯定我錯過了一些明顯的東西。 xCode 7.3.1,OSX 10.11.4

謝謝。

回答

0

通常使用大寫的屬性命名它看作是一個態度不好,你應該使用setup

試試這個代碼:

let setupUrl = NSBundle.mainBundle().URLForResource("DMSetup", withExtension: "mp3") 
if (setupUrl == nil) { 
    print("Could not find file: DMSetup.mp3") 
    return 
} 
do { audioPlayer = try AVAudioPlayer(contentsOfURL: Setup!, fileTypeHint: nil) } 
catch let error as NSError { print(error.description) } 

if let player = audioPlayer { 
    player.prepareToPlay() 
} 
-1

試試這個代碼:

var audioPlayer: AVAudioPlayer! 

let path = NSBundle.mainBundle().pathForResource("yourfilename", ofType: "format") 

let CREATE_ANY_VARIABLE = NSURL(fileURLWithPath: path!) 

do { 
     try audioPlayer = AVAudioPlayer(contentsOfURL: CREATE_ANY_VARIABLE) 

} catch let error as NSError { 
     print(error.debugDescription) 
    } 


audioPlayer.prepareToPlay() 

,然後你可以在任何地方玩這個功能! 您的代碼中的問題是您的var的名稱安裝程序與AVFoundation的另一個安裝方法重疊

+0

嘗試此操作並最終導致更多錯誤。既然你說問題出在我的變量上,我把這個名字改成了不太可能與別的東西重疊的東西,然後再次嘗試我的原始代碼。現在我收到1條錯誤消息:「可以調用throw,但它沒有標記'嘗試',並且錯誤沒有被處理。」有什麼建議麼? –

相關問題