質問編集履歴

5

コード編集

2018/07/11 04:13

投稿

torkia
torkia

スコア24

test CHANGED
File without changes
test CHANGED
File without changes

4

コード記入

2018/07/11 04:13

投稿

torkia
torkia

スコア24

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,15 @@
2
2
 
3
3
 
4
4
 
5
- 具体的には、初期画面のテーブルビューのセルから画面遷移をし、遷移したページから8ページめくったところでクラッシュしてしまいます。その際のエラー箇所が audioPlayer.delegate=selfのところで、デバックエリアには fatal error: unexpectedly found nil while unwrapping an Optional value と出力されています。
5
+ 具体的には、初期画面のテーブルビューのセルから画面遷移をし、遷移したページからページをいくらかめくったところでクラッシュしてしまいます。
6
+
7
+
8
+
9
+ audioPlayer関連のコードを消せば、ページめくりをしてもエラーは出ません。
10
+
11
+ mp3のデータを各ページ少なくするとエラーはでません。
12
+
13
+ 約260コ分ぐらいののデータをページスクロールし続けたらクラッシュします。
6
14
 
7
15
 
8
16
 
@@ -10,12 +18,6 @@
10
18
 
11
19
  上記のことから、mp3の音データはちゃんと揃ってあります。
12
20
 
13
- mp3データはcsvファイルからファイル名を取得して呼び出すようにしています。
14
-
15
-
16
-
17
- また、audioPlayer関連のコードを消せば、ページめくりをしてもエラーは出ません。
18
-
19
21
 
20
22
 
21
23
 
@@ -32,6 +34,16 @@
32
34
 
33
35
  ```
34
36
 
37
+ 該当コード内で
38
+
39
+ audioPlayers.append(audioPlayer)のところで[Thread1: EXC BAD INSTRUCTION]
40
+
41
+
42
+
43
+ デバックエリアの出力では
44
+
45
+ Error The operation couldn\U2019t be completed. (NSOSStatusErrorDomain error -42.)
46
+
35
47
  fatal error: unexpectedly found nil while unwrapping an Optional value
36
48
 
37
49
 
@@ -48,8 +60,6 @@
48
60
 
49
61
  [使用バージョン Swift3]
50
62
 
51
-
52
-
53
63
  import UIKit
54
64
 
55
65
  import AVFoundation
@@ -58,265 +68,445 @@
58
68
 
59
69
  class PageContentViewController: UIViewController, UIScrollViewDelegate, AVAudioPlayerDelegate {
60
70
 
61
-
62
-
63
- // 各ページのindex
71
+
64
-
72
+
65
- var pageIndex:Int = 0
73
+ let soundNameArray = [
74
+
66
-
75
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 1
76
+
67
-
77
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 2
78
+
68
-
79
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 3
80
+
81
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 4
82
+
83
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 5
84
+
85
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 6
86
+
87
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 7
88
+
89
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 8
90
+
91
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 9
92
+
93
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 10
94
+
95
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 11
96
+
97
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 12
98
+
99
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 13
100
+
101
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 14
102
+
103
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 15
104
+
105
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 16
106
+
107
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 17
108
+
109
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 18
110
+
111
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"], // 19
112
+
113
+ ["1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"] // 20
114
+
69
-    ・
115
+ ]
70
-
71
-    ・
116
+
72
-
73
- (略
117
+ // 1つの配列に16コずつだと16ページ目でクラッシュ(17ページ目を呼ぶのにエラーがでている
74
-
118
+
119
+
120
+
75
-    ・
121
+ var pageIndex:Int = 0 // 各ページのindex
76
-
122
+
77
-    ・
123
+ let indexPageLabel = UILabel() // index確認用ラベル
78
-
79
-
80
-
124
+
125
+
126
+
81
- // sound関係
127
+ // sound関係
82
-
128
+
83
- var soundArray:[String?] = [] // csvファイルからファイル名のデータを格納する
129
+ var soundArray:[String] = [] // ファイル名のデータを格納する
84
-
130
+
85
- var audioUrls:[URL?] = [] // soundArrayの要素からパスとurlを作成し格納する
131
+ var audioUrls:[URL] = [] // soundArrayの要素からパスとurlを作成し格納する
86
132
 
87
133
  var audioPlayer:AVAudioPlayer! // urlから音声データにして格納する
88
134
 
89
- var audioPlayers:[AVAudioPlayer?] = [] // 音声データを格納する
135
+ var audioPlayers:[AVAudioPlayer] = [] // 音声データを格納する
90
136
 
91
137
  var soundButtons = [UIButton]() // 音声を鳴らすボタンを格納する
92
138
 
93
-
94
-
95
-
139
+
140
+
141
+
96
142
 
97
143
  override func viewDidLoad() {
98
144
 
99
145
  super.viewDidLoad()
100
146
 
101
-
102
-
103
-    ・
147
+
104
-
105
-    ・
148
+
106
-
107
- (略)
108
-
109
-    ・
110
-
111
-    ・
112
-
113
-
114
-
115
- //CSVファイ名を引数にしてCSVファイルを読み込む
149
+ // indexPageラベ
116
-
150
+
117
- let csvSoundArray = loadCSV("sound201604")
151
+ indexPageLabel.frame = CGRect(x: 20, y: 10, width: 240, height: 20)
118
-
119
-
120
-
121
- //タップされたセルのindexでcsvSoundArrayの行を取得して配列に格納する
152
+
122
-
123
- soundArray = csvSoundArray[pageIndex].components(separatedBy: ";")
153
+ indexPageLabel.text = "pageIndex: (pageIndex) ページNo: ((pageIndex) + 1)"
154
+
124
-
155
+ indexPageLabel.backgroundColor = .yellow
156
+
157
+ self.view.addSubview(indexPageLabel)
158
+
159
+
160
+
161
+
162
+
125
- print("soundArray: (soundArray)")
163
+ soundArray = soundNameArray[pageIndex]
126
-
127
-
128
-
129
-
130
-
164
+
165
+
166
+
131
- // soundArrayの要素すべてを取り出してpathとurlをaudioUrls配列に格納する
167
+ // soundArrayの要素すべてを取り出してaudioPlayers配列に格納する
132
168
 
133
169
  for i in soundArray {
134
170
 
135
- let audioPath = Bundle.main.path(forResource: "(i!)", ofType:"mp3")!
171
+ let audioPath = Bundle.main.path(forResource: "(i)", ofType:"mp3")!
136
172
 
137
173
  let audioUrl = URL(fileURLWithPath: audioPath)
138
174
 
139
- audioUrls.append(audioUrl)
140
-
141
- }
142
-
143
-
144
-
145
-
146
-
147
- // audioUrlsの要素すべてを取り出してエラーチェックをし、audioPlayers配列に格納する
148
-
149
- for i in audioUrls {
150
-
151
- // auido を再生するプレイヤーを作成する
152
-
153
- var audioError:NSError?
154
-
155
175
  do {
156
176
 
157
- audioPlayer = try AVAudioPlayer(contentsOf: i!)
177
+ audioPlayer = try AVAudioPlayer(contentsOf: audioUrl)
158
-
159
- } catch let error as NSError {
178
+
160
-
161
- audioError = error
162
-
163
- audioPlayer = nil
179
+ audioPlayers.append(audioPlayer)
180
+
181
+ } catch {
182
+
183
+ print("エラー")
164
184
 
165
185
  }
166
186
 
187
+ }
188
+
189
+
190
+
191
+ audioPlayer.delegate = self
192
+
193
+ audioPlayer.prepareToPlay()
194
+
195
+
196
+
197
+
198
+
199
+ // サウンドボタンを量産 button
200
+
201
+ for index in 0..<soundArray.count {
202
+
203
+ let soundButton = UIButton()
204
+
205
+ soundButton.frame = CGRect(x: 20, y: 40 + (index * 30), width: 40, height: 30)
206
+
207
+ soundButton.backgroundColor = UIColor.lightGray
208
+
209
+ soundButton.titleLabel?.font = UIFont.systemFont(ofSize: 10)
210
+
211
+ soundButton.setTitle("▶", for: .normal)
212
+
213
+ soundButton.setTitleColor(UIColor.darkGray, for: .normal)
214
+
215
+ soundButton.tag = index // ボタン識別用ID
216
+
217
+ soundButton.addTarget(self, action: #selector(buttonEventSound(sender:)), for: .touchUpInside)
218
+
219
+ soundButtons.append(soundButton)
220
+
221
+ self.view.addSubview(soundButton)
222
+
223
+ }
224
+
225
+
226
+
227
+ } //viewDidLoadを閉じる
228
+
229
+
230
+
231
+
232
+
167
- // エラーが起きたとき
233
+ // ボタンイベント音再生
234
+
168
-
235
+ func buttonEventSound(sender: UIButton) {
236
+
237
+ let index = sender.tag
238
+
239
+ if (audioPlayers[index].isPlaying){
240
+
241
+ audioPlayers[index].stop()
242
+
243
+ audioPlayers[index].currentTime = 0
244
+
245
+ }
246
+
247
+ else{
248
+
169
- if let error = audioError {
249
+ for i in audioPlayers {
170
-
250
+
171
- print("Error (error.localizedDescription)")
251
+ if (i.isPlaying) {
252
+
253
+ i.stop()
254
+
255
+ i.currentTime = 0
256
+
257
+ }
172
258
 
173
259
  }
174
260
 
175
- audioPlayers.append(audioPlayer)
176
-
177
- }
178
-
179
-
180
-
181
- audioPlayer.delegate = self
182
-
183
- audioPlayer.prepareToPlay()
184
-
185
-
186
-
187
-
188
-
189
- // サウンドボタンを作成
190
-
191
- for index in 0..<soundArray.count {
192
-
193
- let soundButton = UIButton()
194
-
195
- soundButton.frame = CGRect(x: soundButtonsX, y: soundButtonsY , width: soundButtonsW, height: soundButtonsH)
196
-
197
- soundButton.titleLabel?.font = UIFont.systemFont(ofSize: 10)
198
-
199
- soundButton.setTitle("▶", for: .normal)
200
-
201
- soundButton.setTitleColor(UIColor.darkGray, for: .normal)
202
-
203
- soundButton.setTitle("▶", for: .highlighted)
204
-
205
- soundButton.setTitleColor(UIColor.magenta, for: .highlighted)
206
-
207
- soundButton.tag = index // ボタン識別用ID
208
-
209
- soundButton.addTarget(self, action: #selector(buttonEventSound(sender:)), for: .touchUpInside)
210
-
211
- soundButtons.append(soundButton)
212
-
213
- scrollView.addSubview(soundButton)
214
-
215
- }
216
-
217
-
218
-
219
- // 音声のないサウンドボタンは表示させない
220
-
221
- for i in 0..<soundArray.count {
222
-
223
- if soundArray[i] == "" {
224
-
225
- soundButtons[i].isHidden = true
226
-
227
- } else {
228
-
229
- soundButtons[i].isHidden = false
261
+ audioPlayers[index].play()
262
+
263
+ }
264
+
265
+ }
266
+
267
+
268
+
269
+ // 音楽再生が成功した時に呼ばれるメソッド
270
+
271
+ func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
272
+
273
+ print("音終了")
274
+
275
+ }
276
+
277
+
278
+
279
+ // デコード中にエラーが起きた時に呼ばれるメソッド
280
+
281
+ func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
282
+
283
+ print("デコードエラー")
284
+
285
+ }
286
+
287
+
288
+
289
+
290
+
291
+ override func didReceiveMemoryWarning() {
292
+
293
+ super.didReceiveMemoryWarning()
294
+
295
+ }
296
+
297
+
298
+
299
+ }
300
+
301
+ ```
302
+
303
+ ### その他のソースコード
304
+
305
+ ```
306
+
307
+ import UIKit
308
+
309
+
310
+
311
+ class PageViewController: UIPageViewController, UIPageViewControllerDataSource {
312
+
313
+
314
+
315
+ var selectedIndex: Int = 0 // タップされたセルのindex
316
+
317
+ var pageIndex:Int = 0 // 各ページに割り当てたindex
318
+
319
+ var contentVCs = [UIViewController]() // ページングするviewControllerを格納する配列
320
+
321
+
322
+
323
+ override func viewDidLoad() {
324
+
325
+ super.viewDidLoad()
326
+
327
+
328
+
329
+ // ナビゲーションバーの透過を無効にする。
330
+
331
+ self.navigationController!.navigationBar.isTranslucent = false
332
+
333
+
334
+
335
+ dataSource = self
336
+
337
+
338
+
339
+ for index in 0..<20 {
340
+
341
+ let contentVC = storyboard?.instantiateViewController(withIdentifier: "PageContentViewController") as! PageContentViewController
342
+
343
+ contentVC.pageIndex = index
344
+
345
+ contentVCs.append(contentVC)
346
+
347
+ }
348
+
349
+
350
+
351
+ self.setViewControllers([contentVCs[selectedIndex]], direction: .forward, animated: true, completion: nil)
352
+
353
+
354
+
355
+ } // viewDidLoad()を閉じる
356
+
357
+
358
+
359
+
360
+
361
+ // MARK: - UIPageViewControllerDataSource
362
+
363
+ // スワイプでページを戻る(Before)
364
+
365
+ func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
366
+
367
+ guard let index = contentVCs.index(of: viewController as! PageContentViewController), index > 0 else {
368
+
369
+ return nil
370
+
371
+ }
372
+
373
+ let previousVC = contentVCs[index - 1]
374
+
375
+ return previousVC
376
+
377
+ }
378
+
379
+
380
+
381
+ // スワイプでページを進む(After)
382
+
383
+ func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
384
+
385
+ guard let index = contentVCs.index(of: viewController as! PageContentViewController), index < contentVCs.count - 1 else {
386
+
387
+ return nil
388
+
389
+ }
390
+
391
+ let nextVC = contentVCs[index + 1]
392
+
393
+ return nextVC
394
+
395
+ }
396
+
397
+ }
398
+
399
+ ```
400
+
401
+ ```
402
+
403
+ import UIKit
404
+
405
+
406
+
407
+ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
408
+
409
+
410
+
411
+ let sectionTitle = ["Title"] //セクションに表示するデータ
412
+
413
+ let section0 = Array(1...20) //セルに表示するデータ
414
+
415
+
416
+
417
+ @IBOutlet weak var tableView: UITableView!
418
+
419
+
420
+
421
+ override func viewDidLoad() {
422
+
423
+ super.viewDidLoad()
424
+
425
+
426
+
427
+ tableView.delegate = self
428
+
429
+ tableView.dataSource = self
430
+
431
+ }
432
+
433
+
434
+
435
+ override func didReceiveMemoryWarning() {
436
+
437
+ super.didReceiveMemoryWarning()
438
+
439
+ }
440
+
441
+
442
+
443
+ func numberOfSections(in tableView: UITableView) -> Int {
444
+
445
+ return sectionTitle.count
446
+
447
+ }
448
+
449
+
450
+
451
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
452
+
453
+ return section0.count
454
+
455
+ }
456
+
457
+
458
+
459
+ func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
460
+
461
+ return sectionTitle[section]
462
+
463
+ }
464
+
465
+
466
+
467
+ func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
468
+
469
+ return 30
470
+
471
+ }
472
+
473
+
474
+
475
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
476
+
477
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
478
+
479
+ cell.textLabel?.text = "(section0[indexPath.row])"
480
+
481
+ return cell
482
+
483
+ }
484
+
485
+
486
+
487
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
488
+
489
+ if segue.identifier == "ToPageViewController" {
490
+
491
+ if let indexPath = self.tableView.indexPathForSelectedRow {
492
+
493
+ tableView.deselectRow(at: indexPath, animated: false)
494
+
495
+ if let pageViewController = segue.destination as? PageViewController {
496
+
497
+ pageViewController.selectedIndex = indexPath.row
498
+
499
+ }
230
500
 
231
501
  }
232
502
 
233
503
  }
234
504
 
235
-
236
-
237
- } //viewDidLoadを閉じる
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
- // ボタンイベント音再生
246
-
247
- func buttonEventSound(sender: UIButton) {
248
-
249
-
250
-
251
- let index = sender.tag
252
-
253
-
254
-
255
- if (audioPlayers[index]?.isPlaying)!{
256
-
257
- audioPlayers[index]?.stop()
258
-
259
- audioPlayers[index]?.currentTime = 0
260
-
261
- }
505
+ }
262
-
263
- else{
506
+
264
-
265
- for i in audioPlayers {
507
+
266
-
267
- if (i?.isPlaying)! {
508
+
268
-
269
- i?.stop()
270
-
271
- i?.currentTime = 0
272
-
273
- }
509
+ }
274
-
275
- }
276
-
277
- audioPlayers[index]?.play()
278
-
279
- }
280
-
281
- }
282
-
283
-
284
-
285
- // 音楽再生が成功した時に呼ばれるメソッド
286
-
287
- func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
288
-
289
- print("Music Finish")
290
-
291
- }
292
-
293
-
294
-
295
- // デコード中にエラーが起きた時に呼ばれるメソッド
296
-
297
- func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
298
-
299
- print("Error")
300
-
301
- }
302
-
303
-
304
-
305
-    ・
306
-
307
-    ・
308
-
309
- (略)
310
-
311
-    ・
312
-
313
-    ・
314
-
315
-
316
-
317
- } //classを閉じる
318
-
319
-
320
510
 
321
511
  ```
322
512
 
@@ -326,9 +516,9 @@
326
516
 
327
517
 
328
518
 
329
- クラッシュするページで、csvファイルからのデータ、urlまでは取得できているのですが、audioPlayerのところからnilになり(エラーチェックでエラーがあるならnilが入るようにしている)、デバックエリアに[Error The operation couldn\U2019t be completed. (NSOSStatusErrorDomain error -42.)] と出力されます。
330
-
331
- エラーチェックでnilが入るコード関連を消すと、9ページめくったところでクラッシュし、Appdelegateのクラス宣言 signal SIGABLT のエラーがでます。デバックエリアには [libc++abi.dylib: terminating with uncaught exception of type NSException] のメッセージが出力されます。
519
+ クラッシュするページでurlまでは取得できているのですが、audioPlayerのところからnilになり(エラーチェックでエラーがあるならnilが入るようにしている)、デバックエリアに [Error The operation couldn\U2019t be completed. (NSOSStatusErrorDomain error -42.)] と出力されます。
520
+
521
+ エラーチェックでnilが入るコード関連を消すと、1ページ分多くめくったところで、AppDelegateのクラス宣言ところに [Thread1: signal SIGABLT] と表示され、デバックエリアには [libc++abi.dylib: terminating with uncaught exception of type NSException] 出力されています。
332
522
 
333
523
 
334
524
 

3

タイトル

2018/07/11 03:40

投稿

torkia
torkia

スコア24

test CHANGED
@@ -1 +1 @@
1
- Swift audioPlayerのデリゲのエ
1
+ Swift AVAudioPlayerを導入するとペジスクロールッシュする
test CHANGED
@@ -1,4 +1,4 @@
1
- mp3データの音の再生にAVAudioPlayerDelegateを使用しているのですが、ページをめくって画面遷移していると途中でクラッシュしてしまいます。
1
+ mp3データの音の再生にAVAudioPlayerを使用しているのですが、ページをめくって画面遷移していると途中でクラッシュしてしまいます。
2
2
 
3
3
 
4
4
 

2

コード記述

2018/07/06 12:47

投稿

torkia
torkia

スコア24

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,3 @@
1
- ### 前提・実現したいこと
2
-
3
-
4
-
5
1
  mp3データの音の再生にAVAudioPlayerDelegateを使用しているのですが、ページをめくって画面遷移していると途中でクラッシュしてしまいます。
6
2
 
7
3
 
@@ -286,6 +282,26 @@
286
282
 
287
283
 
288
284
 
285
+ // 音楽再生が成功した時に呼ばれるメソッド
286
+
287
+ func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
288
+
289
+ print("Music Finish")
290
+
291
+ }
292
+
293
+
294
+
295
+ // デコード中にエラーが起きた時に呼ばれるメソッド
296
+
297
+ func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
298
+
299
+ print("Error")
300
+
301
+ }
302
+
303
+
304
+
289
305
     ・
290
306
 
291
307
     ・

1

コード記述

2018/07/05 10:05

投稿

torkia
torkia

スコア24

test CHANGED
File without changes
test CHANGED
@@ -280,8 +280,6 @@
280
280
 
281
281
  audioPlayers[index]?.play()
282
282
 
283
- currentPlayer = audioPlayers[index]
284
-
285
283
  }
286
284
 
287
285
  }