Skip to content

Commit ee80976

Browse files
tanhakabirjw1540
andcommitted
add sleep timer feature
Co-Authored-By: Joe Williams <[email protected]>
1 parent 10aea39 commit ee80976

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Example/SwiftAudioPlayer/Base.lproj/Main.storyboard

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@
137137
<action selector="skipSilencesSwitched:" destination="vXZ-lx-hvc" eventType="valueChanged" id="p7X-Y8-7hO"/>
138138
</connections>
139139
</switch>
140+
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="IGe-aU-Y6D">
141+
<rect key="frame" x="226" y="540" width="49" height="31"/>
142+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
143+
<connections>
144+
<action selector="sleepSwitched:" destination="vXZ-lx-hvc" eventType="valueChanged" id="noa-m8-VHy"/>
145+
</connections>
146+
</switch>
147+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Sleep After 5 s" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vf6-kr-yWa">
148+
<rect key="frame" x="83" y="545" width="112" height="21"/>
149+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
150+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
151+
<nil key="textColor"/>
152+
<nil key="highlightedColor"/>
153+
</label>
140154
</subviews>
141155
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
142156
<constraints>
@@ -198,6 +212,7 @@
198212
<outlet property="skipBackwardButton" destination="tFH-sY-Xu9" id="LwM-2S-m6F"/>
199213
<outlet property="skipForwardButton" destination="0QE-3F-a4G" id="cQ7-b7-pW7"/>
200214
<outlet property="skipSilencesSwitch" destination="2cn-E5-TeQ" id="TRI-IT-YJT"/>
215+
<outlet property="sleepSwitch" destination="IGe-aU-Y6D" id="BZn-9C-hOk"/>
201216
<outlet property="streamButton" destination="6d9-Bc-hIz" id="DZe-ga-3RV"/>
202217
</connections>
203218
</viewController>

Example/SwiftAudioPlayer/ViewController.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,14 @@ class ViewController: UIViewController {
316316
_ = SAPlayer.Features.SkipSilences.disable()
317317
}
318318
}
319+
@IBOutlet weak var sleepSwitch: UISwitch!
320+
321+
@IBAction func sleepSwitched(_ sender: Any) {
322+
if sleepSwitch.isOn {
323+
_ = SAPlayer.Features.SleepTimer.enable(afterDelay: 5.0)
324+
} else {
325+
_ = SAPlayer.Features.SleepTimer.disable()
326+
}
327+
}
319328
}
320329

Source/SAPlayerFeatures.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,30 @@ extension SAPlayer {
9292
}
9393
}
9494
}
95+
96+
/**
97+
Feature to pause the player after a delay. This will happen regardless of if another audio clip has started.
98+
*/
99+
public struct SleepTimer {
100+
static var timer: Timer?
101+
102+
/**
103+
Enable feature to pause the player after a delay. This will happen regardless of if another audio clip has started.
104+
105+
- Parameter afterDelay: The number of seconds to wait before pausing the audio
106+
*/
107+
public static func enable(afterDelay delay: Double) {
108+
timer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false, block: { _ in
109+
SAPlayer.shared.pause()
110+
})
111+
}
112+
113+
/**
114+
Disable feature to pause the player after a delay.
115+
*/
116+
public static func disable() {
117+
timer?.invalidate()
118+
}
119+
}
95120
}
96121
}

0 commit comments

Comments
 (0)