| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
by Song
App Delegate 메소드를 통한 Audio Session 활성화
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setAudioSession()
// 기타 코드 생략
}
private func setAudioSession() {
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
try? AVAudioSession.sharedInstance().setActive(true)
}각 씬에 필요한 Audio Player를 컨트롤 하는 객체 생성
struct MusicStation {
static let shared = MusicStation()
private var mainMusicPlayer = AudioPlayerFactory.create(of: Music.main, mode: .music)
private var gameMusicPlayer = AudioPlayerFactory.create(of: Music.game, mode: .music)
func play(type: Music) {
switch type {
case .main:
mainMusicPlayer?.currentTime = 0 // 음악을 맨 처음으로 되돌린다
mainMusicPlayer?.play()
gameMusicPlayer?.stop()
case .game:
gameMusicPlayer?.currentTime = 0
gameMusicPlayer?.play()
mainMusicPlayer?.stop()
}
}
func stop() {
mainMusicPlayer?.stop()
gameMusicPlayer?.stop()
}
}struct AudioPlayerFactory {
enum Mode {
case music
case effect
}
static func create(of soundType: BundlePathIncludable, mode: Mode) -> AVAudioPlayer? {
guard let bundlePath = soundType.bundlePath else { return nil }
let url = URL(fileURLWithPath: bundlePath)
let audioPlayer = try? AVAudioPlayer(contentsOf: url)
audioPlayer?.numberOfLoops = mode == .music ? .max : 0
return audioPlayer
}
}protocol BundlePathIncludable {
var bundlePath: String? { get }
}
enum MainSoundEffect: BundlePathIncludable {
case reward
case upgrade
var bundlePath: String? {
switch self {
case .reward:
return Bundle.main.path(forResource: "reward", ofType: "wav")
case .upgrade:
return Bundle.main.path(forResource: "upgrade", ofType: "wav")
}
}
}created by 우송
| Back | FazBrowse Home | New Git URL |