Skip to content

Commit

Permalink
Use Google cast Player
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagomartinho committed Jun 18, 2019
1 parent f5737e0 commit 58e1829
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 283 deletions.
4 changes: 4 additions & 0 deletions CastVideos-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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>"; };
Expand Down Expand Up @@ -462,6 +464,7 @@
isa = PBXGroup;
children = (
2404316F22B8E61400071A09 /* GoogleCastButton.swift */,
2404317122B8E68600071A09 /* GoogleCastPlayer.swift */,
D5EA0403217544B700013FC8 /* MediaTableViewController.swift */,
2404316E22B8E4D900071A09 /* New Group */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
104 changes: 104 additions & 0 deletions CastVideos-swift/Classes/GoogleCastPlayer.swift
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"
}
}
}
Loading

0 comments on commit 58e1829

Please sign in to comment.