Video not playing on iPhone - ios

I have just got a simple video going. When I run the app in Simulator the video plays but when I run it on my iPhone it says video missing. Any help would be much appreciated thanks!
Here is my code :
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerviewcontroller = AVPlayerViewController()
var playerview = AVPlayer ()
#IBAction func playMusic(sender: AnyObject) {
var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/32134.mp4")
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

The video file should be added in Xcode project like this:
Then you can get the video url like this:
let movieURL = NSBundle.mainBundle().URLForResource("ElephantSeals", withExtension: "mov")!

Please put the video file into your project bundle. Simply drag and drop the video into your project.
NSURL *url=[[NSBundle mainBundle]URLForResource:#"arreg" withExtension:#"mp4"];
AVPlayer *video=[AVPlayer playerWithURL:url];
AVPlayerViewController *controller=[[AVPlayerViewController alloc]init];
controller.player=video;
[self.view addSubview:controller.view];
controller.view.frame=self.view.frame;
[self addChildViewController:controller];
[video play];

Related

AVPlayerViewController play/pause issue in iOS

I am working on the iOS application. This application base on the NEWS media.
I am playing the small video of highlighted News. I am facing a stranger problem with AVPlayerViewController.
When i play the video everything is fine. When i go to offline the buffer video play perfect & after the play buffer video, the video stops but the progress bar(Slider) continuously in play state while the video has been stopped.
For more clearance please watch this GIF image :
Please visit this link for batter understanding.
This is my code for player.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.playVideo()
}
func playVideo(){
let videoURL = NSURL(string: "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")
let player = AVPlayer(URL: videoURL!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I've tried your code and I couldn't even see picture from your gif.
I've also tried your code with another video link (https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4) and problem wasn't reproduced.
I guess your problem is in the way you try to show your AVPlayerViewController.
Consider next snippet:
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
This should work. If you need some ui customization you shouldn't use AVPlayerViewController I guess.
UPDATE:
Try adding Reachaility support:
class MyViewController: UIViewController {
var reachability: Reachability?
override func viewDidLoad() {
super.viewDidLoad()
reachability = try? Reachability.reachabilityForInternetConnection()
reachability?.whenUnreachable = { _ in
player.pause()
}
try? reachability?.startNotifier()

Button triggering video to open and play

I am very new to coding and have no experience.
I have found a way to make a video play but it autoplays / autopens. I need the video to only open and play when a button is pressed. Please can someone help me tweak my code to do this.
This is my current code:
import UIKit
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerviewcontroller = AVPlayerViewController()
var playerview = AVPlayer ()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/Warm Up/Video View/Video View/ViewController.swift")
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try this:
#IBAction func playMusic(sender: AnyObject) {
var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/Warm Up/Video View/Video View/ViewController.swift")
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
You can simply put it anywhere in your ViewController, as long as it is not inside another function.
Then connect it with a button in your storyboard:
this is how a button would set up to look, this example is pulling a movie from firebase but other than that this is how it would look
#IBAction func playV(sender: UIButton) {
let amovieUrl:NSURL? = NSURL(string: "https://firebasestorage.googleapis.com/v0/b/messenger-test-d225b.appspot.com/o/test%2FTestVideo.mov?alt=media&token=bd4ccba3-b446-43bc-809e-b1152aa3c2ff")
if let aurl = amovieUrl {
self.avPlayer = AVPlayer(url: aurl as URL)
self.avPlayerViewController.player = self.avPlayer
self.present(self.avPlayerViewController, animated: true) { () -> Void in
self.avPlayerViewController.player?.play()
}
}
}
Create an Action Connection as explained here.
Move that code in viewDidAppear to the action you created in step 1.
Profit.
Also, you don't need those default implementation for viewDidLoad and didReceiveMemoryWarning:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerviewcontroller = AVPlayerViewController()
var playerview = AVPlayer ()
#IBAction func yourActionForButton(sender: UIButton) {
// Your code.
}

AVPlayer playback with Swift 2.0

I'm having issues with playing back a video. All I want is for the video to playback after the launch image has been displayed. I followed this (https://www.youtube.com/watch?v=iYATp9--VIM) tutorial but when I simulated I received a blank screen.
Here is the code:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
#IBOutlet var videoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupView() {
let path = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("streetview", ofType: "mov")!)
let player = AVPlayer(URL:path)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoView.frame
self.videoView.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
player.play()
}
}
Please help me if you can!
If you're just getting a blank black screen, it would generally point to an issue with the source of the video. I'd check that you're spelling the name of your file correctly and that it definitely is a .mov file.
I use this code to launch the AVPlayer and play my video. It plays a video from a URL from the internet. I have it inside my button function so it happens when the user presses the play button I have:
let videoURL = NSURL(string: "YOUR URL") {
let player = AVPlayer(URL: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
I can confirm this works for me in Swift 2.0

AvPlayer not playing video in Xcode 7

I've been trying to get this to work but can't seem to find the solution. I want a video to start playing on viewDidLoad. I can get the AVPlayerViewController to show up but no video plays. Any suggestions would be greatly appreciated.
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var videoPlayer = AVPlayerViewController()
let videoPath = NSURL(string: "https://youtu.be/dWYNNQdD6rY")
override func viewDidLoad() {
super.viewDidLoad()
let player = AVPlayer(URL: videoPath!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I believe you need to feed the AVPlayer a URL to either an m3u8 file or directly to a movie. Some example m3u8 files can be found on Stack Overflow and other websites.
Change your video path to be something like this:
let videoPath = NSURL(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")

Implementing video view in the Storyboard

I want to build simple video app thats views a video form youtube links thats users add.
I didn't find "VideoView" I mean If image view is for image what is UIView for videos.
There is no object in the original library that performs video viewing function. But you can import the MediaPlayer framework in your project and add it programmatically.
Here is a Swift example
import MediaPlayer
class Step1ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController?
override func viewDidLoad() {
super.viewDidLoad()
playVideo()
}
func playVideo() {
let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height))
let pathToEx1 = NSBundle.mainBundle().pathForResource("myVideoFile", ofType: "mp4")
let pathURL = NSURL.fileURLWithPath(pathToEx1!)
moviePlayer = MPMoviePlayerController(contentURL: pathURL)
if let player = moviePlayer {
player.view.frame = videoView.bounds
player.prepareToPlay()
player.scalingMode = .AspectFill
videoView.addSubview(player.view)
}
self.view.addSubview(videoView)
}
}
As for further customisations and app notifications it has a bunch of in-build possibilities. So check it out.
To play video apple has provided MPMovieViewController
see this https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html
and this
http://www.brianjcoleman.com/tutorial-play-video-swift/
In youtube Video case we got embedded links are used so for that you will get help from this https://github.com/gilesvangruisen/Swift-YouTube-Player
import AVFoundation
import AVKit
class ViewController: UIViewController {
var player = AVPlayer()
var playerController = AVPlayerViewController()
func playVideo() {
let videoURL = NSURL(string: videoUrl)
player = AVPlayer(url: videoURL! as URL)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
// Add your view Frame
playerController.view.frame = self.view.frame
// Add subview in your view
self.view.addSubview(playerController.view)
player.play()
}
func stopVideo() {
player.pause()
}
}
In Xcode 10 (with a bit less code):
In the interface builder (Storyboard), add "AVKit Player View Controller"
Create a View Controller that derives from AVPlayerViewController:
import UIKit
import AVKit
class VideoPlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!
self.player = AVPlayer(url: videoURL)
}
override func viewDidAppear(_ animated: Bool) {
self.player?.play()
}
}
Don't forget to connect this custom class in the storyboard's identity inspector :)
Note: this example uses video URL pointing to a bundle's resource.

Resources