ピアノの鍵盤を押すと音が出るというアプリを作成しており、あるページを参考にして以下のようなコードを書きました。
import UIKit
import AVFoundation
class ViewController: UIViewController {
let pianoKeys = ["C3", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "C4"] var audioPlayers:[AVAudioPlayer] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. for key in pianoKeys { let pianoSoundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound/\(key)", ofType: "mp3")!) __** let audioPlayer = AVAudioPlayer(contentsOfURL: pianoSoundURL, error: nil)**__ audioPlayer.prepareToPlay() audioPlayers.append(audioPlayer) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func playKey(sender: UIButton) { let keynum = sender.tag audioPlayers[keynum].currentTime = 0 audioPlayers[keynum].play() }
}
太字部分のコードにエラーが出てしまいます。エラーの内容は
Incorrect argument lable in call (have'contentsOfURL:'.error:'.expected'contentsOfURL:fileTypeHint:')
というものです。調べてみても解決策がわからず困っています。どなたか原因のわかる方、教えていただけると幸いです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/09 13:38
2015/11/09 14:32
2015/11/10 02:30
2015/11/10 03:02
2015/11/10 06:52
2015/11/10 07:23
2015/11/10 16:11