Skip to content

Commit fea09d8

Browse files
authored
Merge pull request #86 from analogcode/dev
Version 2.0.1
2 parents 26055fc + d88ffe9 commit fea09d8

File tree

6 files changed

+386
-10
lines changed

6 files changed

+386
-10
lines changed

SwiftRadio.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
94D260981B45E8B800DE671C /* Track.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94D260971B45E8B800DE671C /* Track.swift */; };
5555
94D30EA71AD07A880024FE96 /* StationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94D30EA61AD07A880024FE96 /* StationTableViewCell.swift */; };
5656
94E9761C1B1A8F3200F52B1E /* UIImage+DropShadow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94E9761B1B1A8F3200F52B1E /* UIImage+DropShadow.swift */; };
57+
CA512B5B2043AB4B00D425A4 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA512B5A2043AB4B00D425A4 /* Reachability.swift */; };
5758
CAA7C15D1FD77F3A003CABDF /* FRadioAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA7C15B1FD77F3A003CABDF /* FRadioAPI.swift */; };
5859
CAA7C15E1FD77F3A003CABDF /* FRadioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA7C15C1FD77F3A003CABDF /* FRadioPlayer.swift */; };
5960
CAA8FDB52000614600050F77 /* RadioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA8FDB42000614600050F77 /* RadioPlayer.swift */; };
@@ -125,6 +126,7 @@
125126
94D30EA61AD07A880024FE96 /* StationTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StationTableViewCell.swift; sourceTree = "<group>"; };
126127
94E9761B1B1A8F3200F52B1E /* UIImage+DropShadow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+DropShadow.swift"; sourceTree = "<group>"; };
127128
B90086461BBE40AF00E5372C /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
129+
CA512B5A2043AB4B00D425A4 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
128130
CAA7C15B1FD77F3A003CABDF /* FRadioAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FRadioAPI.swift; sourceTree = "<group>"; };
129131
CAA7C15C1FD77F3A003CABDF /* FRadioPlayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FRadioPlayer.swift; sourceTree = "<group>"; };
130132
CAA8FDB42000614600050F77 /* RadioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioPlayer.swift; sourceTree = "<group>"; };
@@ -312,6 +314,7 @@
312314
children = (
313315
CAA7C15B1FD77F3A003CABDF /* FRadioAPI.swift */,
314316
CAA7C15C1FD77F3A003CABDF /* FRadioPlayer.swift */,
317+
CA512B5A2043AB4B00D425A4 /* Reachability.swift */,
315318
);
316319
path = FRadioPlayer;
317320
sourceTree = "<group>";
@@ -459,6 +462,7 @@
459462
5F22BA561F72AD5A00CB5911 /* DesignableView.swift in Sources */,
460463
5F22BA521F72AD5A00CB5911 /* UnwindSegue.swift in Sources */,
461464
94E9761C1B1A8F3200F52B1E /* UIImage+DropShadow.swift in Sources */,
465+
CA512B5B2043AB4B00D425A4 /* Reachability.swift in Sources */,
462466
94452E551AD7086800BFE7A5 /* AboutViewController.swift in Sources */,
463467
5F22BA551F72AD5A00CB5911 /* AutoTextView.swift in Sources */,
464468
94D260981B45E8B800DE671C /* Track.swift in Sources */,

SwiftRadio/AppDelegate.swift

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2323
UINavigationBar.appearance().barStyle = .black
2424

2525
// FRadioPlayer config
26+
FRadioPlayer.shared.isAutoPlay = true
27+
FRadioPlayer.shared.enableArtwork = true
2628
FRadioPlayer.shared.artworkSize = 600
2729

2830
// Get weak ref of StationsViewController

SwiftRadio/Info.plist

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>NSPhotoLibraryAddUsageDescription</key>
6-
<string>This app would like to save the image associated with the current track and station to your photo library.</string>
75
<key>CFBundleDevelopmentRegion</key>
86
<string>en</string>
97
<key>CFBundleDisplayName</key>
@@ -19,18 +17,20 @@
1917
<key>CFBundlePackageType</key>
2018
<string>APPL</string>
2119
<key>CFBundleShortVersionString</key>
22-
<string>2.0</string>
20+
<string>2.0.1</string>
2321
<key>CFBundleSignature</key>
2422
<string>????</string>
2523
<key>CFBundleVersion</key>
26-
<string>2</string>
24+
<string>3</string>
2725
<key>LSRequiresIPhoneOS</key>
2826
<true/>
2927
<key>NSAppTransportSecurity</key>
3028
<dict>
3129
<key>NSAllowsArbitraryLoads</key>
3230
<true/>
3331
</dict>
32+
<key>NSPhotoLibraryAddUsageDescription</key>
33+
<string>This app would like to save the image associated with the current track and station to your photo library.</string>
3434
<key>NSUserActivityTypes</key>
3535
<array>
3636
<string>NSUserActivityTypeBrowsingWeb</string>

SwiftRadio/Libraries/FRadioPlayer/FRadioAPI.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
22
// FRadioAPI.swift
3-
// FRadioPlayerDemo
3+
// FRadioPlayer
44
//
55
// Created by Fethi El Hassasna on 2017-11-25.
6-
// Copyright © 2017 Fethi El Hassasna. All rights reserved.
6+
// Copyright © 2017 Fethi El Hassasna (@fethica). All rights reserved.
77
//
88

99
import Foundation

SwiftRadio/Libraries/FRadioPlayer/FRadioPlayer.swift

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
22
// FRadioPlayer.swift
3-
// FRadioPlayerDemo
3+
// FRadioPlayer
44
//
55
// Created by Fethi El Hassasna on 2017-11-11.
6-
// Copyright © 2017 Fethi El Hassasna. All rights reserved.
6+
// Copyright © 2017 Fethi El Hassasna (@fethica). All rights reserved.
77
//
88

99
import AVFoundation
@@ -211,6 +211,12 @@ open class FRadioPlayer: NSObject {
211211
}
212212
}
213213

214+
/// Reachability for network interruption handling
215+
private let reachability = Reachability()!
216+
217+
/// Current network connectivity
218+
private var isConnected = false
219+
214220
// MARK: - Initialization
215221

216222
private override init() {
@@ -225,6 +231,11 @@ open class FRadioPlayer: NSObject {
225231

226232
// Check for headphones
227233
checkHeadphonesConnection(outputs: AVAudioSession.sharedInstance().currentRoute.outputs)
234+
235+
// Reachability config
236+
try? reachability.startNotifier()
237+
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
238+
isConnected = reachability.connection != .none
228239
}
229240

230241
// MARK: - Control Methods
@@ -380,6 +391,11 @@ open class FRadioPlayer: NSObject {
380391
})
381392
}
382393

394+
private func reloadItem() {
395+
player?.replaceCurrentItem(with: nil)
396+
player?.replaceCurrentItem(with: playerItem)
397+
}
398+
383399
private func resetPlayer() {
384400
stop()
385401
playerItem = nil
@@ -419,6 +435,34 @@ open class FRadioPlayer: NSObject {
419435
}
420436
}
421437

438+
@objc func reachabilityChanged(note: Notification) {
439+
440+
guard let reachability = note.object as? Reachability else { return }
441+
442+
// Check if the internet connection was lost
443+
if reachability.connection != .none, !isConnected {
444+
checkNetworkInterruption()
445+
}
446+
447+
isConnected = reachability.connection != .none
448+
}
449+
450+
// Check if the playback could keep up after a network interruption
451+
private func checkNetworkInterruption() {
452+
guard
453+
let item = playerItem,
454+
!item.isPlaybackLikelyToKeepUp,
455+
reachability.connection != .none else { return }
456+
457+
player?.pause()
458+
459+
// Wait 1 sec to recheck and make sure the reload is needed
460+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
461+
if !item.isPlaybackLikelyToKeepUp { self.reloadItem() }
462+
self.isPlaying ? self.player?.play() : self.player?.pause()
463+
}
464+
}
465+
422466
// MARK: - Responding to Route Changes
423467

424468
private func checkHeadphonesConnection(outputs: [AVAudioSessionPortDescription]) {
@@ -464,7 +508,10 @@ open class FRadioPlayer: NSObject {
464508

465509
case "playbackBufferEmpty":
466510

467-
if item.isPlaybackBufferEmpty { self.state = .loading }
511+
if item.isPlaybackBufferEmpty {
512+
self.state = .loading
513+
self.checkNetworkInterruption()
514+
}
468515

469516
case "playbackLikelyToKeepUp":
470517

@@ -480,4 +527,3 @@ open class FRadioPlayer: NSObject {
480527
}
481528
}
482529
}
483-

0 commit comments

Comments
 (0)