Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2.0.1 #86

Merged
merged 3 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update FRadioPlayer to version 0.1.9
  • Loading branch information
fethica committed Feb 26, 2018
commit 408be9e1544d03d328d060ea3364f46d3a8d345e
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
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