-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc28995
commit 33c9874
Showing
5 changed files
with
54 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Sources/SwiftFortuneWheel/Utils/Audio/AudioPersistencing.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// AudioPersistencing.swift | ||
// SwiftFortuneWheel | ||
// | ||
// Created by Sherzod Khashimov on 27/10/22. | ||
// Copyright © 2022 SwiftFortuneWheel. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import AVFoundation | ||
|
||
protocol AudioPersistencing { | ||
func enableHandleInterruption() | ||
} | ||
|
||
extension AudioPersistencing { | ||
#if canImport(AVAudioSession) | ||
func enableHandleInterruption() { | ||
NotificationCenter.default.addObserver(self, | ||
selector: #selector(handleInterruption), | ||
name: AVAudioSession.interruptionNotification, | ||
object: AVAudioSession.sharedInstance()) | ||
} | ||
|
||
/// Stops audio when interrupted | ||
@objc func handleInterruption(notification: Notification) { | ||
guard let userInfo = notification.userInfo, | ||
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, | ||
let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { | ||
return | ||
} | ||
|
||
isAudioInterrupted = type == .began | ||
|
||
if isAudioInterrupted { | ||
node.stop() | ||
} | ||
} | ||
#else | ||
func enableHandleInterruption() { | ||
//do nothing | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters