質問編集履歴
1
コードをみやすくするために縮小
test
CHANGED
File without changes
|
test
CHANGED
@@ -168,137 +168,7 @@
|
|
168
168
|
|
169
169
|
|
170
170
|
|
171
|
-
|
171
|
+
|
172
|
-
|
173
|
-
{
|
174
|
-
|
175
|
-
if (sender).isOn
|
176
|
-
|
177
|
-
{
|
178
|
-
|
179
|
-
timer = Timer.scheduledTimer(withTimeInterval: 1 * 1, repeats: false, block: { timer in
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
self.audioPlayer.play()
|
184
|
-
|
185
|
-
self.audioPlayer.numberOfLoops = -1
|
186
|
-
|
187
|
-
print(self.audioPlayer.isPlaying)
|
188
|
-
|
189
|
-
})
|
190
|
-
|
191
|
-
}else{
|
192
|
-
|
193
|
-
timer.invalidate()
|
194
|
-
|
195
|
-
print("switch3stopped")
|
196
|
-
|
197
|
-
self.audioPlayer.stop()
|
198
|
-
|
199
|
-
}
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
@IBAction func switch4(_ sender: UISwitch)
|
206
|
-
|
207
|
-
{
|
208
|
-
|
209
|
-
if (sender).isOn
|
210
|
-
|
211
|
-
{
|
212
|
-
|
213
|
-
timer = Timer.scheduledTimer(withTimeInterval: 1 * 1, repeats: false, block: { timer in
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
self.audioPlayer.play()
|
218
|
-
|
219
|
-
self.audioPlayer.numberOfLoops = -1
|
220
|
-
|
221
|
-
print(self.audioPlayer.isPlaying)
|
222
|
-
|
223
|
-
})
|
224
|
-
|
225
|
-
}else{
|
226
|
-
|
227
|
-
timer.invalidate()
|
228
|
-
|
229
|
-
print("switch4stopped")
|
230
|
-
|
231
|
-
self.audioPlayer.stop()
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
}
|
236
|
-
|
237
|
-
@IBAction func switch5(_ sender: UISwitch)
|
238
|
-
|
239
|
-
{
|
240
|
-
|
241
|
-
if (sender).isOn
|
242
|
-
|
243
|
-
{
|
244
|
-
|
245
|
-
timer = Timer.scheduledTimer(withTimeInterval: 1 * 1, repeats: false, block: { timer in
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
self.audioPlayer.play()
|
250
|
-
|
251
|
-
self.audioPlayer.numberOfLoops = -1
|
252
|
-
|
253
|
-
print(self.audioPlayer.isPlaying)
|
254
|
-
|
255
|
-
})
|
256
|
-
|
257
|
-
}else{
|
258
|
-
|
259
|
-
timer.invalidate()
|
260
|
-
|
261
|
-
print("switch5stopped")
|
262
|
-
|
263
|
-
self.audioPlayer.stop()
|
264
|
-
|
265
|
-
}
|
266
|
-
|
267
|
-
}
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
@IBAction func switch6(_ sender: UISwitch)
|
272
|
-
|
273
|
-
{
|
274
|
-
|
275
|
-
if (sender).isOn
|
276
|
-
|
277
|
-
{
|
278
|
-
|
279
|
-
timer = Timer.scheduledTimer(withTimeInterval: 1 * 1, repeats: false, block: { timer in
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
self.audioPlayer.play()
|
284
|
-
|
285
|
-
self.audioPlayer.numberOfLoops = -1
|
286
|
-
|
287
|
-
print(self.audioPlayer.isPlaying)
|
288
|
-
|
289
|
-
})
|
290
|
-
|
291
|
-
}else{
|
292
|
-
|
293
|
-
timer.invalidate()
|
294
|
-
|
295
|
-
print("switch6stopped")
|
296
|
-
|
297
|
-
self.audioPlayer.stop()
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
}
|
302
172
|
|
303
173
|
override func viewDidLoad() {
|
304
174
|
|
@@ -310,117 +180,7 @@
|
|
310
180
|
|
311
181
|
|
312
182
|
|
313
|
-
|
183
|
+
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
if let url=Bundle.main.url(forResource:"2",withExtension:".mp3" ){
|
318
|
-
|
319
|
-
//上記の***の箇所でファイル名と拡張子を指定し変数urlに代入します。
|
320
|
-
|
321
|
-
do {
|
322
|
-
|
323
|
-
audioPlayer = try AVAudioPlayer(contentsOf:url)
|
324
|
-
|
325
|
-
audioPlayer?.play(atTime:1 * 10)
|
326
|
-
|
327
|
-
//処理が成功したらaudioPlayerに指定したurlを代入し、playメソッドを呼び出します。
|
328
|
-
|
329
|
-
}catch{
|
330
|
-
|
331
|
-
audioPlayer = nil
|
332
|
-
|
333
|
-
}
|
334
|
-
|
335
|
-
}else{
|
336
|
-
|
337
|
-
fatalError("Url is nil")
|
338
|
-
|
339
|
-
//URLが空の場合など処理が走らなかった場合はエラー文を表示します。
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
audioPlayer.delegate = self
|
344
|
-
|
345
|
-
audioPlayer.prepareToPlay()
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
/// バックグラウンドでも再生できるカテゴリに設定する
|
350
|
-
|
351
|
-
let session = AVAudioSession.sharedInstance()
|
352
|
-
|
353
|
-
do {
|
354
|
-
|
355
|
-
try session.setCategory(AVAudioSessionCategoryPlayback)
|
356
|
-
|
357
|
-
} catch {
|
358
|
-
|
359
|
-
// エラー処理
|
360
|
-
|
361
|
-
fatalError("カテゴリ設定失敗")
|
362
|
-
|
363
|
-
}
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
// sessionのアクティブ化
|
368
|
-
|
369
|
-
do {
|
370
|
-
|
371
|
-
try session.setActive(true)
|
372
|
-
|
373
|
-
} catch {
|
374
|
-
|
375
|
-
// audio session有効化失敗時の処理
|
376
|
-
|
377
|
-
// (ここではエラーとして停止している)
|
378
|
-
|
379
|
-
fatalError("session有効化失敗")
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
}
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
override func didReceiveMemoryWarning() {
|
390
|
-
|
391
|
-
super.didReceiveMemoryWarning()
|
392
|
-
|
393
|
-
// Dispose of any resources that can be recreated.
|
394
|
-
|
395
|
-
}
|
396
|
-
|
397
|
-
//追加③ セルの個数を指定するデリゲートメソッド(必須)
|
398
|
-
|
399
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
400
|
-
|
401
|
-
return TODO.count
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
//追加④ セルに値を設定するデータソースメソッド(必須)
|
408
|
-
|
409
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
410
|
-
|
411
|
-
// セルを取得する
|
412
|
-
|
413
|
-
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
414
|
-
|
415
|
-
// セルに表示する値を設定する
|
416
|
-
|
417
|
-
cell.textLabel!.text = TODO[indexPath.row]
|
418
|
-
|
419
|
-
return cell
|
420
|
-
|
421
|
-
}
|
422
|
-
|
423
|
-
}
|
424
184
|
|
425
185
|
|
426
186
|
|