Q&A
セルごとに異なる曲を再生したく、プロジェクトに4曲(sound01〜sound04)を保存して、以下のコードを書きました。
そして『Play』ボタンを押すと再生して『Pause』と表示され、『Pause』を押すとボタンが『Play』に変わるようにしたいのですが、以下のエラーが出ます。
エラー内容①:
The btnPlayPause outlet from the SecondViewController to the UIButton is invalid. Outlets cannot be connected to repeating content.
エラー内容②:
Use of unresolved identifier 'audioPath'
//ViewControllerのコード import UIKit import AVFoundation class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate { var player = AVAudioPlayer() let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"] let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"] var audioPlayer = AVAudioPlayer() let imageDescriptions = [ "イヌ", "ネコ", "イヌ", "イヌ" ] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. player = AVAudioPlayer(contentsOfURL: audioPath, error: nil) player.prepareToPlay() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBOutlet weak var btnPlayPause: UIButton! @IBAction func btnPlayPause(sender: UIButton) { if (player.playing) { player.pause() btnPlayPause.setTitle("Play", forState: UIControlState.Normal) } else { player.play() btnPlayPause.setTitle("Pause", forState: UIControlState.Normal) } } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return imageNames.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell1") as! CustomTableViewCell1 cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row]) cell.index = indexPath cell.delegate = self return cell } func selectCellButton(index: NSIndexPath) { let fileName = "sound\(index.row + 1)" print(fileName) do { let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3") let audioPath = NSURL(fileURLWithPath: filePath!) audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath) if audioPlayer.prepareToPlay() { audioPlayer.play() } } catch { print("Error") } } } //CustomTableViewCellのコード import UIKit protocol CustomTableViewCellDelegate: class { func selectCellButton(index: NSIndexPath) } class CustomTableViewCell1: UITableViewCell { @IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var myTitleLabel: UILabel! @IBOutlet weak var myDescriptionLabel: UILabel! @IBOutlet weak var btnPlayPause: UIButton! weak var delegate: CustomTableViewCellDelegate! var index: NSIndexPath! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func setCell(imageName: String, titleText: String, descriptionText: String) { myImageView.image = UIImage(named: imageName) myTitleLabel.text = titleText myDescriptionLabel.text = descriptionText } }
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。