質問編集履歴
2
コードとタイトル
title
CHANGED
|
@@ -1,1 +1,1 @@
|
|
|
1
|
-
ボタン
|
|
1
|
+
table cellでは一つのボタンで再生と停止というのはできないのでしょうか?
|
body
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
セルごとに異なる曲を再生したく、プロジェクトに4曲(sound01〜sound04)を保存して、以下のコードを書きました。
|
|
2
|
-
そして『Play』ボタンを押すと再生
|
|
2
|
+
そして『Play』ボタンを押すと再生して『Pause』と表示され、『Pause』を押すとボタンが『Play』に変わるようにしたいのですが、以下のエラーが出ます。
|
|
3
|
-
また、どこにどのようなコードを記載したらいいか、教えて欲しいです。
|
|
4
3
|
|
|
4
|
+
エラー内容①:
|
|
5
|
+
The btnPlayPause outlet from the SecondViewController to the UIButton is invalid. Outlets cannot be connected to repeating content.
|
|
5
6
|
|
|
7
|
+
エラー内容②:
|
|
8
|
+
Use of unresolved identifier 'audioPath'
|
|
6
9
|
|
|
7
10
|
|
|
11
|
+
|
|
8
12
|
```ここに言語を入力
|
|
9
13
|
//ViewControllerのコード
|
|
10
14
|
import UIKit
|
|
11
15
|
import AVFoundation
|
|
12
16
|
|
|
13
17
|
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {
|
|
18
|
+
|
|
14
|
-
|
|
19
|
+
var player = AVAudioPlayer()
|
|
15
|
-
|
|
20
|
+
|
|
16
21
|
let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"]
|
|
17
22
|
|
|
18
23
|
let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"]
|
|
@@ -28,6 +33,12 @@
|
|
|
28
33
|
override func viewDidLoad() {
|
|
29
34
|
super.viewDidLoad()
|
|
30
35
|
// Do any additional setup after loading the view, typically from a nib.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
player = AVAudioPlayer(contentsOfURL: audioPath, error: nil)
|
|
39
|
+
player.prepareToPlay()
|
|
40
|
+
|
|
41
|
+
|
|
31
42
|
}
|
|
32
43
|
|
|
33
44
|
override func didReceiveMemoryWarning() {
|
|
@@ -35,6 +46,19 @@
|
|
|
35
46
|
// Dispose of any resources that can be recreated.
|
|
36
47
|
}
|
|
37
48
|
|
|
49
|
+
|
|
50
|
+
@IBOutlet weak var btnPlayPause: UIButton!
|
|
51
|
+
|
|
52
|
+
@IBAction func btnPlayPause(sender: UIButton) {
|
|
53
|
+
|
|
54
|
+
if (player.playing) {
|
|
55
|
+
player.pause()
|
|
56
|
+
btnPlayPause.setTitle("Play", forState: UIControlState.Normal)
|
|
57
|
+
} else {
|
|
58
|
+
player.play()
|
|
59
|
+
btnPlayPause.setTitle("Pause", forState: UIControlState.Normal)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
38
62
|
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
39
63
|
return imageNames.count
|
|
40
64
|
}
|
|
@@ -65,15 +89,15 @@
|
|
|
65
89
|
} catch {
|
|
66
90
|
print("Error")
|
|
67
91
|
}
|
|
68
|
-
}
|
|
92
|
+
}
|
|
69
93
|
}
|
|
94
|
+
|
|
95
|
+
|
|
70
96
|
|
|
71
|
-
|
|
72
|
-
|
|
73
97
|
//CustomTableViewCellのコード
|
|
74
98
|
|
|
75
99
|
import UIKit
|
|
76
|
-
|
|
100
|
+
|
|
77
101
|
protocol CustomTableViewCellDelegate: class {
|
|
78
102
|
func selectCellButton(index: NSIndexPath)
|
|
79
103
|
}
|
|
@@ -85,8 +109,9 @@
|
|
|
85
109
|
@IBOutlet weak var myTitleLabel: UILabel!
|
|
86
110
|
@IBOutlet weak var myDescriptionLabel: UILabel!
|
|
87
111
|
|
|
88
|
-
@IBOutlet weak var
|
|
112
|
+
@IBOutlet weak var btnPlayPause: UIButton!
|
|
89
113
|
|
|
114
|
+
|
|
90
115
|
weak var delegate: CustomTableViewCellDelegate!
|
|
91
116
|
var index: NSIndexPath!
|
|
92
117
|
|
|
@@ -106,10 +131,6 @@
|
|
|
106
131
|
myTitleLabel.text = titleText
|
|
107
132
|
myDescriptionLabel.text = descriptionText
|
|
108
133
|
}
|
|
109
|
-
|
|
110
|
-
@IBAction func tapButton(sender: AnyObject) {
|
|
111
|
-
delegate?.selectCellButton(index)
|
|
112
|
-
|
|
134
|
+
}
|
|
113
|
-
}
|
|
114
135
|
|
|
115
136
|
```
|
1
タイトル
title
CHANGED
|
@@ -1,1 +1,1 @@
|
|
|
1
|
-
|
|
1
|
+
ボタンひとつで音楽を再生・停止したい
|
body
CHANGED
|
File without changes
|