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>
Related
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.
I get a white screen when I try to present MPMediaPickerController. After that, it automatically dismisses itself. I can't access music library in order to import songs in my app.
What is really weird is that there is no error printed out in the console.
I am testing on a physical iPhone device.
#IBAction func importSong(_ sender: Any) {
let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes: MPMediaType.music)
mediaPicker.delegate = self
mediaPicker.allowsPickingMultipleItems = false
self.present(mediaPicker, animated: true, completion: nil)
}
See video : https://youtu.be/8fjeWXjObPo
Add NSAppleMusicUsageDescription in your Info.plist file, e.g.:
<key>NSAppleMusicUsageDescription</key>
<string>App wants to use music</string>
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.
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.
Tired of posting questions on SO about the same problem and still not getting solution..If anyone solves this i am gonna award bounty to user afterwards.
I am trying to play video that is on server.A video from the Url(which is commented) gets played but the other dont.What am i doing wrong??
class VideoViewController: UIViewController {
var moviePlayer:MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
//var url = NSURL(string: "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8")
//playVideoFromURL(url!)
var url = NSURL(string: "private url")
playVideoFromURL(url!)
}
func playVideoFromURL( address: NSURL ) {
println(address)
var playerVC = MPMoviePlayerViewController(contentURL: address)
playerVC.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(playerVC, animated: false, completion: nil)
playerVC.moviePlayer.prepareToPlay()
playerVC.moviePlayer.play()
}
}
Movie player plays the video of the commented ones but doesnot plays the other.What may be the reason for this?
Your code is correct. All you need is to update your info.plist file by adding this key:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
Or you can add it another way and it will look like: