mp3の曲の再生と停止で困っています。
アプリの起動と同時にONEという曲が再生されるようにAppDelegateのfunc applicationDidBecomeActiveに以下のコードを書きました。
AppDelegate.swift
func applicationDidBecomeActive(_ application: UIApplication) { do { let filePath = Bundle.main.path(forResource: "ONE", ofType: "mp3") let audioPath = NSURL(fileURLWithPath: filePath!) audioPlayer = try AVAudioPlayer(contentsOf: audioPath as URL) audioPlayer.prepareToPlay() audioPlayer?.numberOfLoops = -1 audioPlayer.play() // 音楽の再生 } catch { print("Error") } }
次にTITLEに来た時にONEを停止し、TWOが流れるようにしました。
TITLE.swift
let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.audioPlayer.stop() do{ let filePath = Bundle.main.path(forResource: "TWO", ofType: "mp3") let audioPath = NSURL(fileURLWithPath: filePath!) audioPlayer = try AVAudioPlayer(contentsOf: audioPath as URL) audioPlayer.prepareToPlay() audioPlayer?.numberOfLoops = -1 audioPlayer.play() } }catch{ print("Error") } }
次にMENUに来た時にTWOを停止させたいのですがそれができてない状況です。
TITLEで再生した曲を止めたいので
let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.audioPlayer.stop()
のAppDelegateの部分をTITLEにしたらいいのかなと思いましたがそれでは無理でした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。