質問編集履歴

1

コードが見えないので修正できたはず

2021/02/02 01:34

投稿

atusi828
atusi828

スコア7

test CHANGED
File without changes
test CHANGED
@@ -153,3 +153,101 @@
153
153
  }
154
154
 
155
155
  }
156
+
157
+
158
+
159
+ ```swift
160
+
161
+
162
+
163
+ コード
164
+
165
+ ```import UIKit
166
+
167
+ import AVFoundation
168
+
169
+
170
+
171
+ class ViewController: UIViewController {
172
+
173
+
174
+
175
+ var audioPlayer: AVAudioPlayer!
176
+
177
+
178
+
179
+ override func viewDidLoad() {
180
+
181
+ super.viewDidLoad()
182
+
183
+ // Do any additional setup after loading the view.
184
+
185
+ }
186
+
187
+
188
+
189
+ // 現在の日付を得る
190
+
191
+ let now = Date()
192
+
193
+ // タイマー起動時間を設定する
194
+
195
+ var calendar = Calendar(identifier: .gregorian)
196
+
197
+ calendar.timeZone = .current
198
+
199
+ calendar.locale = .current
200
+
201
+ let condition1 = calendar.date(from: DateComponents(year: 2021, month: 2, day: 11, hour: 9, minute: 00, second: 00))!
202
+
203
+
204
+
205
+ if now > codition1 {
206
+
207
+ playSound(name: "onsei")
208
+
209
+ }
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+ }
218
+
219
+
220
+
221
+ extension ViewController: AVAudioPlayerDelegate {
222
+
223
+ func playSound(name: String) {
224
+
225
+ guard let path = Bundle.main.path(forResource: name, ofType: "m4a") else {
226
+
227
+ print("音源ファイルが見つかりません")
228
+
229
+ return
230
+
231
+ }
232
+
233
+ do {
234
+
235
+ // AVAudioPlayerのインスタンス化
236
+
237
+ audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
238
+
239
+ // AVAudioPlayerのデリゲートをセット
240
+
241
+ audioPlayer.delegate = self
242
+
243
+ // 音声の再生
244
+
245
+ audioPlayer.play()
246
+
247
+ } catch {
248
+
249
+ }
250
+
251
+ }
252
+
253
+ }