-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from apivideo/feat/display-info-center
feat() add mediaplayer controller to control video from lockscreen
- Loading branch information
Showing
10 changed files
with
277 additions
and
6 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
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
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
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
88 changes: 88 additions & 0 deletions
88
Sources/ApiVideoPlayer/ApiVideoPlayerInformationNowPlaying.swift
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,88 @@ | ||
import Foundation | ||
import MediaPlayer | ||
|
||
protocol InformationNowPlaying { | ||
var nowPlayingData: NowPlayingData? { get set } | ||
func pause(currentTime: CMTime) | ||
func play(currentTime: CMTime) | ||
func updateCurrentTime(currentTime: CMTime) | ||
func updatePlaybackRate(rate: Float) | ||
} | ||
|
||
class ApiVideoPlayerInformationNowPlaying: InformationNowPlaying { | ||
private var infos = [String: Any]() | ||
private let taskExecutor: TasksExecutorProtocol.Type | ||
|
||
init(taskExecutor: TasksExecutorProtocol.Type) { | ||
self.taskExecutor = taskExecutor | ||
} | ||
|
||
var nowPlayingData: NowPlayingData? { | ||
didSet { | ||
guard let nowPlayingData = nowPlayingData else { | ||
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:] | ||
return | ||
} | ||
if let title = nowPlayingData.title { | ||
infos[MPMediaItemPropertyTitle] = title | ||
} | ||
infos[MPMediaItemPropertyPlaybackDuration] = nowPlayingData.duration.seconds | ||
infos[MPNowPlayingInfoPropertyElapsedPlaybackTime] = nowPlayingData.currentTime.seconds | ||
infos[MPNowPlayingInfoPropertyIsLiveStream] = nowPlayingData.isLive | ||
|
||
infos[MPNowPlayingInfoPropertyPlaybackRate] = nowPlayingData.playbackRate | ||
#if !os(macOS) | ||
if let thumb = nowPlayingData.thumbnailUrl { | ||
if let url = URL(string: thumb) { | ||
updateRemoteArtwork(url: url) | ||
} | ||
} | ||
#endif | ||
MPNowPlayingInfoCenter.default().nowPlayingInfo = infos | ||
} | ||
} | ||
|
||
func updateCurrentTime(currentTime: CMTime) { | ||
self.overrideInformations( | ||
for: MPNowPlayingInfoPropertyElapsedPlaybackTime, | ||
value: currentTime.seconds | ||
) | ||
} | ||
|
||
func updatePlaybackRate(rate: Float) { | ||
self.overrideInformations( | ||
for: MPNowPlayingInfoPropertyPlaybackRate, | ||
value: rate | ||
) | ||
} | ||
|
||
func pause(currentTime: CMTime) { | ||
MPNowPlayingInfoCenter.default().playbackState = .paused | ||
self.overrideInformations(for: MPNowPlayingInfoPropertyElapsedPlaybackTime, value: currentTime.seconds) | ||
} | ||
|
||
func play(currentTime: CMTime) { | ||
MPNowPlayingInfoCenter.default().playbackState = .playing | ||
self.overrideInformations(for: MPNowPlayingInfoPropertyElapsedPlaybackTime, value: currentTime.seconds) | ||
} | ||
|
||
private func overrideInformations(for key: String, value: Any) { | ||
infos[key] = value | ||
MPNowPlayingInfoCenter.default().nowPlayingInfo = infos | ||
} | ||
|
||
#if !os(macOS) | ||
private func getArtwork(image: UIImage) -> MPMediaItemArtwork { | ||
return MPMediaItemArtwork(boundsSize: image.size) { _ in image } | ||
} | ||
|
||
private func updateRemoteArtwork(url: URL) { | ||
RequestsBuilder.getThumbnail(taskExecutor: self.taskExecutor, url: url, completion: { image in | ||
let artwork = self.getArtwork(image: image) | ||
self.overrideInformations(for: MPMediaItemPropertyArtwork, value: artwork) | ||
}, didError: { error in | ||
print("Error on artwork : \(error.localizedDescription)") | ||
}) | ||
} | ||
#endif | ||
} |
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,11 @@ | ||
import CoreMedia | ||
import Foundation | ||
|
||
struct NowPlayingData { | ||
let duration: CMTime | ||
let currentTime: CMTime | ||
let isLive: Bool | ||
let thumbnailUrl: String? | ||
let title: String? = nil | ||
let playbackRate: Float | ||
} |
Oops, something went wrong.