New in ios, using Swift 2.0, I have video file in Amazon S3, I use HanekeSwift for download and "cache" that video file and then play it, the issue is that when I play the video, the file written by hakene it isn't yet available, so nothing plays (I'm reusing this player SCPlayer).
How can I get notified when that video file is ready and available for play it?
PS: I've tried using PromiseKit for "play it" in "future" but no luck :(, here the sample of code:
Main call:
// prepare the promise downloads and play first one
when(self.prepareDownloads()).then {
self.playVideo(0)
}
I used this function as a way to know wich videos has been downloaded, and before play the video check if exists inside self.downloaded dictionary
func prepareDownloads() {
if let videos = self.videos {
// iterate the video list for create all the promise
for (index, video) in videos.enumerate() {
let promise = { () -> Promise<NSURL> in
return Promise<NSURL> { fulfill, reject in
log.debug("download video \(index)")
// request the video and cache it
let cache = Shared.dataCache
let resource = NSURL(string: (reaction.objectForKey("resourceURL") as! String))!
cache.fetch(URL: resource).onSuccess { data in
// get the cached file
let location = NSURL(string: DiskCache.basePath())!.URLByAppendingPathComponent("shared-data/original")
let diskCache = DiskCache(path: location.absoluteString)
let file = NSURL(fileURLWithPath: diskCache.pathForKey(resource.absoluteString))
self.downloaded.updateValue(file, forKey: index)
fulfill(file)
}.onFailure { _ in
log.error("error downloading video")
}
}
}
// save it in dictionary
self.downloads.updateValue(promise, forKey: index)
}
}
}
Verify videos before play it
func playVideo(index: Int) {
// hasn't been downloaded?
if (self.downloaded[index] == nil) {
// call the promise to download and then play
if let promise = self.downloads[index] {
promise().then { path in
// play video
self.playMedia(index, filePath: path)
}
}
} else {
// the video has been downloaded
self.playMedia(index, filePath: (self.downloaded[index])!)
}
}
Play the video
func playMedia(index: Int, filePath path: NSURL) {
// play the specific video
self.player.setItemByStringPath(path.absoluteString)
// THIS PRINT "FALSE" CUZ FILE DONT EXISTS YET :/
print(NSFileManager.defaultManager().fileExistsAtPath(path.path!))
self.player.play()
log.debug("playing video \(index)")
}
Related
I am having a weird situation and have no clue how to handle this , I am downloading the videos from firestorage and caching into device for future use , meanwhile the background thread is already doing its job , I am passing a video url to the function to play the video. The issue is that sometimes avplayer is playing the right video and sometimes taking some other video url from the cache.
you can find the code in below :
func cacheVideo(for exercise: Exercise) {
print(exercise.imageFileName)
guard let filePath = filePathURL(for: exercise.imageFileName) else { return }
if fileManager.fileExists(atPath: filePath.path) {
// print("already exists")
} else {
exercise.loadRealURL { (url) in
print(url)
self.getFileWith(with: url, saveTo: filePath)
}
}
}
writing file here
func getFileWith(with url: URL, saveTo saveFilePathURL: URL) {
DispatchQueue.global(qos: .background).async {
print(saveFilePathURL.path)
if let videoData = NSData(contentsOf: url) {
videoData.write(to: saveFilePathURL, atomically: true)
DispatchQueue.main.async {
// print("downloaded")
}
} else {
DispatchQueue.main.async {
let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])
print(error.debugDescription)
}
}
}
}
now playing the video using this
func startPlayingVideoOnDemand(url : URL) {
activityIndicatorView.startAnimating()
activityIndicatorView.isHidden = false
print(url)
let cachingPlayerItem = CachingPlayerItem(url: url)
cachingPlayerItem.delegate = self
cachingPlayerItem.download()
// cachingPlayerItem.preferredPeakBitRate = 0
let avasset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: avasset)
let player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
initializeVideoLayer(for: player)
}
any suggestions would be highly appreciated.
this was solved because the data model which i was using to download bunch of videos files was accessed in background thread and meanwhile i was trying to assign the url to the same data model class in order to fetch the video and play in avplayer. Hence this was the issue and resolved by simply adding a new attribute into data model for assigning the url to play right away.
I attempted the following caching mechanism to cache videos which are added in cells in my app:
import Foundation
public enum Result<T> {
case success(T)
case failure(NSError)
}
class CacheManager {
static let shared = CacheManager()
private let fileManager = FileManager.default
private lazy var mainDirectoryUrl: URL = {
let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
return documentsUrl
}()
func getFileWith(stringUrl: String, completionHandler: #escaping (Result<URL>) -> Void ) {
let file = directoryFor(stringUrl: stringUrl)
//return file path if already exists in cache directory
guard !fileManager.fileExists(atPath: file.path) else {
completionHandler(Result.success(file))
return
}
DispatchQueue.global().async {
if let videoData = NSData(contentsOf: URL(string: stringUrl)!) {
videoData.write(to: file, atomically: true)
DispatchQueue.main.async {
completionHandler(Result.success(file))
}
} else {
DispatchQueue.main.async {
let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])
completionHandler(Result.failure(error))
}
}
}
}
private func directoryFor(stringUrl: String) -> URL {
let fileURL = URL(string: stringUrl)!.lastPathComponent
let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)
return file
}
}
Used like this for each cell if at that index it is a video:
CacheManager.shared.getFileWith(stringUrl: videoURL) { result in
switch result {
case .success(let url):
let player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x: -8, y: 0, width: 138, height: 217)
cell.imageOrVideoView.layer.addSublayer(playerLayer)//seems to use the the first video for all videos...
cell.profImage.sd_setImage(with: URL(string: "\(self.postArray[indexPath.item].user.profileImageUrlString!)"), placeholderImage: UIImage(named: "media"))
break;
// do some magic with path to saved video
case .failure(let error):
print(error, " failure in the Cache of video")
break;
// handle errror
}
}
The problem with this is that now the second video URL is used as the video (which I don't play) for every single video cell.
Do I need to reload cache? This seems wrong...
I have found that this seems to be the problematic code:
//return file path if already exists in cache directory
guard !fileManager.fileExists(atPath: file.path) else {
completionHandler(Result.success(file))
return
}
When commenting out the guard each cell does get the correct video. The problem is that it does not cache it. How can I fix this?
Edit:
I found something weird which may be a symptom of the problem. if at the index path the cell should be a video, then I print video else it must be a image print image. When looking in the console however, I see that image gets printed 5 times and video gets printed 2, even though there are 11 cells in the collection view. Also I can confirm that regardless of which it is I print "in here" which gets printed 7 times. Again, this all when (when you scroll), there are 11 cells all of which should be different but of course all the video ones have the second videos first frame.
What happens is when the first cell is dequeued you get it with it's video layer added successfully then when you scroll this callback
CacheManager.shared.getFileWith(stringUrl: videoURL) { result in
is very slow so the previous cell content appears at that time making the fact that it's the second cell in all cells and that because of cell dequeuing
You have to clear the previous layers before the above line which is caused by
cell.imageOrVideoView.layer.addSublayer(playerLayer)
Edit: after the dequeue line do
cell.imageOrVideoView.layer.sublayers?.forEach {
if $0 is AVPlayerLayer {
$0.removeFromSuperlayer()
}
}
The problem seemed to be tied to using the cache manager for each cell. and the fact that I download a video for each cell. Instead used the thumbnail for each image which I then had to save.
I using Google example for work with Chromecast.
Working with mp4 files is correct.
But if I want to use the m3u8 format, the video will not start playing.
If I rewind the video by 10 seconds ahead, it starts playing. But it doesn't play when rewinding from 0 to 9 seconds, even if you rewind before forward for more than 10 seconds.
This is my code:
func playVideoRemotely() {
GCKCastContext.sharedInstance().presentDefaultExpandedMediaControls()
let url = URL(string: "http://www.streambox.fr/playlists/test_001/stream.m3u8")
guard let mediaURL = url else {
print("invalid mediaURL")
return
}
let mediaInfoBuilder = GCKMediaInformationBuilder(contentURL: mediaURL)
mediaInfoBuilder.contentID = "http://www.streambox.fr/playlists/test_001/stream.m3u8"
mediaInfoBuilder.streamType = GCKMediaStreamType.buffered
mediaInfoBuilder.contentType = "video/m3u8"
mediaInformation = mediaInfoBuilder.build()
guard let mediaInfo = mediaInformation else {
print("invalid mediaInformation")
return
}
if let request = sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInfo) {
request.delegate = self
}
}
I have not development on an iOS chromecast sender, but have done it on web. If you want the video to autoplay once it is loaded then I believe you need to set the autoplay field on the load options.
I try to convert the code which i use to fetch image asset to fetch mp3 file on cloudkit. However, i can't figure the data part. I'm using this library to play audio. It only has one class called "AudioPlayer" so if i want to play music on local folder, it is enough to declare it.
https://github.com/tbaranes/AudioPlayerSwift
func loadSong(completion:#escaping (_ song: AudioPlayer?) -> ()) {
// 1
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
var song: AudioPlayer!
defer {
completion(song)
}
// 2
guard let asset = self.record["Song_File"] as? CKAsset else {
return
}
let songData: AudioPlayer
do {
songData = try Data(contentsOf: asset.fileURL)
} catch {
return
}
song = AudioPlayer(contentsOf: <#T##URL#>)
}
}
Actually, i can't do clearly what i want. (I wanted to stream a song from CloudKit, this one downloads the ckasset). Below code gets the URL of CkAsset so that i can put it in AVPlayer and play it.
func loadSongURL(completion:#escaping (_ url: URL?) -> ()) {
// 1
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
var song_url: URL!
defer {
completion(song_url)
}
// 2
guard let asset = self.record["Song_File"] as? CKAsset else {
return
}
let songURL: URL
do {
print(asset.fileURL)
songURL = asset.fileURL
} catch {
return
}
song_url = songURL
We are using video streaming in our Swift IOS application and it works very well. The problem is that we would like to use AVAssetResourceLoader so we can make the requests to the streaming server using our own URLSession rather than whatever AVPlayer uses (I have been completely unable to find out what session AVPlayer uses or how to influence what session it uses.)
The exact behavior is that shouldWaitForLoadingOfRequestedResource is called once for two bytes on on the .m3u8 file then it is called again asking for the whole file, and then (based on the start time) the correct .ts file is requested. After we fetch the .ts file the video player simply doesn't do anything further.
This happens whether or not we use a sample streaming video file "https://tungsten.aaplimg.com/VOD/bipbop_adv_example_v2/master.m3u8" or our own server. The sequence is identical if we don't use AVAssetResourceLoader (we can tell from our server.) right up until the .ts file is requested. At that point when we don't use the custom loader the AvPlayer brings up the video and keeps requesting .ts files. If we comment out all other interactions with the AVPlayer including setting the initial time the behavior is identical so I am only going to include the code from viewDidLoad and shouldWaitForLoadingOfRequestedResource.
Again if we simply remove the "xyzzy" prefix so that AVAssetResourceLoader isn't used, everything works. Also, I guess importantly, if we target a video file that is not a streaming file everything works either way.
One more thing our transformation of the mime type for the .ts file produced some kind of weird dynamic uti, but this doesn't seem to have anything to do with the problem because even if we hardcode the uti the same thing happens.
override func viewDidLoad() {
super.viewDidLoad()
avPlayer = AVPlayer()
avPlayerLayer = AVPlayerLayer(player: avPlayer)
videoView.layer.insertSublayer(avPlayerLayer, at: 0)
videoView.backgroundColor = UIColor.black
url = URL(string: "xyzzy" + currentPatient.videoURL())!
let asset = AVURLAsset(url: url)
asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
let item = AVPlayerItem(asset: asset)
let avPlayerItem = item
avPlayer.replaceCurrentItem(with: avPlayerItem)
videoScrollView.delegate = self
videoScrollView.minimumZoomScale = 1.0
videoScrollView.maximumZoomScale = 6.0
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
let urlString = loadingRequest.request.url?.absoluteString
let urlComponents = urlString?.components(separatedBy: "xyzzy")
let url = URL(string: urlComponents![1])
let request = loadingRequest.dataRequest!
let infoRequest = loadingRequest.contentInformationRequest
let task = globalSession.dataTask(with: url!) { (data, response, error) in
self.avPlayerThread.async {
if error == nil && data != nil {
let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, response?.mimeType as! CFString, nil)
if let infoRequest = infoRequest {
infoRequest.contentType = uti?.takeRetainedValue() as? String
if request.requestsAllDataToEndOfResource == false {
infoRequest.contentLength = Int64(request.requestedLength)
} else {
infoRequest.contentLength = Int64((data?.count)!)
}
infoRequest.isByteRangeAccessSupported = true
}
if infoRequest == nil || request.requestsAllDataToEndOfResource == true {
loadingRequest.dataRequest?.respond(with: data!)
}
loadingRequest.finishLoading()
} else {
print ("error \(error)")
loadingRequest.finishLoading(with: error)
}
}
}
task.resume()
return true
}