質問編集履歴

1

コード追加

2017/01/06 22:13

投稿

hikarin
hikarin

スコア24

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,39 @@
9
9
  BGMは一つしか使う予定はないのでこれでうまくいったのですが、ムービーを流す時だけBGMを止めたいと思っています。
10
10
 
11
11
  ムービーの画面になってaudioPlayer.stop()をしてもAppDelegateで流れた音楽が止まらないのですがどうしたらいいのでしょうか。
12
+
13
+
14
+
15
+ ```
16
+
17
+ var audioPlayer : AVAudioPlayer!
18
+
19
+ func applicationDidBecomeActive(_ application: UIApplication) {
20
+
21
+ do {
22
+
23
+ // 音楽ファイルが"sample.mp3"の場合
24
+
25
+ let filePath = Bundle.main.path(forResource: "sample", ofType: "mp3")
26
+
27
+ let audioPath = NSURL(fileURLWithPath: filePath!)
28
+
29
+ audioPlayer = try AVAudioPlayer(contentsOf: audioPath as URL)
30
+
31
+ audioPlayer.prepareToPlay()
32
+
33
+ audioPlayer?.numberOfLoops = -1
34
+
35
+ audioPlayer.play() // 音楽の再生
36
+
37
+ } catch {
38
+
39
+ print("Error")
40
+
41
+ }
42
+
43
+ }
44
+
45
+
46
+
47
+ ```