Play a local video automatically in in a UIView [duplicate] - ios

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()
}

Related

Play local GIF using AVPlayer

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 :)

Play several videos using AVPlayer in a view

I have an UIView which contains some some texts and views. in each view I need to play a video with AVPlayer , but my problem is only the first view shows the video :
Here is my code :
func playVideo(name:String , onView:UIView) {
let path = Bundle.main.path(forResource: name, ofType: "mp4")
let videoURL = URL(fileURLWithPath: path!)
let player = AVPlayer(url: videoURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = onView.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
onView.layer.addSublayer(playerLayer)
player.play()
}
Using the function :
override func viewDidLoad() {
super.viewDidLoad()
//ScrollView
scrollView.contentSize = menuView.frame.size
scrollView.addSubview(menuView)
//Play videos
playVideo(name: "pizza", onView: videoView1)
playVideo(name: "salad", onView: videoView2)
playVideo(name: "fries", onView: videoView3)
}
when I run the app video only play in videoView1 , any suggestion why this happens ?
I also put my code in viewWillAppear , viewDidLayoutSubviews
According to Apple doc:
class AVPlayer
Note
AVPlayer is intended for playing a single media asset at a time.
I just found an alternative solution , I replaced UIView with UIContainerView and then link them with Embed to other view controller and play video in each view controller. Now it works perfectly as I expected.

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()
}

Play YouTube videos from my tvOS application

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()
}
}
}

AVPlayerViewController - UIButton not triggering video

I am using AVPlayerViewController in order to play a .mov file (TeethingFinal.mov) in my project. The funny thing is, I have the feature working perfectly with two other buttons on the same ViewController but for some reason will not launch the AVPlayer! The tap gesture is being recognized, because I can see the button highlight when pressed but nothing happens. I would post error message but there is not one!
#IBAction func sugarBugPlay(sender: UIButton) {
let moviePlayerController = AVPlayerViewController()
let path = NSBundle.mainBundle().pathForResource("TeethingFinal", ofType:"mov")
let url = NSURL.fileURLWithPath(path!)
let playerVC = AVPlayerViewController()
var player: AVPlayer = AVPlayer()
playerVC.player = AVPlayer(URL: url)
self.presentViewController(playerVC, animated: true, completion: nil)
}
So sometimes Xcode is funny. I created a completely new ViewController the exact same way and reattached all of my outlets and it worked!

Resources