다른보기로 변경할 때 배경 음악 재생을 중지하려고합니다. 보기 변경이 제공 될 때마다 플레이어를 중지 그냥 코드 아래에 쓰기 Swift :보기를 변경할 때 배경 음악을 중지하는 방법
나는 음악을import Foundation
import AVFoundation
class MusicHelper {
static let sharedHelper = MusicHelper()
var audioPlayer: AVAudioPlayer?
func playBackgroundMusic() {
let aSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "The Walking Dead", ofType: "mp3")!)
do {
audioPlayer = try AVAudioPlayer(contentsOf:aSound as URL)
audioPlayer!.numberOfLoops = -1
audioPlayer!.prepareToPlay()
audioPlayer!.play()
} catch {
print("Cannot play the file")
}
}
}
을 재생하는 글로벌 변수가 그리고 여기보기에
import UIKit
import SwiftGifOrigin
import AVFoundation
class ViewController: UIViewController {
var audioPlayer: AVAudioPlayer?
@IBOutlet weak var introGif: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Gif intro
self.introGif.image = UIImage.gifWithName("WalkingDeadIntro")
_ = try! Data(contentsOf: Bundle.main.url(forResource: "WalkingDeadIntro", withExtension: "gif")!)
//self.bottomImageView.image = UIImage.gif(data: imageData)
// Load background Music
MusicHelper.sharedHelper.playBackgroundMusic()
}
예를 들어 viewWillDisappear()에 MusicHelper.sharedHelper.stopBackgroundMusic()을 추가하는 것이 잘못된 이유는 무엇입니까? – Gruntcakes