前提・実現したいこと
選択したカテゴリによって音源をグループ別けしたいと考えています。エラーは出ないのですが、何を選択しても最後のグループしか流れません。
該当のソースコード
swift
1 2 //AlarmApplicationDelegateプロトコル 3 func playSound(_ soundName: String) { 4 5 //バイブ初期動作を設定する 6 AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) 7 //バイブのコールバックを設定する 8 AudioServicesAddSystemSoundCompletion(SystemSoundID(kSystemSoundID_Vibrate),nil,nil, 9 { (_:SystemSoundID, _:UnsafeMutableRawPointer?) -> Void in 10 AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) 11 },nil) 12 //音源をランダムに選択 13 let url1 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body", ofType: "mp3")!) 14 let url2 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body-1", ofType: "mp3")!) 15 let url3 = URL(fileURLWithPath: Bundle.main.path(forResource: "Body-2", ofType: "mp3")!) 16 let url4 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice", ofType: "mp3")!) 17 let url5 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-1", ofType: "mp3")!) 18 let url6 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-2", ofType: "mp3")!) 19 let url7 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-3", ofType: "mp3")!) 20 let url8 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-4", ofType: "mp3")!) 21 let url9 = URL(fileURLWithPath: Bundle.main.path(forResource: "Voice-5", ofType: "mp3")!) 22 let url10 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face", ofType: "mp3")!) 23 let url11 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-1", ofType: "mp3")!) 24 let url12 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-2", ofType: "mp3")!) 25 let url13 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-3", ofType: "mp3")!) 26 let url14 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-4", ofType: "mp3")!) 27 let url15 = URL(fileURLWithPath: Bundle.main.path(forResource: "Face-5", ofType: "mp3")!) 28 29 var error: NSError? 30 //音源を追加するときは数値をあげる 31 do { 32 let rand = arc4random() % 3 33 let rand1 = arc4random() % 6 34 let rand2 = arc4random() % 6 35 36 switch rand1 { 37 case 1: 38 audioPlayer = try AVAudioPlayer(contentsOf: url4) 39 case 2: 40 audioPlayer = try AVAudioPlayer(contentsOf: url5) 41 case 3: 42 audioPlayer = try AVAudioPlayer(contentsOf: url6) 43 case 4: 44 audioPlayer = try AVAudioPlayer(contentsOf: url7) 45 case 5: 46 audioPlayer = try AVAudioPlayer(contentsOf: url8) 47 default: 48 audioPlayer = try AVAudioPlayer(contentsOf: url9) 49 } 50 51 switch rand2 { 52 case 1: 53 audioPlayer = try AVAudioPlayer(contentsOf: url10) 54 case 2: 55 audioPlayer = try AVAudioPlayer(contentsOf: url11) 56 case 3: 57 audioPlayer = try AVAudioPlayer(contentsOf: url12) 58 case 4: 59 audioPlayer = try AVAudioPlayer(contentsOf: url13) 60 case 5: 61 audioPlayer = try AVAudioPlayer(contentsOf: url14) 62 default: 63 audioPlayer = try AVAudioPlayer(contentsOf: url15) 64 } 65 66 switch rand { 67 case 1: 68 audioPlayer = try AVAudioPlayer(contentsOf: url1) 69 case 2: 70 audioPlayer = try AVAudioPlayer(contentsOf: url2) 71 default: 72 audioPlayer = try AVAudioPlayer(contentsOf: url3) 73 } 74 75 } catch let error1 as NSError { 76 error = error1 77 audioPlayer = nil 78 } 79 80 if let err = error { 81 print("audioPlayer error (err.localizedDescription)") 82 return 83 } else { 84 audioPlayer!.delegate = self 85 audioPlayer!.prepareToPlay() 86 } 87 //負の数は無限のループ 88 audioPlayer!.numberOfLoops = 0 89 audioPlayer!.play() 90 } 91} 92
試したこと
どうしても記述した最後のグループしか流れません。初心者なため質問の仕方もわからず本当に申し訳ありませんが、どなたかご指南いただければとてもありがたいです。
どうかよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/07 06:14 編集