Skip to content

Commit 3e646eb

Browse files
authored
Merge pull request #522 from Rexios80/feature/resume-session
[audio_streamer] Resume audio session after interruption
2 parents 29b12b4 + f85e77b commit 3e646eb

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

packages/audio_streamer/ios/Classes/SwiftAudioStreamerPlugin.swift

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,38 @@ public class SwiftAudioStreamerPlugin: NSObject, FlutterPlugin, FlutterStreamHan
3838
object: nil)
3939
}
4040

41-
@objc func handleInterruption(notification: Notification) {
42-
// If no eventSink to emit events to, do nothing (wait)
43-
if eventSink == nil {
44-
return
41+
@objc func handleInterruption(notification: Notification) {
42+
// If no eventSink to emit events to, do nothing (wait)
43+
if eventSink == nil {
44+
return
45+
}
46+
47+
guard let userInfo = notification.userInfo,
48+
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
49+
let type = AVAudioSession.InterruptionType(rawValue: typeValue)
50+
else {
51+
return
52+
}
53+
54+
switch type {
55+
case .began: ()
56+
case .ended:
57+
// An interruption ended. Resume playback, if appropriate.
58+
59+
guard let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else { return }
60+
let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
61+
if options.contains(.shouldResume) {
62+
startRecording()
63+
}
64+
65+
default:
66+
eventSink!(
67+
FlutterError(
68+
code: "100", message: "Recording was interrupted",
69+
details: "Another process interrupted recording."))
70+
}
4571
}
46-
// To be implemented.
47-
eventSink!(
48-
FlutterError(
49-
code: "100", message: "Recording was interrupted",
50-
details: "Another process interrupted recording."))
51-
}
72+
5273

5374
// Handle stream emitting (Swift => Flutter)
5475
private func emitValues(values: [Float]) {

0 commit comments

Comments
 (0)