質問編集履歴

3

delegate周りを修正いたしました。

2020/07/22 05:10

投稿

ibuibu
ibuibu

スコア3

test CHANGED
File without changes
test CHANGED
@@ -96,7 +96,7 @@
96
96
 
97
97
 
98
98
 
99
- 修正
99
+ 解決
100
100
 
101
101
  ```SwiftUI
102
102
 
@@ -112,15 +112,15 @@
112
112
 
113
113
  struct ContentView: View {
114
114
 
115
- var audioPlayer = avp() // 追記
115
+ var audioPlayer = avp() // 追記
116
116
 
117
-
117
+
118
118
 
119
119
  var body: some View {
120
120
 
121
121
  Button(action: {
122
122
 
123
- self.audioPlayer.playSound() // 修正
123
+ self.audioPlayer.playSound() //修正
124
124
 
125
125
  }) {
126
126
 
@@ -136,23 +136,25 @@
136
136
 
137
137
  class avp:NSObject, AVAudioPlayerDelegate {
138
138
 
139
+
139
140
 
140
-
141
- func playSound() { // 修正
141
+ func playSound() {
142
142
 
143
143
  let soundURL = Bundle.main.url(forResource: "C4_T100", withExtension: "mp3")
144
144
 
145
-
145
+
146
146
 
147
147
  do {
148
148
 
149
149
  player = try AVAudioPlayer(contentsOf: soundURL!)
150
150
 
151
- player?.volume = 10
151
+ player?.delegate = self // 追記
152
152
 
153
- player?.prepareToPlay() // 即時再生させる
153
+ player?.volume = 10
154
154
 
155
+ player?.prepareToPlay() // 即時再生させる
156
+
155
- player?.play() // BGMを鳴らす
157
+ player?.play() // BGMを鳴らす
156
158
 
157
159
  } catch {
158
160
 
@@ -162,11 +164,11 @@
162
164
 
163
165
  }
164
166
 
165
-
167
+
166
168
 
167
169
  // 音楽再生が成功した時に呼ばれるメソッド
168
170
 
169
- private static func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
171
+ func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { // 修正
170
172
 
171
173
  print("再生終了")
172
174
 
@@ -180,6 +182,10 @@
180
182
 
181
183
 
182
184
 
185
+
186
+
187
+
188
+
183
189
  ### 補足情報
184
190
 
185
191
  Xcode(11.5)

2

iinternal → private staticに修正

2020/07/22 05:10

投稿

ibuibu
ibuibu

スコア3

test CHANGED
File without changes
test CHANGED
@@ -114,7 +114,7 @@
114
114
 
115
115
  var audioPlayer = avp() // 追記
116
116
 
117
-
117
+
118
118
 
119
119
  var body: some View {
120
120
 
@@ -136,13 +136,13 @@
136
136
 
137
137
  class avp:NSObject, AVAudioPlayerDelegate {
138
138
 
139
-
139
+
140
140
 
141
141
  func playSound() { // 修正
142
142
 
143
143
  let soundURL = Bundle.main.url(forResource: "C4_T100", withExtension: "mp3")
144
144
 
145
-
145
+
146
146
 
147
147
  do {
148
148
 
@@ -162,11 +162,11 @@
162
162
 
163
163
  }
164
164
 
165
-
165
+
166
166
 
167
167
  // 音楽再生が成功した時に呼ばれるメソッド
168
168
 
169
- internal func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
169
+ private static func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
170
170
 
171
171
  print("再生終了")
172
172
 

1

ご返事ありあがとうございます。ご指摘の通りsataticを排除しインスタンスを生成してplaySound()を呼ぶようにしました。

2020/07/22 02:57

投稿

ibuibu
ibuibu

スコア3

test CHANGED
File without changes
test CHANGED
@@ -13,6 +13,8 @@
13
13
  ### 該当のソースコード
14
14
 
15
15
 
16
+
17
+ ・修正前
16
18
 
17
19
  ```SwiftUI
18
20
 
@@ -94,6 +96,88 @@
94
96
 
95
97
 
96
98
 
99
+ ・修正後
100
+
101
+ ```SwiftUI
102
+
103
+ import SwiftUI
104
+
105
+ import AVFoundation
106
+
107
+
108
+
109
+ var player: AVAudioPlayer?
110
+
111
+
112
+
113
+ struct ContentView: View {
114
+
115
+ var audioPlayer = avp() // 追記
116
+
117
+
118
+
119
+ var body: some View {
120
+
121
+ Button(action: {
122
+
123
+ self.audioPlayer.playSound() // 修正
124
+
125
+ }) {
126
+
127
+ Text("再生")
128
+
129
+ }
130
+
131
+ }
132
+
133
+ }
134
+
135
+
136
+
137
+ class avp:NSObject, AVAudioPlayerDelegate {
138
+
139
+
140
+
141
+ func playSound() { // 修正
142
+
143
+ let soundURL = Bundle.main.url(forResource: "C4_T100", withExtension: "mp3")
144
+
145
+
146
+
147
+ do {
148
+
149
+ player = try AVAudioPlayer(contentsOf: soundURL!)
150
+
151
+ player?.volume = 10
152
+
153
+ player?.prepareToPlay() // 即時再生させる
154
+
155
+ player?.play() // BGMを鳴らす
156
+
157
+ } catch {
158
+
159
+ print("error...")
160
+
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+ // 音楽再生が成功した時に呼ばれるメソッド
168
+
169
+ internal func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
170
+
171
+ print("再生終了")
172
+
173
+ }
174
+
175
+ }
176
+
177
+ ```
178
+
179
+
180
+
97
181
 
98
182
 
99
183
  ### 補足情報