Play local GIF using AVPlayer - ios

We are trying to play local GIF using AVPlayer. Does AVPlayer supports GIF format? This is what we tried:
guard let gifUrl = Bundle.main.url(forResource: "myGif", withExtension: "gif") else {
return
}
let gifPlayer = AVPlayer(url: gifUrl)
self.videoContainer.player = gifPlayer
self.videoContainer.player?.play()
Is it possible to play GIF with AVPlayer?
Any help or advice will be highly appreciated. Thank you.
Best Regards, Roi

You can try these steps to play local video using AVPlayer in swift:-
1) First of all add the video into your xcode project.
2) Check your added video in Bundle. Select your project your Target -> Build Phases -> Copy Bundle Resources.
If video is not available in Copy Bundle Resources then you can add by tapping on Plus button.
3) Use the following code in you project.
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}
private func playVideo() {
guard let path = Bundle.main.path(forResource: "video", ofType:"m4v") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
player.play()
}
}
}
Hope this will help you :)

Related

Trouble setting AVLayerVideoGravity with the AVKit for Swift

I am very new to Swift and Xcode and I was trying to use a video as a transition from one scene to another. I have most of the code for this figured out but the size of the video does not match the size of the screen, meaning when the transition occurs the background changes size. I could fix this by changing the AVLayerVideoGravity but the commands in the documentation give me errors, and I haven't found a way to set them properly. I'm sure this is really simple I just can't find the syntax I need anywhere, especially in the newest versions of swift and Xcode so I thought I would ask. Here is the code I have.
import UIKit
import AVKit
class MainRoom: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func Backpack(_ sender: Any) {
if let path = Bundle.main.path(forResource: "OpenBackpack", ofType: "avi", inDirectory: "SegueAnimations")
{
let OpenBackpack = AVPlayer(url: URL(fileURLWithPath: path))
let animPlayer = AVPlayerViewController()
animPlayer.player = OpenBackpack
animPlayer.showsPlaybackControls = true
let avPlayerLayer = AVPlayerLayer(player: OpenBackpack)
//Set avlayervideogravity to resizeaspectfill
present(animPlayer, animated: true, completion:
{
OpenBackpack.play()
})
}
}
}
Thank you for your help!
Found it! I misunderstood the code in another post, this works well
animPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

AVPlayer not loading stream url but browser loads it no problem

i need some help with playing stream on really simple ios app which i've made but i can load the stream into the browser and there won't be any problem, but when i try to load it into the ios app its only loading and i don't know what i did wrong.
url of the stream: https://tv1.cdn.netbadgers.com
ViewController.swift
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func playBroadcastClicked(_ sender: Any) {
let videoURL = URL(string: "https://tv1.cdn.netbadgers.com")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated:true) {
playerViewController.player!.play()
}
}
}
You url is not pointing to a video file, it is actually a HTML page with a video which will never play in the iOS AVPlayer.
Find the direct link to the video, also, make sure the video is in a supported format. If you want a quick solution, just add a WKWebView to your ViewController and then load your url.

Playing Local video with Avplayer/kit Issue

Whenever I run the project, it pulls up the video player, but the video doesn't play
.
How do I fix this? In case the code in the picture isn't clear enough, here is the code:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
let avPlayerViewController = AVPlayerViewController()
var avPlayer:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from nib.
let urlPathString = Bundle.main.path(forResource: "small", ofType: "mp4")
if urlPathString != nil {
let movieUrl = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
self.avPlayer = AVPlayer(url: movieUrl as URL)
self.avPlayerViewController.player = self.avPlayer
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playButtonTapped(_ sender: AnyObject) {
//Trigger the video to play
self.present(self.avPlayerViewController, animated: true) { () -> Void in self.avPlayerViewController.player?.play() }
}
}
The solutions suggested in the comment, and the one #Guan has answered are both correct.
Your urlPathString is actually nil here,
To make it work -
1- Either add the Target Membership, as suggested in the comment and the answer above or
Do the following
1.1- Delete the small.mp4 from bundle, make sure you move it to trash
2.1 - Go to trash and drag and drop your mp4 file into the project again, you will get a window like this -
While adding it back make sure you -
Check the projectTarget checkbox like the screenshot below -
Make sure the file name is small.mp4
Make sure your urlPathString is not nil.
I don't know what the following code mean:
let movieUrl = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
I think the simulator can not get the path of file which in your mac.(I'm not sure, I haven't do this before).
You can check the file's target membership.
The code may work like this:
var urlPaht = Bundle.main.url(forResource: "small", withExtension:"mp4")
if urlPaht == nil {
urlPaht = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
}
self.avPlayer = AVPlayer(url: movieUrl as URL)
self.avPlayerViewController.player = self.avPlayer

streaming videos from google drive in swift 2

im trying to stream videos from google drive but its not working
this is my code
func playVideo () {
let url = NSURL(string: "https://drive.google.com/file/d/0B9XuPgWwSkzrdHN5WWUydjJScDg/preview")!
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player?.play()
}
}
i get this when i start the function image here
i solved the problem by Decodeing the html from the link and take the steam link from it
you can see the code here : https://github.com/DevZaid/GDSLink
your link is to flash video, not to some video supported by AVPlayer.

Play RTSP on Mac OS X from IP Camera - Swift

Since I spend the whole day going through Apple's Documentation and search on google how to connect and play a feed/stream through my IP camera which I can access it through the IP(can access it through my browser), I was wondering if anyone can point me to the right direction?
All the relative searches are leading me to the iOS SDK.
I have already imported AVFoundation and UIKit on my project. And I already know that the Mac OS X SDK has an attribute called AVPlayerView.
What I am asking here is how to do the following, on an application written in Swift, for the Mac:
let moviePath = NSBundle.mainBundle().pathForResource("sample_iPod", ofType: "m4v")
if let path = moviePath {
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
if let validPlayer = playerViewController.player {
validPlayer.play()
}
}
}
Obviously my path will be of type let fileUrl = NSURL(string: "hhtp://10.1.xx.xx")
I found out that AVPlayerViewController is not on the MacOS.
Also, I saw that lately the QTPlayer(or whatever is called) has moved into AVFoundation for the Mac
Thanks in advance..!
This is what I have so far
import Cocoa
import AVKit
import AVFoundation
class ViewController: NSViewController {
#IBOutlet var avPlayerView: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
let asset = AVAsset(URL: NSURL.fileURLWithPath("http://10.1.xx.xx"))
let item = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: item)
avPlayerView.player = player
player.play()
}

Resources