-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5737e0
commit 58e1829
Showing
4 changed files
with
275 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ | |
10FCA0551E42AF6700BE6122 /* Accelerate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6F233F5D1CA4776100E484F1 /* Accelerate.framework */; }; | ||
10FCA0561E42AF6C00BE6122 /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FAEDDF51CF77F8000E1691F /* libc++.tbd */; }; | ||
2404317022B8E61400071A09 /* GoogleCastButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2404316F22B8E61400071A09 /* GoogleCastButton.swift */; }; | ||
2404317222B8E68600071A09 /* GoogleCastPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2404317122B8E68600071A09 /* GoogleCastPlayer.swift */; }; | ||
6F1300911CB47B7A0015E8D4 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 6F1300901CB47B7A0015E8D4 /* [email protected] */; }; | ||
6F233EF31CA4610200E484F1 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6F233EBA1CA4610200E484F1 /* Images.xcassets */; }; | ||
6F233F261CA4610200E484F1 /* Settings.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 6F233EED1CA4610200E484F1 /* Settings.bundle */; }; | ||
|
@@ -152,6 +153,7 @@ | |
/* Begin PBXFileReference section */ | ||
10FC9FF51E42A16F00BE6122 /* CastVideos-swift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "CastVideos-swift.app"; sourceTree = BUILT_PRODUCTS_DIR; }; | ||
2404316F22B8E61400071A09 /* GoogleCastButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleCastButton.swift; sourceTree = "<group>"; }; | ||
2404317122B8E68600071A09 /* GoogleCastPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleCastPlayer.swift; sourceTree = "<group>"; }; | ||
637E433DD77556EEE12772C9 /* Pods_CastVideos_objc.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CastVideos_objc.framework; sourceTree = BUILT_PRODUCTS_DIR; }; | ||
6F1300901CB47B7A0015E8D4 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "[email protected]"; path = "Resources/[email protected]"; sourceTree = "<group>"; }; | ||
6F233EBA1CA4610200E484F1 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = Resources/Images.xcassets; sourceTree = "<group>"; }; | ||
|
@@ -462,6 +464,7 @@ | |
isa = PBXGroup; | ||
children = ( | ||
2404316F22B8E61400071A09 /* GoogleCastButton.swift */, | ||
2404317122B8E68600071A09 /* GoogleCastPlayer.swift */, | ||
D5EA0403217544B700013FC8 /* MediaTableViewController.swift */, | ||
2404316E22B8E4D900071A09 /* New Group */, | ||
); | ||
|
@@ -815,6 +818,7 @@ | |
D5EA0410217544B700013FC8 /* MediaItem.swift in Sources */, | ||
D5EA0413217544B700013FC8 /* LocalPlayerView.swift in Sources */, | ||
D5EA040E217544B700013FC8 /* RootContainerViewController.swift in Sources */, | ||
2404317222B8E68600071A09 /* GoogleCastPlayer.swift in Sources */, | ||
D5EA0411217544B700013FC8 /* ActionSheet.swift in Sources */, | ||
D5EA0416217544B800013FC8 /* Toast.swift in Sources */, | ||
D5EA0415217544B700013FC8 /* MediaListModel.swift in Sources */, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import GoogleCast | ||
|
||
class GoogleCastPlayer: NSObject { | ||
|
||
var hasConnectedSession: Bool { return sessionManager?.hasConnectedSession() ?? false } | ||
|
||
private var sessionManager: GCKSessionManager? { return GCKCastContext.sharedInstance().sessionManager } | ||
private var castSession: GCKCastSession? { return sessionManager?.currentSession as? GCKCastSession } | ||
|
||
var progress: Double { | ||
guard let client = castSession?.remoteMediaClient else { return 0.0 } | ||
let approximateStreamPosition = client.approximateStreamPosition() | ||
let mediaInfo = client.mediaStatus?.currentQueueItem?.mediaInformation | ||
guard let streamDuration = mediaInfo?.streamDuration else { return 0.0 } | ||
if streamDuration <= 0 { return 0.0 } | ||
return approximateStreamPosition / streamDuration | ||
} | ||
|
||
func addListener() { | ||
sessionManager?.add(self) | ||
} | ||
|
||
func removeListener() { | ||
sessionManager?.remove(self) | ||
} | ||
|
||
func play(mediaInfo: GCKMediaInformation) { | ||
let builder = GCKMediaQueueItemBuilder() | ||
builder.mediaInformation = mediaInfo | ||
builder.autoplay = true | ||
builder.preloadTime = TimeInterval(30) | ||
let item = builder.build() | ||
let options = GCKMediaQueueLoadOptions() | ||
options.repeatMode = .off | ||
let request = castSession?.remoteMediaClient?.queueLoad([item], with: options) | ||
request?.delegate = self | ||
castSession?.remoteMediaClient?.add(self) | ||
GCKCastContext.sharedInstance().presentDefaultExpandedMediaControls() | ||
} | ||
} | ||
|
||
extension GoogleCastPlayer: GCKSessionManagerListener { | ||
func sessionManager(_: GCKSessionManager, didStart _: GCKSession) { | ||
} | ||
|
||
func sessionManager(_: GCKSessionManager, didResumeSession _: GCKSession) { | ||
} | ||
|
||
func sessionManager(_: GCKSessionManager, didEnd _: GCKSession, withError error: Error?) { | ||
|
||
} | ||
|
||
func sessionManager(_: GCKSessionManager, didFailToStartSessionWithError _: Error?) { | ||
|
||
} | ||
|
||
func sessionManager(_: GCKSessionManager, | ||
didFailToResumeSession _: GCKSession, withError _: Error?) { | ||
|
||
} | ||
} | ||
|
||
extension GoogleCastPlayer: GCKRemoteMediaClientListener { | ||
func remoteMediaClient(_: GCKRemoteMediaClient, didStartMediaSessionWithID _: Int) { | ||
|
||
} | ||
|
||
func remoteMediaClient(_: GCKRemoteMediaClient, didUpdate mediaStatus: GCKMediaStatus?) { | ||
guard let state = mediaStatus?.playerState else { return } | ||
print("GoogleCastPlayer State: \(state.description)") | ||
} | ||
} | ||
|
||
extension GoogleCastPlayer: GCKRequestDelegate { | ||
func request(_: GCKRequest, didFailWithError _: GCKError) { | ||
} | ||
|
||
func request(_: GCKRequest, didAbortWith _: GCKRequestAbortReason) { | ||
} | ||
|
||
func requestDidComplete(_ request: GCKRequest) { | ||
if request.error != nil { | ||
} | ||
} | ||
} | ||
|
||
extension GCKMediaPlayerState { | ||
var description: String { | ||
switch self { | ||
case .buffering: | ||
return "buffering" | ||
case .playing: | ||
return "playing" | ||
case .paused: | ||
return "paused" | ||
case .idle: | ||
return "idle" | ||
case .loading: | ||
return "loading" | ||
default: | ||
return "unknown" | ||
} | ||
} | ||
} |
Oops, something went wrong.