質問編集履歴
2
ソースを対象の部分に限定しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
音源をカテゴリ別に分けてランダムに再生したい。
|
test
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
選択したカテゴリによって音源をグループ別けしたいと考えています。エラーは出ないのですが、何を選択しても最後のグループしか流れません。
|
6
|
-
|
7
|
-
本当はランダムではなく順番に流れるのが理想なのですが、それも判っていない初心者です。
|
8
6
|
|
9
7
|
### 該当のソースコード
|
10
8
|
|
@@ -12,382 +10,184 @@
|
|
12
10
|
|
13
11
|
|
14
12
|
|
15
|
-
import UIKit
|
16
|
-
|
17
|
-
import Foundation
|
18
|
-
|
19
|
-
import AudioToolbox
|
20
|
-
|
21
|
-
import AVFoundation
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@UIApplicationMain
|
26
|
-
|
27
|
-
class AppDelegate: UIResponder, UIApplicationDelegate, AVAudioPlayerDelegate, AlarmApplicationDelegate{
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
var window: UIWindow?
|
34
|
-
|
35
|
-
var audioPlayer: AVAudioPlayer?
|
36
|
-
|
37
|
-
l
|
13
|
+
//AlarmApplicationDelegateプロトコル
|
38
|
-
|
14
|
+
|
39
|
-
|
15
|
+
func playSound(_ soundName: String) {
|
16
|
+
|
17
|
+
|
18
|
+
|
40
|
-
|
19
|
+
//バイブ初期動作を設定する
|
20
|
+
|
41
|
-
|
21
|
+
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
22
|
+
|
42
|
-
|
23
|
+
//バイブのコールバックを設定する
|
24
|
+
|
25
|
+
AudioServicesAddSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate),nil,nil,
|
26
|
+
|
27
|
+
{ (_:SystemSoundID, _:UnsafeMutableRawPointer?) -> Void in
|
28
|
+
|
29
|
+
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
30
|
+
|
31
|
+
},nil)
|
32
|
+
|
33
|
+
//音源をランダムに選択
|
34
|
+
|
35
|
+
let url1 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body", ofType: "mp3")!)
|
36
|
+
|
37
|
+
let url2 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body-1", ofType: "mp3")!)
|
38
|
+
|
39
|
+
let url3 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body-2", ofType: "mp3")!)
|
40
|
+
|
41
|
+
let url4 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice", ofType: "mp3")!)
|
42
|
+
|
43
|
-
|
43
|
+
let url5 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-1", ofType: "mp3")!)
|
44
|
+
|
45
|
+
let url6 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-2", ofType: "mp3")!)
|
46
|
+
|
47
|
+
let url7 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-3", ofType: "mp3")!)
|
48
|
+
|
49
|
+
let url8 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-4", ofType: "mp3")!)
|
50
|
+
|
51
|
+
let url9 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-5", ofType: "mp3")!)
|
52
|
+
|
53
|
+
let url10 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face", ofType: "mp3")!)
|
54
|
+
|
55
|
+
let url11 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-1", ofType: "mp3")!)
|
56
|
+
|
57
|
+
let url12 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-2", ofType: "mp3")!)
|
58
|
+
|
59
|
+
let url13 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-3", ofType: "mp3")!)
|
60
|
+
|
61
|
+
let url14 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-4", ofType: "mp3")!)
|
62
|
+
|
63
|
+
let url15 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-5", ofType: "mp3")!)
|
64
|
+
|
65
|
+
|
44
66
|
|
45
67
|
var error: NSError?
|
46
68
|
|
69
|
+
//音源を追加するときは数値をあげる
|
70
|
+
|
47
71
|
do {
|
48
72
|
|
73
|
+
let rand = arc4random() % 3
|
74
|
+
|
75
|
+
let rand1 = arc4random() % 6
|
76
|
+
|
77
|
+
let rand2 = arc4random() % 6
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
switch rand1 {
|
82
|
+
|
83
|
+
case 1:
|
84
|
+
|
85
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url4)
|
86
|
+
|
87
|
+
case 2:
|
88
|
+
|
89
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url5)
|
90
|
+
|
91
|
+
case 3:
|
92
|
+
|
93
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url6)
|
94
|
+
|
95
|
+
case 4:
|
96
|
+
|
97
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url7)
|
98
|
+
|
99
|
+
case 5:
|
100
|
+
|
101
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url8)
|
102
|
+
|
103
|
+
default:
|
104
|
+
|
105
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url9)
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
switch rand2 {
|
112
|
+
|
113
|
+
case 1:
|
114
|
+
|
49
|
-
|
115
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url10)
|
116
|
+
|
50
|
-
|
117
|
+
case 2:
|
118
|
+
|
119
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url11)
|
120
|
+
|
121
|
+
case 3:
|
122
|
+
|
123
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url12)
|
124
|
+
|
125
|
+
case 4:
|
126
|
+
|
127
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url13)
|
128
|
+
|
129
|
+
case 5:
|
130
|
+
|
131
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url14)
|
132
|
+
|
133
|
+
default:
|
134
|
+
|
135
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url15)
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
switch rand {
|
142
|
+
|
143
|
+
case 1:
|
144
|
+
|
145
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url1)
|
146
|
+
|
147
|
+
case 2:
|
148
|
+
|
149
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url2)
|
150
|
+
|
151
|
+
default:
|
152
|
+
|
153
|
+
audioPlayer = try AVAudioPlayer(contentsOf: url3)
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
51
|
-
} catch let error1 as NSError{
|
159
|
+
} catch let error1 as NSError {
|
52
160
|
|
53
161
|
error = error1
|
54
162
|
|
55
|
-
|
163
|
+
audioPlayer = nil
|
56
164
|
|
57
165
|
}
|
58
166
|
|
59
|
-
|
167
|
+
|
60
|
-
|
61
|
-
|
168
|
+
|
62
|
-
|
63
|
-
} catch let error1 as NSError{
|
64
|
-
|
65
|
-
|
169
|
+
if let err = error {
|
66
|
-
|
170
|
+
|
67
|
-
print("
|
171
|
+
print("audioPlayer error (err.localizedDescription)")
|
172
|
+
|
173
|
+
return
|
174
|
+
|
175
|
+
} else {
|
176
|
+
|
177
|
+
audioPlayer!.delegate = self
|
178
|
+
|
179
|
+
audioPlayer!.prepareToPlay()
|
68
180
|
|
69
181
|
}
|
70
182
|
|
183
|
+
//負の数は無限のループ
|
184
|
+
|
71
|
-
|
185
|
+
audioPlayer!.numberOfLoops = 0
|
72
|
-
|
73
|
-
|
74
|
-
|
186
|
+
|
75
|
-
|
187
|
+
audioPlayer!.play()
|
76
188
|
|
77
189
|
}
|
78
190
|
|
79
|
-
|
80
|
-
|
81
|
-
//フォアグラウンドでアプリケーションを使用するときにローカル通知を受け取る
|
82
|
-
|
83
|
-
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
//警告ウィンドウを表示する
|
88
|
-
|
89
|
-
let storageController = UIAlertController(title: "ナマケモノめっ!", message: "目醒めるべきか 鍛えるか", preferredStyle: .alert)
|
90
|
-
|
91
|
-
var isSnooze: Bool = false
|
92
|
-
|
93
|
-
var soundName: String = ""
|
94
|
-
|
95
|
-
var index: Int = -1
|
96
|
-
|
97
|
-
if let userInfo = notification.userInfo {
|
98
|
-
|
99
|
-
isSnooze = userInfo["snooze"] as! Bool
|
100
|
-
|
101
|
-
soundName = userInfo["soundName"] as! String
|
102
|
-
|
103
|
-
index = userInfo["index"] as! Int
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
playSound(soundName)
|
110
|
-
|
111
|
-
//スヌーズのスケジュール通知
|
112
|
-
|
113
|
-
if isSnooze {
|
114
|
-
|
115
|
-
let snoozeOption = UIAlertAction(title: "鍛えるっ!", style: .default) {
|
116
|
-
|
117
|
-
(action:UIAlertAction)->Void in self.audioPlayer?.stop()
|
118
|
-
|
119
|
-
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate)
|
120
|
-
|
121
|
-
self.alarmScheduler.setNotificationForSnooze(snoozeMinute: 1, soundName: soundName, index: index)
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
storageController.addAction(snoozeOption)
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
let stopOption = UIAlertAction(title: "目醒めるっ!", style: .default) {
|
130
|
-
|
131
|
-
(action:UIAlertAction)->Void in self.audioPlayer?.stop()
|
132
|
-
|
133
|
-
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate)
|
134
|
-
|
135
|
-
self.alarmModel = Alarms()
|
136
|
-
|
137
|
-
self.alarmModel.alarms[index].onSnooze = false
|
138
|
-
|
139
|
-
//UIを変更する
|
140
|
-
|
141
|
-
var mainVC = self.window?.visibleViewController as? MainAlarmViewController
|
142
|
-
|
143
|
-
if mainVC == nil {
|
144
|
-
|
145
|
-
let storyboard = UIStoryboard(name: "Main", bundle: nil)
|
146
|
-
|
147
|
-
mainVC = storyboard.instantiateViewController(withIdentifier: "Alarm") as? MainAlarmViewController
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
mainVC!.changeSwitchButtonState(index: index)
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
storageController.addAction(stopOption)
|
158
|
-
|
159
|
-
window?.visibleViewController?.navigationController?.present(storageController, animated: true, completion: nil)
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
//アプリがバックグラウンドでスヌーズ通知するハンドラ
|
166
|
-
|
167
|
-
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
|
168
|
-
|
169
|
-
var index: Int = -1
|
170
|
-
|
171
|
-
var soundName: String = ""
|
172
|
-
|
173
|
-
if let userInfo = notification.userInfo {
|
174
|
-
|
175
|
-
soundName = userInfo["soundName"] as! String
|
176
|
-
|
177
|
-
index = userInfo["index"] as! Int
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
self.alarmModel = Alarms()
|
182
|
-
|
183
|
-
self.alarmModel.alarms[index].onSnooze = false
|
184
|
-
|
185
|
-
if identifier == Id.snoozeIdentifier {
|
186
|
-
|
187
|
-
alarmScheduler.setNotificationForSnooze(snoozeMinute: 1, soundName: soundName, index: index)
|
188
|
-
|
189
|
-
self.alarmModel.alarms[index].onSnooze = true
|
190
|
-
|
191
|
-
}
|
192
|
-
|
193
|
-
completionHandler()
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
//登録されたすべてのNSNotificationをデバッグ用に出力する
|
200
|
-
|
201
|
-
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
print(notificationSettings.types.rawValue)
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
//AlarmApplicationDelegateプロトコル
|
214
|
-
|
215
|
-
func playSound(_ soundName: String) {
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
//バイブ初期動作を設定する
|
220
|
-
|
221
|
-
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
222
|
-
|
223
|
-
//バイブのコールバックを設定する
|
224
|
-
|
225
|
-
AudioServicesAddSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate),nil,nil,
|
226
|
-
|
227
|
-
{ (_:SystemSoundID, _:UnsafeMutableRawPointer?) -> Void in
|
228
|
-
|
229
|
-
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
230
|
-
|
231
|
-
},nil)
|
232
|
-
|
233
|
-
//音源をランダムに選択
|
234
|
-
|
235
|
-
let url1 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body", ofType: "mp3")!)
|
236
|
-
|
237
|
-
let url2 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body-1", ofType: "mp3")!)
|
238
|
-
|
239
|
-
let url3 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body-2", ofType: "mp3")!)
|
240
|
-
|
241
|
-
let url4 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice", ofType: "mp3")!)
|
242
|
-
|
243
|
-
let url5 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-1", ofType: "mp3")!)
|
244
|
-
|
245
|
-
let url6 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-2", ofType: "mp3")!)
|
246
|
-
|
247
|
-
let url7 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-3", ofType: "mp3")!)
|
248
|
-
|
249
|
-
let url8 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-4", ofType: "mp3")!)
|
250
|
-
|
251
|
-
let url9 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-5", ofType: "mp3")!)
|
252
|
-
|
253
|
-
let url10 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face", ofType: "mp3")!)
|
254
|
-
|
255
|
-
let url11 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-1", ofType: "mp3")!)
|
256
|
-
|
257
|
-
let url12 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-2", ofType: "mp3")!)
|
258
|
-
|
259
|
-
let url13 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-3", ofType: "mp3")!)
|
260
|
-
|
261
|
-
let url14 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-4", ofType: "mp3")!)
|
262
|
-
|
263
|
-
let url15 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-5", ofType: "mp3")!)
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
var error: NSError?
|
268
|
-
|
269
|
-
//音源を追加するときは数値をあげる
|
270
|
-
|
271
|
-
do {
|
272
|
-
|
273
|
-
let rand = arc4random() % 3
|
274
|
-
|
275
|
-
let rand1 = arc4random() % 6
|
276
|
-
|
277
|
-
let rand2 = arc4random() % 6
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
switch rand1 {
|
282
|
-
|
283
|
-
case 1:
|
284
|
-
|
285
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url4)
|
286
|
-
|
287
|
-
case 2:
|
288
|
-
|
289
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url5)
|
290
|
-
|
291
|
-
case 3:
|
292
|
-
|
293
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url6)
|
294
|
-
|
295
|
-
case 4:
|
296
|
-
|
297
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url7)
|
298
|
-
|
299
|
-
case 5:
|
300
|
-
|
301
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url8)
|
302
|
-
|
303
|
-
default:
|
304
|
-
|
305
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url9)
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
switch rand2 {
|
312
|
-
|
313
|
-
case 1:
|
314
|
-
|
315
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url10)
|
316
|
-
|
317
|
-
case 2:
|
318
|
-
|
319
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url11)
|
320
|
-
|
321
|
-
case 3:
|
322
|
-
|
323
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url12)
|
324
|
-
|
325
|
-
case 4:
|
326
|
-
|
327
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url13)
|
328
|
-
|
329
|
-
case 5:
|
330
|
-
|
331
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url14)
|
332
|
-
|
333
|
-
default:
|
334
|
-
|
335
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url15)
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
switch rand {
|
342
|
-
|
343
|
-
case 1:
|
344
|
-
|
345
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url1)
|
346
|
-
|
347
|
-
case 2:
|
348
|
-
|
349
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url2)
|
350
|
-
|
351
|
-
default:
|
352
|
-
|
353
|
-
audioPlayer = try AVAudioPlayer(contentsOf: url3)
|
354
|
-
|
355
|
-
}
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
} catch let error1 as NSError {
|
360
|
-
|
361
|
-
error = error1
|
362
|
-
|
363
|
-
audioPlayer = nil
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
if let err = error {
|
370
|
-
|
371
|
-
print("audioPlayer error (err.localizedDescription)")
|
372
|
-
|
373
|
-
return
|
374
|
-
|
375
|
-
} else {
|
376
|
-
|
377
|
-
audioPlayer!.delegate = self
|
378
|
-
|
379
|
-
audioPlayer!.prepareToPlay()
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
//負の数は無限のループ
|
384
|
-
|
385
|
-
audioPlayer!.numberOfLoops = 0
|
386
|
-
|
387
|
-
audioPlayer!.play()
|
388
|
-
|
389
|
-
}
|
390
|
-
|
391
191
|
}
|
392
192
|
|
393
193
|
|
@@ -400,6 +200,6 @@
|
|
400
200
|
|
401
201
|
|
402
202
|
|
403
|
-
|
203
|
+
どうしても記述した最後のグループしか流れません。初心者なため質問の仕方もわからず本当に申し訳ありませんが、どなたかご指南いただければとてもありがたいです。
|
404
204
|
|
405
205
|
どうかよろしくお願いします。
|
1
swiftの記述を訂正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,9 +8,7 @@
|
|
8
8
|
|
9
9
|
### 該当のソースコード
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
```swift-言語名
|
14
12
|
|
15
13
|
|
16
14
|
|
@@ -394,7 +392,7 @@
|
|
394
392
|
|
395
393
|
|
396
394
|
|
397
|
-
|
395
|
+
```
|
398
396
|
|
399
397
|
|
400
398
|
|