Skip to content

Commit

Permalink
Merge pull request #86 from analogcode/dev
Browse files Browse the repository at this point in the history
Version 2.0.1
  • Loading branch information
fethica authored Feb 26, 2018
2 parents 26055fc + d88ffe9 commit fea09d8
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 10 deletions.
4 changes: 4 additions & 0 deletions SwiftRadio.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
94D260981B45E8B800DE671C /* Track.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94D260971B45E8B800DE671C /* Track.swift */; };
94D30EA71AD07A880024FE96 /* StationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94D30EA61AD07A880024FE96 /* StationTableViewCell.swift */; };
94E9761C1B1A8F3200F52B1E /* UIImage+DropShadow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94E9761B1B1A8F3200F52B1E /* UIImage+DropShadow.swift */; };
CA512B5B2043AB4B00D425A4 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA512B5A2043AB4B00D425A4 /* Reachability.swift */; };
CAA7C15D1FD77F3A003CABDF /* FRadioAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA7C15B1FD77F3A003CABDF /* FRadioAPI.swift */; };
CAA7C15E1FD77F3A003CABDF /* FRadioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA7C15C1FD77F3A003CABDF /* FRadioPlayer.swift */; };
CAA8FDB52000614600050F77 /* RadioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA8FDB42000614600050F77 /* RadioPlayer.swift */; };
Expand Down Expand Up @@ -125,6 +126,7 @@
94D30EA61AD07A880024FE96 /* StationTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StationTableViewCell.swift; sourceTree = "<group>"; };
94E9761B1B1A8F3200F52B1E /* UIImage+DropShadow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+DropShadow.swift"; sourceTree = "<group>"; };
B90086461BBE40AF00E5372C /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
CA512B5A2043AB4B00D425A4 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
CAA7C15B1FD77F3A003CABDF /* FRadioAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FRadioAPI.swift; sourceTree = "<group>"; };
CAA7C15C1FD77F3A003CABDF /* FRadioPlayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FRadioPlayer.swift; sourceTree = "<group>"; };
CAA8FDB42000614600050F77 /* RadioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RadioPlayer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -312,6 +314,7 @@
children = (
CAA7C15B1FD77F3A003CABDF /* FRadioAPI.swift */,
CAA7C15C1FD77F3A003CABDF /* FRadioPlayer.swift */,
CA512B5A2043AB4B00D425A4 /* Reachability.swift */,
);
path = FRadioPlayer;
sourceTree = "<group>";
Expand Down Expand Up @@ -459,6 +462,7 @@
5F22BA561F72AD5A00CB5911 /* DesignableView.swift in Sources */,
5F22BA521F72AD5A00CB5911 /* UnwindSegue.swift in Sources */,
94E9761C1B1A8F3200F52B1E /* UIImage+DropShadow.swift in Sources */,
CA512B5B2043AB4B00D425A4 /* Reachability.swift in Sources */,
94452E551AD7086800BFE7A5 /* AboutViewController.swift in Sources */,
5F22BA551F72AD5A00CB5911 /* AutoTextView.swift in Sources */,
94D260981B45E8B800DE671C /* Track.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions SwiftRadio/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
UINavigationBar.appearance().barStyle = .black

// FRadioPlayer config
FRadioPlayer.shared.isAutoPlay = true
FRadioPlayer.shared.enableArtwork = true
FRadioPlayer.shared.artworkSize = 600

// Get weak ref of StationsViewController
Expand Down
8 changes: 4 additions & 4 deletions SwiftRadio/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app would like to save the image associated with the current track and station to your photo library.</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand All @@ -19,18 +17,20 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.0</string>
<string>2.0.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>3</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app would like to save the image associated with the current track and station to your photo library.</string>
<key>NSUserActivityTypes</key>
<array>
<string>NSUserActivityTypeBrowsingWeb</string>
Expand Down
4 changes: 2 additions & 2 deletions SwiftRadio/Libraries/FRadioPlayer/FRadioAPI.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// FRadioAPI.swift
// FRadioPlayerDemo
// FRadioPlayer
//
// Created by Fethi El Hassasna on 2017-11-25.
// Copyright © 2017 Fethi El Hassasna. All rights reserved.
// Copyright © 2017 Fethi El Hassasna (@fethica). All rights reserved.
//

import Foundation
Expand Down
54 changes: 50 additions & 4 deletions SwiftRadio/Libraries/FRadioPlayer/FRadioPlayer.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// FRadioPlayer.swift
// FRadioPlayerDemo
// FRadioPlayer
//
// Created by Fethi El Hassasna on 2017-11-11.
// Copyright © 2017 Fethi El Hassasna. All rights reserved.
// Copyright © 2017 Fethi El Hassasna (@fethica). All rights reserved.
//

import AVFoundation
Expand Down Expand Up @@ -211,6 +211,12 @@ open class FRadioPlayer: NSObject {
}
}

/// Reachability for network interruption handling
private let reachability = Reachability()!

/// Current network connectivity
private var isConnected = false

// MARK: - Initialization

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

// Check for headphones
checkHeadphonesConnection(outputs: AVAudioSession.sharedInstance().currentRoute.outputs)

// Reachability config
try? reachability.startNotifier()
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
isConnected = reachability.connection != .none
}

// MARK: - Control Methods
Expand Down Expand Up @@ -380,6 +391,11 @@ open class FRadioPlayer: NSObject {
})
}

private func reloadItem() {
player?.replaceCurrentItem(with: nil)
player?.replaceCurrentItem(with: playerItem)
}

private func resetPlayer() {
stop()
playerItem = nil
Expand Down Expand Up @@ -419,6 +435,34 @@ open class FRadioPlayer: NSObject {
}
}

@objc func reachabilityChanged(note: Notification) {

guard let reachability = note.object as? Reachability else { return }

// Check if the internet connection was lost
if reachability.connection != .none, !isConnected {
checkNetworkInterruption()
}

isConnected = reachability.connection != .none
}

// Check if the playback could keep up after a network interruption
private func checkNetworkInterruption() {
guard
let item = playerItem,
!item.isPlaybackLikelyToKeepUp,
reachability.connection != .none else { return }

player?.pause()

// Wait 1 sec to recheck and make sure the reload is needed
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
if !item.isPlaybackLikelyToKeepUp { self.reloadItem() }
self.isPlaying ? self.player?.play() : self.player?.pause()
}
}

// MARK: - Responding to Route Changes

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

case "playbackBufferEmpty":

if item.isPlaybackBufferEmpty { self.state = .loading }
if item.isPlaybackBufferEmpty {
self.state = .loading
self.checkNetworkInterruption()
}

case "playbackLikelyToKeepUp":

Expand All @@ -480,4 +527,3 @@ open class FRadioPlayer: NSObject {
}
}
}

Loading

0 comments on commit fea09d8

Please sign in to comment.