Swift: Video and image player (pager) - ios

I work on an iOS app that bring videos and images from server, each object have many images and videos, I need to show these images and videos in a slider (pager) where user can slide to get the next one, and I need these videos and images cached in device, I google that and I found this pod but it not play video which is a few seconds infinitely, and I try to play the provided video URL in example of pod and it's also have the same issue, are there any alternative solution or any solution for this pod?

First of all create a UIView outlet for display image or Video then you can use this code `
let avPlayer = AVPlayer()
let videolink = (videosString[indexPath.row] as? String)!
let videoURL = NSURL(string: videolink)
avPlayer = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: avPlayer)
playerLayer.frame = CGRect(x: cell.videoview.frame.origin.x, y: cell.videoview.frame.origin.x, width: cell.videoview.frame.size.width, height: cell.videoview.frame.size.height)
cell.videoview.layer.addSublayer(playerLayer)
avPlayer.play()`
like videoVideo is a view where you wish to play a video(avplayer will add a layer to that view)
To present Image, you can use SKPhotoBrowser
to differentiate Image and Video you can use bool
Hope you got it
If you need any help ask me i can code for you

Related

Play Video From Instagram

recently I have see that one of my apps feature stopped working. The main problem is that, now, I can play instagram videos from an URL. This feature was working time ago, but I can not say how time is broken because I have noticed the problem now.
For example, I have this URL from Instagram:
https://embed.storiesig.net/aHR0cHM6Ly9zY29udGVudC1hdGwzLTIuY2RuaW5zdGFncmFtLmNvbS9vMS92L3QxNi9mMS9tMTcvQzQ0NjhFNDI4NTkwN0JCM0M3QkREQjFEQ0MzMTJCODhfdHJhbnNjb2RlX291dHB1dF9kYXNoaW5pdC5tcDQ/ZWZnPWV5SnhaVjluY205MWNITWlPaUpiWENKcFoxOTNaV0pmWkdWc2FYWmxjbmxmZG5SelgyOTBabHdpWFNJc0luWmxibU52WkdWZmRHRm5Jam9pZG5SelgzWnZaRjkxY214blpXNHVOekl3TG5OMGIzSjVMbVJsWm1GMWJIUWlmUSZfbmNfaHQ9c2NvbnRlbnQtYXRsMy0yLmNkbmluc3RhZ3JhbS5jb20mX25jX2NhdD0xMDUmdnM9MTc4NzE0ODIzOTk2MjExNTBfMzM4ODI0NTY4MiZfbmNfdnM9SEJrY0ZRSVlXR2xuWDNOMGIzSnBaWE5mY0dWeWJXRnVaVzUwWDNCeWIyUXZRelEwTmpoRk5ESTROVGt3TjBKQ00wTTNRa1JFUWpGRVEwTXpNVEpDT0RoZmRISmhibk5qYjJSbFgyOTFkSEIxZEY5a1lYTm9hVzVwZEM1dGNEUVZBQUxJQVFBb0FCZ0FHd0dJQjNWelpWOXZhV3dCTVJVQUFDYjBvdWpjbG8zRVB4VUNLQUpETXl3WFFCUUFBQUFBQUFBWUVtUmhjMmhmWW1GelpXeHBibVZmTVY5Mk1SRUFkZWdIQUElM0QlM0QmY2NiPTktNCZvZT02MUIzRURDRSZvaD05ZjIyMmE3NWYxZmE1ZmYzNDlhZGM4MWNhNzFhZjExNCZfbmNfc2lkPWJhYjYzOA==
If you open the URL in web browser (Safari, Chrome, etc...) you will see the video running.
But If I try to play the video from a AVPlayer like it used to work, the video is not played. My code for video playback is like this:
let videoUrl = story.videoUrl!
if (videoUrl.absoluteString.lengthOfBytes(using: .utf8) > 0) {
let player = AVPlayer(url: videoUrl)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
}
Where story.videoUrl is the URL of the example. If yo play the URL in a AVPlayer you will see nothing, so I don't know what is going on.

Error while playing You Tube videos in IOS Swift app

My following code plays the "fox village.mp4" video, which is commented out, but not the you tube link and gives this error:
AddInstanceForFactory: No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46
Please help if you can. Thanks!!
let videoURL = URL(string: "https://youtu.be/MTNCcC_H3oM")!
// let videoURL = URL(string: "https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.mp4")!
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
player.play()
The first thing you need to clear that AVPlayer does not have the capacity to play youtube video from URL.
Note:
One thing you need to keep in your mind, AVPlayer is able played video if URL is direct downloadable.
In your case, you need to go with below solutions
Solution: 1
To play a youtube video, You need to go with the WKWebView or WKWebView with iFrame
OR
I suggest you need to go with YTPlayerView
OR
How to embed a Youtube video into my app?

AVKit unable to stream Dropbox Link Videos

I have been using AVKit to stream a video, below is the code I am using to stream videos in TVOS. But the problem is I am unable to do the same with the Dropbox Link? Is a Dropbox API integration required for this?
func _viewVideo(_ url: Foundation.URL)
{
let url = Foundation.URL(string: "http://techslides.com/demos/sample-videos/small.mp4")
let asset = AVURLAsset(url: url)
let item = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: item)
playerViewController?.player = player
playerViewController?.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
playerViewController?.showsPlaybackControls = true
self.view.addSubview((playerViewController?.view)!)
player.play()
}
You can stream videos from Dropbox share links in Apple TV, however, you need to make a tiny change in the link.
You need to force a file to download in your link, just change the end of the link from dl=0 to dl=1. In this way, Apple TV can stream videos from Dropbox share links.
For reference: Force a file or folder to download, or to render on dropbox.com

AVPlayer not playing local mp4

I am trying to use an AVPlayer to play a video that has been recorded in my app. However, the player won't play the video. I know for a fact that this is a properly recorded mp4 file, because I can take it and play it on my Mac just fine. Here's the setup for the player:
let documents = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let URL = NSURL(fileURLWithPath: "tempVideo", relativeToURL: documentsDirectory)
let asset = AVAsset(URL: URL)
let item = AVPlayerItem(asset: asset)
//videoPlayer is a property on the view controller being used
videoPlayer = AVPlayer()
//videoPlayerLayer is a property on the view controller being used
videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
videoPlayerLayer.frame.size = view.frame.size
videoPlayerLayer.backgroundColor = UIColor.redColor().CGColor
view.layer.addSublayer(videoPlayerLayer!)
//wait 5 seconds
videoPlayer.play()
I know for sure that the videoPlayer is, in fact, ready to play, because I've checked its status property. I also know that videoPlayerLayer has properly been added to view.layer because its visible and takes up the whole screen. When I call videoPlayer.play(), the music playing on the device stops, but videoPlayerLayer doesn't show anything.
Any ideas? Thank you in advance for the help!
EDIT: I forgot to show that videoPlayerLayer is indeed connected to videoPlayer, I have updated my question to reflect this.
The correct answer was given by #Dershowitz123, but he or she left it in a comment so I can't mark it as correct. The solution was to change the URL to include the .mp4 extension. Thank you for your help.

Fetch Video Detail From HTTP Live Streaming using AVPlayerViewController

I am currently working on HTTP Live streaming video with AVPlayerViewController / AVPlayer
i am play a video with .m38u file supported
It playing fine but my question is that can i have the data of video like i have set 4 type of resolution while genrating the .m38u file.. , i can varies the resoltion at my end point now the question is how to get the all values which i have setup at my endpoint. I am play this is in android also and using Track i am able to fetch all the video information but in ios how can i fetch all the details containing the video like its height , with, track,resolution supported by video etc..
I have search alot but could not get succeed..
Need help
Thanks in advance
Anita, I start I hope here is slice of code for playing a VIDEO..
self.command.text = "Loading VIDEO"
let videoURL = self.currentSlide.aURL
self.playerItem = AVPlayerItem(URL: videoURL)
self.player = AVPlayer(playerItem: self.playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)
self.streamPlayer = AVPlayerViewController()
self.streamPlayer.player = self.player
self.streamPlayer.view.frame = CGRect(x: 128, y: 222, width: 512, height: 256)
let currentFM = self.streamPlayer.player?.currentItem?.asset
for blah in (currentFM?.metadata.enumerate())! {
print("blah \(blah)")
}
self.presentViewController(self.streamPlayer, animated: true)
self.streamPlayer.player!.play()
}
I added a little extra for showing meta data about the video... it printed...
blah (0, <AVMetadataItem: 0x15e7e4280, identifier=itsk/gsst, keySpace=itsk, key class = __NSCFNumber, key=gsst, commonKey=(null), extendedLanguageTag= (null), dataType=com.apple.metadata.datatype.UTF-8, time={INVALID}, duration= {INVALID}, startDate=(null), extras={
dataType = 1;
dataTypeNamespace = "com.apple.itunes";
}, value=0>)
blah (1, <AVMetadataItem: 0x15e7e1a50, identifier=itsk/gstd, keySpace=itsk, key class = __NSCFNumber, key=gstd, commonKey=(null), extendedLanguageTag=(null), dataType=com.apple.metadata.datatype.UTF-8, time={INVALID}, duration={INVALID}, startDate=(null), extras={
dataType = 1;
dataTypeNamespace = "com.apple.itunes";
}, value=222980>)
Hopefully it means a lot more to you than it does to me :) What your looking I think is classed/called metadata in Applespeak...
Take a look at this post too
Capture still image from AVCaptureSession in Swift
It describes how-to capture a frame of your video, once you have a frame you can take a closer look at its meta data and I suspect find out some of the details you seek.
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
Are the commands to try and fetch that... let me know if you manage to succeed!

Resources