xcode 8 || AVPlayer is showing video however it's not playing - ios

I have this simple code to play an online video. After doing the HTTP permission option, it did start to show the video and I can scroll through it, however, it doesn't play at any point. Here is the code. I would appreciate if someone could help me with this.
regards,
A Beginner Coder
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) {
let movieUrl: NSURL? = NSURL(string: "http://clips.vorwaerts-gmbh.de/VfE_html5.mp4")
playerView = AVPlayer(url: movieUrl as! URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
}
}

When I ran your code in a sample project the debugger prints:
"App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file."
The two options I found were:
1) Change the scheme to https:// in your URL
or
2) Adjust the App Transport Security settings in your Info.plist
I successfully played the video using both options with your code.

It turns out that the only thing i had done was, the last line had
"self.playerViewController.player?.play()" ----->>> "?" and when i changed it to --------->>> "!"
IT MAGICALLY STARTED WORKING....
i have seen the meme on the internet where they talk about coders not knowing why the code works and why it doesn't... same happpened to me i guess.

Related

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.

Cant play video in my ios app

I want to have video player in my ios application (I work with xcode version 9.2 and swift version 4). It must play m3u8 video from internet. I use the code below for this:
import UIKit
import AVKit
import AVFoundation
class ViewControllerPlayer: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let video = AVPlayer(url: videoURL!)
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
present(videoPlayer, animated: true, completion: {
video.play()
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}
but unfortunately after compile my app crashes and doesnt play video. Then I transfered code from viewDidLoad() method to viewDidAppear(). But there is no effect. So, how can i play video from internet in my ios app? Thanks.
Documentation for present method says the following about completion:
The block to execute after the presentation finishes. This block has no return value and takes no parameters. You may specify nil for this parameter.
Move video.play() above present and it should work.
However you are using HTTP, so ATS will complain about it and won't show video.
Add (BAD PRACTICE) the following code to your Info.plist to disable ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

Strange behavior of AVPlayer

I noticed a very odd behavior when working with AVPlayer from AVFoundation framework. I'm trying to stream (play) some mp3 radio file from internet.
So I created a new project in Xcode and added the following code to the default ViewController:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let player: AVPlayer = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let asset = AVAsset(url: URL(string: "https://rfcmedia.streamguys1.com/Newport.mp3")!);
let playerItem = AVPlayerItem(asset: asset);
player.replaceCurrentItem(with: playerItem)
player.play();
}
}
There's nothing particularly interesting going on there. I just create an AVPlayerItem instance with the radio station URL and give it to AVPlayer and then ask the player object to play the song and it works just as expected.
However if I remove the player object instantiation from the class block and put it in the viewDidLoad method the code simply doesn't work (doesn't play anything) yet it doesn't crash or spit out any kinds of errors or warning.
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let player: AVPlayer = AVPlayer()
let asset = AVAsset(url: URL(string: "https://rfcmedia.streamguys1.com/Newport.mp3")!);
let playerItem = AVPlayerItem(asset: asset);
player.replaceCurrentItem(with: playerItem)
player.play();
}
}
I can't understand this behavior. Why is that happening? Is it limited to AVPlayer or I should watch out for cases like this?
As Andrea Mugnaini explained in the comments to my post, my second example is going to cause an immediate deallocation by the ARC (Automatic Reference Counting) as we don't keep a strong reference to the AVPlayer object.
This is a common pitfall of developers new to Apple's ecosystem.

Unable to play .mp4 video file using AVPlayer

I am using the AVPlayer to play the video from url.
func playVideoFromUrl(urlString: String){
let videoURL = URL(string: urlString)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
delegate.window?.rootViewController?.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
The url of video:- "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4"
I am not getting any warning at console. Below is the screenshot of simulator. What I am doing wrong?
I have accomplished what you've wanted to do. Here is my approach:
First import AVFoundation, AVKit(I believe you had) then I have configured my viewcontroller like this
#IBOutlet weak var avPlayerView: UIView! // drag a UIView to view controller
var videoPlayer = AVPlayer()
var avPlayerViewController = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
let videoURL = URL(string: "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4")
self.playVideo(url: videoURL!)
}
playVideo function is configured like below
func playVideo(url: URL) {
self.videoPlayer = AVPlayer(url: url)
self.avPlayerViewController = AVPlayerViewController()
self.avPlayerViewController.player = self.videoPlayer
avPlayerViewController.view.frame = avPlayerView.frame
self.addChildViewController(avPlayerViewController)
self.view.addSubview((avPlayerViewController.view)!)
}
Open your info.plist as Source Code
then add this to info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
screen shot is given below
Have you tried adding arbitrary load flag in your info.plist file as i see the url is a HTTP url and to make the HTTP url work, we need to specify it in app transportation security via info.plist as follows
Please try doing this as video is working perfectly fine with you code.
Read following document for more info on App Transportation Security
I hope this will help you :)
I recently found out that a device running iOS 14 using AVPlayer cannot play .mp4 files. A compatible video format that works on AVPlayer is .mov.
I also have had a trouble playing .mp4 files. When I made the URL end with .mp4 it works.

Streaming Video via Swift 3

Right now my task is to stream a video from a site like this http://watchers.to/sazktfmnvzqc.html on an iPhone. (Be warned, there are tons of pop ups)
I've tried using a UIWebView to simply open up a portal for clicking the video at that URL, but honestly they're so many pop ups its actually impossible.
Is there any clever way to circumvent this that you all can think of? Like code that I could use to instantly click underneath the popup overlay or even click on the overlay the required amount of times, instantly reloading back from the new page to stream the video? Just looking for a direction to go.
Thanks!
First you have to know the exact URL of the video. Then you can use an AVPlayerViewController, which needs an AVPlayer initialised with this URL.
Example:
import AVFoundation // this you need for the `AVPlayer`
import AVKit // this is for the `AVPlayerViewController`
let viewController = AVPlayerViewController()
let videoURL = "http://you.videourl.com"
if let player = AVPlayer.playerWithURL(videoURL) as? AVPlayer {
viewController = player
}
Then you can push the view controller and play the video.
I got if from here.
Worked for me.
https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/GettingStarted/GettingStarted.html
func play (){
//Your video path URL
guard let url = URL(string: "http://www.xxx.xxx/xx/xxxx/xxxxxxx.mov") else {return}
let player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
}

Resources