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:
Related
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>
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.
There is no error message when playing the sound. I put a print statement for testing, and the url is getting the right path. The play button clicks, and I hear a faint click sometimes, but that's it. No other sound comes out. I've tried different mp3 files. I get an error message if I delete the mp3 file. I've checked the volume.
The play button is in a second view controller. The main view controller has a table view. When user taps on the cell, it takes him to this view controller.
// DetailVC.swift
import AVFoundation
import UIKit
class DetailVC: UIViewController {
var duaPlayer = "" //is this correct???
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func playbutton(sender: AnyObject) {
do {
let url = NSBundle.mainBundle().URLForResource("track1", withExtension: "mp3")
var duaPlayer = try AVAudioPlayer(contentsOfURL: url!)
duaPlayer.prepareToPlay()
print (url)
duaPlayer.volume = 0.5
duaPlayer.play()
}
catch
{ fatalError("err")}
}
i think that AVAudioPlayer do not support streaming. For live streaming use AVPlayer
//try this, it should work
var player = AVPlayer()
func configureView() {
let url = "http://yoururl.com"
let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()
}
in the other hand , with AVAudioPlayer you can download the mp3 file first than play it -->
let url = "http://YourURL/music/music.mp3"
let fileURL = NSURL(string:url)
let soundData = NSData.dataWithContentsOfURL(fileURL, options: nil, error: nil)
var error: NSError?
self.player = AVAudioPlayer(data: soundData, error: &error)
if player == nil {
if let e = error {
println(e.localizedDescription)
}
}
player.prepareToPlay()
player.volume = 1.0
player.delegate = self
player.play()
PS: Please keep in mind since iOS 9: App Transport Security (ATS) must be configured to accept non ssl connection.
add this code to your info.plist->
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
I have an MP3 file, and I want to be able to play this file with 'Media Player' Framework. How do I upload this file onto a URL? I tried putting it on YouTube, then putting the YouTube URL in the app, but it does not work. What do I do?
Here is my code for the app:
import UIKit
import MediaPlayer
class AudioViewController: UIViewController {
var movie:MPMoviePlayerViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.playAudio("https://www.youtube.com/embed/tQsxOtH8-RQ")
}
func playAudio(URL:String){
let movieurl:NSURL? = NSURL(string: "\(URL)")
if movieurl != nil {
self.movie = MPMoviePlayerViewController(contentURL: movieurl!)
}
if self.movie != nil {
self.presentViewController(self.movie!, animated: true, completion: nil)
self.movie?.moviePlayer.play()
}
}
}
Your code is fine. The code snippet below also works. The current problem you are experiencing is due to trying to play a youtube/vimeo stream in the MPMoviewPlayerViewController. If you want to stream from those sites, you will have to use a UIWebView. There are some custom players on GitHub and elsewhere, but if you will use the snippet below and replace the URL with an mp3 hosted on your own site (i.e. WordPress) somewhere, it will play just fine.
var url = NSURL(string: "non-youtube/vimeo URL")
var mediaPlayerController = MPMoviePlayerViewController(contentURL: url)
self.presentViewController(mediaPlayerController, animated: true, completion: nil)
mediaPlayerController.moviePlayer.prepareToPlay()
mediaPlayerController.moviePlayer.play()
Hope this helps.
FYI, make sure you add the MediaPlayer Framework and add the import MediaPlayer line to the top of your class.