MPMoviePlayerController doesn't display video - ios

I saw a lot of topics there and always the same problem: it's not displaying at all.
So because I am little stubborn, I want it more
This is my code :
let moviePath = NSBundle.mainBundle().pathForResource("back", ofType:"mp4");
println(moviePath!)
let movieURL = NSURL.fileURLWithPath(moviePath!)
let theMoviePlayer = MPMoviePlayerController(contentURL: movieURL!)
self.view.addSubview(theMoviePlayer.view)
theMoviePlayer.prepareToPlay()
theMoviePlayer.shouldAutoplay = true
theMoviePlayer.fullscreen = true
theMoviePlayer.play()
I checked and I have the movie file in the app's bundle resources.
The video dimension : 640 × 360 pixels
So where it can be from?
Did I forget something important ?
Edit
This new code works but always some problems :
let url = NSBundle.mainBundle().URLForResource("back", withExtension: ".mp4")
let player = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(player)
player.moviePlayer.prepareToPlay()
player.moviePlayer.repeatMode = MPMovieRepeatMode.One
player.moviePlayer.shouldAutoplay = true
player.moviePlayer.play()
It show me the video in landscape view and not full screen.

This is how I did it in one of my apps. You might need to call presentMoviePlayerViewControllerAnimated() and pass in the MPMoviePlayerViewController instead of doing addSubview.
let url = NSBundle.mainBundle().URLForResource("back", withExtension: ".mp4")
let player = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(player)
player.prepareToPlay()
player.shouldAutoplay = true
player.fullscreen = true
player.moviePlayer.play()

** Resolved **
I resolved addend the line :
player.moviePlayer.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI / 2))
Of course, it do a rotation of the video. So If it's a simple background animated like clouds or something simple, it's ok.

if there is no issue with the content type of video, call below function. It just works. :)
private func presentVideoVC(){
var url: NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
var moviePlayerVC: MPMoviePlayerViewController! = MPMoviePlayerViewController(contentURL: url)
self.presentMoviePlayerViewControllerAnimated(moviePlayerVC)
}

my two cents:
in case of ios9 use https.. (or add exceptions... not good.)
dont call directly on viewDidload, it does not work, call in async to wait some time, allowing controller to be set:
dispatch_async(dispatch_get_main_queue()){
self.playIt()
}
func playIt() {
let urlString = "https://www.ingconti.com/softysit/Spinelli_short.mp4"
let url: NSURL = NSURL(string: urlString)!
let moviePlayerVC: MPMoviePlayerViewController! = MPMoviePlayerViewController(contentURL: url)
moviePlayerVC.moviePlayer.shouldAutoplay = true
self.presentMoviePlayerViewControllerAnimated(moviePlayerVC)
}
note that now MPMoviePlayerViewController is deprecated.

Related

Video player shown with a cross mark on the Play button in iOS swift AVPLayer?

I am trying to play a video form my Server but when I try to do so I get a Crossed mark on pay button.
All other files like .ppt,.keynote etc work fine when I use them with my Server url and use a WKWebview to show them but .mp4 files are not working.
I tried 2 methods.
1) Playing directly using WKWebview.
2) Download the file and saving it in a local path and then using AVPlayer to play the file.
In both cases I get same crossed mark on the play button.
func playVideo(url:String){
var newUrl:String = url
if !( url.hasPrefix("http") || url.hasPrefix("https") ){
let host = kGetHostName()
newUrl = host + newUrl
}
do
{
let videoData = NSData(contentsOf: URL(string: newUrl)!)
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let path = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("myMove.mp4")
if videoData == nil{
print("No data in this")
return
}
videoData?.write(toFile: path.absoluteString, atomically: true)
let playeritem = AVPlayerItem(url: path)
let player = AVPlayer(playerItem: playeritem)
let controller = AVPlayerViewController()
controller.player = player
self.present(controller, animated: true) {
player.play()
}
} catch {
print(error)
}
And when trying with WKWebview
func loadWebView(url:String){
var newUrl:String = url
if !( url.hasPrefix("http") || url.hasPrefix("https") ){
let host = kGetHostName()
newUrl = host + newUrl
}
self.webView.load(URLRequest(url: URL(string: newUrl)!))
}
Both cases fail and get this. Is there any way I can play the video without changing my plist settings as I want to download and play ?
1.Screen Shot
2.Screen Shot

MP4 video as background

I am trying to create something like vine app has.
This is what I got so far:
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("WelcomeVideo", ofType: "mp4")
let pathURL = NSURL.fileURLWithPath(path!)
let myPlayer = MPMoviePlayerViewController(contentURL: pathURL)
myPlayer.moviePlayer.repeatMode = .One
presentMoviePlayerViewControllerAnimated(myPlayer)
}
But the problem is that it shows the playbutton and such.. Any suggestions?
Have you tried with something like this ?
myPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone

blank screen appears when click on UILabel in swift

i'm trying to open iPhone call screen with selected number when click on UIlabel which is hyperlink in swift.
a blank screen appears for 1 second and goes. No iPhone call screen appears. Don't know what is wrong in my code.
Code in viewDidLoad
let strPhone = "080 2854 2105"
let attributedPhoneStr = NSMutableAttributedString(string:strPhone, attributes:[NSLinkAttributeName: NSURL(string: "http://aerospace.honeywell.com")!])
lblPhoneContact.attributedText = attributedPhoneStr
lblPhoneContact.userInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))
lblPhoneContact.addGestureRecognizer(gestureRecognizer)
Function labelPressed
func labelPressed() {
let url:NSURL = NSURL(string: "tel://08028542105")!
UIApplication.sharedApplication().openURL(url)
}
what is wrong or missing in my code.
I tried running on my iPhone device.
for swift use
let url:NSURL = NSURL(string: "telprompt:08028542105")!
instead of
let url:NSURL = NSURL(string: "tel://08028542105")!
You can try to use the GCD's dispatch_async to invoke the "openURL" method:
dispatch_async(dispatch_get_main_queue()) { () -> Void in
UIApplication.sharedApplication().openURL(url)
}
You should omit the // from the uri - I.e. It should just be
let url:NSURL = NSURL(string: "tel:08028542105")!

Controlling UIWebView

In my IOS app i am using a webview to play audio from my website. My problem is that webview plays automatically when i don't even click on the webview. It says in the ios docs that mediaPlaybackRequiresUserAction is equal to true by default but it still plays. I have tried even setting mediaPlaybackRequiresUserAction to true in my code but that still doesn't solve the problem. I need to stop it from playing without user action because when the user goes to the next screen the webview automatically plays the audio. I have looked around on stack overflow and none of the solutions i found seem to help with this problem.
Here is my code:
//function to get NSURLrequest
func configureView() -> NSURLRequest {
let fileNameDownload: AnyObject = self.detailItem!
let fileNameURL = (fileNameDownload as NSString)
let spaces = fileNameURL.componentsSeparatedByString(" ")
if (spaces.count > 0) {
let fileNameURL = fileNameURL.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: NSMakeRange(0, fileNameURL.length))
println(fileNameURL)
let url = NSURL(string: fileNameURL)
let request = NSURLRequest(URL: url!)
return request
}
else {
let url = NSURL(string: fileNameURL)
let request = NSURLRequest(URL: url!)
return request
}
}
override func viewDidLoad() {
super.viewDidLoad()
previewWebView.loadRequest(configureView())
}

MPMoviePlayerController shows only black screen - Swift

I have a View inside a ViewController that I want to add a move player controller to it. But so far I'm only getting a black screen.
I'm running this on the simulator, and using the apple dev stream which I tested in Safari and it works.
The videoView is added to the controller through an IBOutlet. I'm using Xcode 6 beta 7.
This is all inside a UIViewController.
Declaration of videoView 320x320 (global):
#IBOutlet var videoView: UIView!
Declaration of MPMoviePlayerController (global):
var videoPlayer : MPMoviePlayerController = MPMoviePlayerController()
Adding videoPlayer to View:
videoURLWithPath = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
let videoURL = NSURL(fileURLWithPath: videoURLWithPath)
videoPlayer.movieSourceType = MPMovieSourceType.Streaming;
videoPlayer.contentURL = videoURL
videoPlayer.view.frame = videoView.bounds
videoView.addSubview(videoPlayer.view)
videoPlayer.controlStyle = MPMovieControlStyle.Embedded
videoPlayer.prepareToPlay()
videoPlayer.play()
Storyboard:
Simulator:
Update:
I've also tried doing this. Super simple. And I'm still getting the same result. The frame size is set that way so that I can see that the player has actually been added.
let streamURL = NSURL(string: "http://www.thumbafon.com/code_examples/video/segment_example/prog_index.m3u8")
var streamPlayer = MPMoviePlayerController(contentURL: streamURL)
streamPlayer.view.frame = CGRect(x: 10, y: 10, width: 200, height: 200)
streamPlayer.controlStyle = MPMovieControlStyle.Embedded
videoView.addSubview(streamPlayer.view)
streamPlayer.play()
I ended up ditching MPMoviePlayerController and opted to use AVFoundation instead.
Working example:
Global declarations:
var player : AVPlayer? = nil
var playerLayer : AVPlayerLayer? = nil
var asset : AVAsset? = nil
var playerItem: AVPlayerItem? = nil
inside viewDidLoad:
let videoURLWithPath = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
let videoURL = NSURL(string: videoURLWithPath)
asset = AVAsset.assetWithURL(videoURL) as? AVAsset
playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: self.playerItem)
playerLayer = AVPlayerLayer(player: self.player)
playerLayer!.frame = videoView.frame
videoView.layer.addSublayer(self.playerLayer)
player!.play()
Rather than using MPMoviePlayerController, this works perfectly for me:
// import MediaPlayer
var moviePlayer : MPMoviePlayerViewController?
#IBAction func playVideo() {
let path = NSBundle.mainBundle().pathForResource("ra3", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
let videoURL = url
moviePlayer = MPMoviePlayerViewController(contentURL: videoURL )
if let player = moviePlayer {
player.view.frame = self.view.bounds
self.presentViewController(moviePlayer!, animated: true, completion: nil)
}
else {
NSLog("no player")
}
}

Resources