質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

386閲覧

音源をカテゴリ別に分けてランダムに再生したい。

flarefactory

総合スコア6

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/07/08 05:53

編集2019/01/07 05:38

前提・実現したいこと

選択したカテゴリによって音源をグループ別けしたいと考えています。エラーは出ないのですが、何を選択しても最後のグループしか流れません。

該当のソースコード

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

試したこと

どうしても記述した最後のグループしか流れません。初心者なため質問の仕方もわからず本当に申し訳ありませんが、どなたかご指南いただければとてもありがたいです。
どうかよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

rand1による選択 → rand2による選択 → randによる選択

と、処理が流れていきますが、それぞれで選択した曲を全てaudioPlayerに保存(上書き)しているので、最後の「randによる選択」で選んだ曲が再生されることになります。

ようするに、カテゴリの選択が出来ていないということです。

投稿2019/01/07 05:49

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

flarefactory

2019/01/07 06:14 編集

なるほど。ありがとうございます! Body,Voice,Faceという三つのカテゴリ毎にrand,rand1,rand2のグループ別でランダム再生が出来るようにしたいのですが、どう調べても答えが見つからず悩んでいます。もう一度考えてみます!
guest

0

自己解決

playSound(soundName)をif caseで三つに分け、

if case soundName = "Body"{ playSound(soundName)}
if case soundName = "Voice"{ playSound1(soundName)}
if case soundName = "Face"{ playSound2(soundName)}

ApplicationDelegateのfuncも三種類記述することで上手くカテゴリ分けができました。

func playSound(_ soundName: String)
func playSound1(_ soundName: String)
func playSound2(_ soundName: String)

投稿2019/01/14 03:31

flarefactory

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問