AVPlayer not loading stream url but browser loads it no problem - ios

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.

Related

Customize AVPlayerViewController

I'm using AVPlayerViewController to play video in my application,
I don't want to make the player from scratch.
is there a way to add some other extra UI components and functionality to the AVPlayerViewController?
class AVKitViewController: AVPlayerViewController {
private let url =
URL(string:
"https://www.digikala.com/mag/wp-content/uploads/2019/09/Call-of-Duty-Modern-Warfare-Story-Trailer.mp4")
override func viewDidLoad() {
super.viewDidLoad()
let video = AVPlayer(url: url!)
player = video
player?.play()
}
}
It looks like there is no way to do such thing. the only way possible, is to use AVFoundation to create our own AVPlayer from scratch.

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.

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

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.

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

how to play a video in fullscreen mode using swift ios?

I've a url = http//www.dd.com/mysong.mp4, and I've a UIView controller in portrait mode of iphones using Auto layout and size classes. and I need to rotate the viewController and then play my video in fullscreen. because I cant use orientation, cause constraints wont support. it is Compact and regular size class. how to play video in fullscreen using AV PLAYER. Atleast help me with playing video in fullscreen what ever the things may be? Any related stuff Please
You can create instance of and present it.
import AVKit
import AVFoundation
func playVideo(urlString: String) {
guard let url = URL(string: urlString) else {return}
DispatchQueue.main.async {
let player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true, completion: {
playerViewController.player!.play()
})
}
}

Resources