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

Add ability to set auth headers #64

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions Example/Tests/AVPlayerWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class AVPlayerWrapperTests: XCTestCase {
default: break
}
}
wrapper.load(from: LongSource.url, playWhenReady: true, initialTime: 4.0)
wrapper.load(from: LongSource.url, playWhenReady: true, initialTime: 4.0, headers: [:])
wait(for: [expectation], timeout: 20.0)
}

Expand Down Expand Up @@ -146,7 +146,8 @@ class AVPlayerWrapperTests: XCTestCase {
holder.didSeekTo = { seconds in
expectation.fulfill()
}
wrapper.load(from: LongSource.url, playWhenReady: false, initialTime: 4.0)

wrapper.load(from: LongSource.url, playWhenReady: false, initialTime: 4.0, headers: [:])
wait(for: [expectation], timeout: 20.0)
}

Expand Down
13 changes: 9 additions & 4 deletions SwiftAudio/Classes/AVPlayerWrapper/AVPlayerWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,21 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {
}
}

func load(from url: URL, playWhenReady: Bool) {
func load(from url: URL, playWhenReady: Bool, headers: [String: Any]? = nil) {
reset(soft: true)
_playWhenReady = playWhenReady

if currentItem?.status == .failed {
recreateAVPlayer()
}

var options: [String: Any] = [:]
if let headers = headers {
options = ["AVURLAssetHTTPHeaderFieldsKey": headers]
}

// Set item
self._pendingAsset = AVURLAsset(url: url)
self._pendingAsset = AVURLAsset(url: url, options: options)
if let pendingAsset = _pendingAsset {
pendingAsset.loadValuesAsynchronously(forKeys: [Constants.assetPlayableKey], completionHandler: {
var error: NSError? = nil
Expand Down Expand Up @@ -220,10 +225,10 @@ class AVPlayerWrapper: AVPlayerWrapperProtocol {
}
}

func load(from url: URL, playWhenReady: Bool, initialTime: TimeInterval?) {
func load(from url: URL, playWhenReady: Bool, initialTime: TimeInterval?, headers: [String: Any]?) {
_initialTime = initialTime
self.pause()
self.load(from: url, playWhenReady: playWhenReady)
self.load(from: url, playWhenReady: playWhenReady, headers: headers)
}

// MARK: - Util
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protocol AVPlayerWrapperProtocol: class {

func seek(to seconds: TimeInterval)

func load(from url: URL, playWhenReady: Bool)
func load(from url: URL, playWhenReady: Bool, headers: [String: Any]?)

func load(from url: URL, playWhenReady: Bool, initialTime: TimeInterval?)
func load(from url: URL, playWhenReady: Bool, initialTime: TimeInterval?, headers: [String: Any]?)

}
26 changes: 26 additions & 0 deletions SwiftAudio/Classes/AudioItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public protocol InitialTiming {
func getInitialTime() -> TimeInterval
}

/// Make your `AudioItem`-subclass conform to this protocol to control enable the ability to set authorization headers.
public protocol Authorizing {
func getHeaders() -> [String: Any]
}

public class DefaultAudioItem: AudioItem {

public var audioUrl: String
Expand Down Expand Up @@ -125,3 +130,24 @@ public class DefaultAudioItemInitialTime: DefaultAudioItem, InitialTiming {
}

}

/// An AudioItem that also conforms to the `Authorizing`-protocol
public class DefaultAudioItemAuthorizing: DefaultAudioItem, Authorizing {

public var headers: [String: Any]

public override init(audioUrl: String, artist: String?, title: String?, albumTitle: String?, sourceType: SourceType, artwork: UIImage?) {
self.headers = [:]
super.init(audioUrl: audioUrl, artist: artist, title: title, albumTitle: albumTitle, sourceType: sourceType, artwork: artwork)
}

public init(audioUrl: String, artist: String?, title: String?, albumTitle: String?, sourceType: SourceType, artwork: UIImage?, headers: [String: Any]) {
self.headers = headers
super.init(audioUrl: audioUrl, artist: artist, title: title, albumTitle: albumTitle, sourceType: sourceType, artwork: artwork)
}

public func getHeaders() -> [String: Any] {
return headers
}

}
3 changes: 2 additions & 1 deletion SwiftAudio/Classes/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public class AudioPlayer: AVPlayerWrapperDelegate {

wrapper.load(from: url,
playWhenReady: playWhenReady,
initialTime: (item as? InitialTiming)?.getInitialTime())
initialTime: (item as? InitialTiming)?.getInitialTime(),
headers: (item as? Authorizing)?.getHeaders())

self._currentItem = item

Expand Down