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()
}
Related
I am trying to write a rather simple iOS app synchronizing the playback of a video with AVPlayer (from AVFoundation) to audio playback from multiple AKPlayers (Audiokit 4.0), using the .setRate function of AVPlayer together with CMClockGetHostTimeClock() and mach_absolute_time().
For some reason all my attempts end in the error message
AVPlayer cannot service a synchronized playback request via setRate:time:atHostTime: until its status is AVPlayerStatusReadyToPlay.
Obviously I keep on missing something. The movie plays just fine if I replace everything starting from "let time..." with a simple
videoPlayer.play()
This is a minimal "ViewController.swift" to reproduce the error:
import UIKit
import AudioKit
import AVFoundation
class ViewController: UIViewController {
var videoPlayer = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
guard let videopath = Bundle.main.url(forResource: "test", withExtension: "mov") else {
debugPrint("test.mov not found")
return
}
videoPlayer = AVPlayer(url: videopath)
videoPlayer.automaticallyWaitsToMinimizeStalling = false
let playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = self.view!.bounds
self.view!.layer.addSublayer(playerLayer)
let time: TimeInterval = 1 // 1 second in the future
videoPlayer.masterClock = CMClockGetHostTimeClock()
let hostTime = mach_absolute_time()
let cmHostTime = CMClockMakeHostTimeFromSystemUnits(hostTime)
let cmVTime = CMTimeMakeWithSeconds(time, preferredTimescale: videoPlayer.currentTime().timescale)
let futureTime = CMTimeAdd(cmHostTime, cmVTime)
videoPlayer.setRate(1, time: CMTime.invalid, atHostTime: futureTime)
}
}
I'm trying to get a video to play in my macOS project and I want it to continuously loop. I was able to get it to play once but I can't figure out how to get it to loop. I'm using the AVPlayerLooper but seem to be messing up somewhere. Here is my code:
import Cocoa
import AVKit
import AVFoundation
class ViewController: NSViewController {
#IBOutlet weak var videoPlayer: AVPlayerView!
override func viewDidLoad() {
//var loop: AVPlayerLooper?
super.viewDidLoad()
guard let path = Bundle.main.path(forResource: "Background", ofType:"mp4") else {
debugPrint("Not found")
return
}
let asset = AVAsset(url: URL(fileURLWithPath: path))
let item = AVPlayerItem(asset: asset)
let queuePlayer = AVQueuePlayer(playerItem: item)
let loop = AVPlayerLooper(player: queuePlayer, templateItem: item)
videoPlayer.player = queuePlayer
videoPlayer.player?.play()
}
}
I also have an AVPlayerView in my storyboard that I have connected to my viewcontroller.
It builds and runs fine but when I run it nothing shows up. I just get a black screen.
Any help is appreciated. Thanks!
AVPlayerLoop is sort of a "top level" object that manages both the player and the player item. So you have to keep it around, e.g. in a custom property of the view controller, or it will be released before it can really do anything
Since AVPlayerLoop manages the items in the AVQueuePlayer, initialize the queue player without passing in the player item
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 :)
This question already has answers here:
How to play a local video with Swift?
(9 answers)
Closed 5 years ago.
I want to play a local video with no controls in a UIView I have imported the frameworks AVFoundation and AVKit into my code. I have also made my class for the UIView, however, I am not sure on how to play the video in my UIView with no controls. I have already found a way to play the video from the internet and that is not what I needed.
In swift 3.0, try this code.
//for Playing Video
#IBAction func btnvideoPlayClicked(_ sender: UIButton) {
self.videoPlay()
}
func videoPlay()
{
let playerController = AVPlayerViewController()
playerController.delegate = self
let bundle = Bundle.main
let moviePath: String? = bundle.path(forResource: "SoSorry", ofType: "mp4")
let movieURL = URL(fileURLWithPath: moviePath!)
let player = AVPlayer(url: movieURL)
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
I want to play YouTube videos from my tvOS application. I found to play URL videos by AVPlayer, but not getting to play YouTube videos since UIWebView is not supported on tvOS. I don't want to use any third party libraries.
You need to use YoutubeSourceParserKit to parse data that you receive from url. Then you will have the url and you will be able to play video with the following code (Swift 2 solution for tvOS 9):
Youtube.h264videosWithYoutubeURL(NSURL.init(string: "https://www.youtube.com/any-video-url")!)
{ (videoInfo, error) -> Void in
if let videoURLString = videoInfo?["url"] as? String,
videoTitle = videoInfo?["title"] as? String {
let videoURL = NSURL(string: videoURLString)
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
}