41

I'm trying to play a video from a server using Swift.

I have imported MediaPlayer framework, here is my code:

import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

   override func viewDidLoad() {
        super.viewDidLoad()

        var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")        
        var moviePlayer = MPMoviePlayerController(contentURL: url)

        moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)        
        self.view.addSubview(moviePlayer.view)

        moviePlayer.fullscreen = true        
        moviePlayer.controlStyle = MPMovieControlStyle.Embedded        
   }
}

I only get a black box when I run in the simulator but no video is playing no matter where I try to load a video from.

UPDATE

Here is the current code

var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")

var moviePlayer = MPMoviePlayerController(contentURL: url)

moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)        
moviePlayer.movieSourceType = MPMovieSourceType.File

self.view.addSubview(moviePlayer.view)        
moviePlayer.prepareToPlay()
moviePlayer.play()

This code interestingly plays ~2 seconds of video before going black again!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mate Hegedus
  • 2,887
  • 1
  • 19
  • 30
  • 2
    possible duplicate of [MPMoviePlayerController showing black empty screen](http://stackoverflow.com/questions/8429701/mpmovieplayercontroller-showing-black-empty-screen) – s1m0n Jul 30 '14 at 10:34
  • The issue seems to be the same but none of the solutions posted there helped. Maybe there is something different with Swift and Objective-C in this case? – Mate Hegedus Jul 30 '14 at 12:22
  • @s1m0n not sure how you think this is a duplicate of that question since that one was about objective-c and this one is about swift. – Popeye Jul 30 '14 at 13:05
  • 1
    @Popeye The answers to that question are just as relevant here; this about the MediaPlayer API, not the language. In particular, if I take the above code and change it so that it keeps a reference to the moviePlayer in a property, it works fine. Keeping the instance around until you're done playing the video is suggested in at least three of the answers to that question. – Matt Gibson Jul 30 '14 at 13:10
  • @MattGibson I would disagree the subject matter here is swift not objective-c the answer may help but the question isn't a duplicate. It's like saying this question in Java is a duplicate of this question in C# when they are not. I totally disagree with you here. Please note I don't disagree that the answers may help I disagree that the question is a duplicate. – Popeye Jul 30 '14 at 13:16
  • Problem solved by putting moviePlayer in a property. – Mate Hegedus Jul 30 '14 at 13:32
  • 2
    Well, it turns out this question was a duplicate since the answer is **exactly** the same. This question is about the MediaPlayer framework and not about Swift itself. The answer is exactly the same regardless of you using Swift or Objective-C: maintain a strong reference to the `MPMoviePlayerController`. This is about the framework and has nothing to do with the used language. A lot of questions about the .Net framework are also marked as duplicates regardless of you using VB, C# or C++ since the language used to implement the solution is totally irrelevant to these questions. – s1m0n Jul 30 '14 at 14:56
  • 2
    Note that the answer of 'Toms Shaji Mathew' could be pasted here without changing a single word and it would be a great, perfectly valid answer solving this question. – s1m0n Jul 30 '14 at 14:56
  • I had a little search on Meta and found multiple examples of this situation were a question was marked as a duplicate even though the language was completely different and they all near enough said the same thing if it is marked as a different language whether the questions seem the same they are different questions based on the language even if the answers may in turn be the same. – Popeye Jul 31 '14 at 08:50
  • http://meta.stackexchange.com/questions/143845/should-these-related-c-and-c-questions-be-marked-as-duplicates http://meta.stackexchange.com/questions/183074/i-am-not-sure-this-question-was-closed-as-a-duplicate-correctly?rq=1 http://meta.stackexchange.com/questions/207207/different-questions-for-same-problem-in-different-language-duplicate-or-not This has been discussed many times on Meta the above are just a few and in all cases they have said the same different language means not a duplicate. – Popeye Jul 31 '14 at 08:52
  • @Popeye [This is probably the best place to discuss that issue](http://meta.stackoverflow.com/questions/258413/how-to-handle-cross-language-questions). – Matt Gibson Jul 31 '14 at 10:50

4 Answers4

71

UPDATE 2019, Swift 4:

MPMovieControlStyle' was deprecated in iOS 9.0: Use AVPlayerViewController in AVKit.

So, following the advice, let's change it. Original answer from 2016 is below.

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
        
        playVideo(url: url)
    }
    
    func playVideo(url: URL) {
        let player = AVPlayer(url: url)
        
        let vc = AVPlayerViewController()
        vc.player = player
        
        self.present(vc, animated: true) { vc.player?.play() }
    }
}

The original answer:

import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

   var moviePlayer:MPMoviePlayerController!

   override func viewDidLoad() {
      super.viewDidLoad()

      let url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")

      moviePlayer = MPMoviePlayerController(contentURL: url)
      moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)

      self.view.addSubview(moviePlayer.view)
      moviePlayer.fullscreen = true

      moviePlayer.controlStyle = MPMovieControlStyle.Embedded

   }
}
Mate Hegedus
  • 2,887
  • 1
  • 19
  • 30
17
import AVKit
import AVFoundation

class VideoController: UIViewController

override func viewDidLoad()

    {
       let videoURL = NSURL(string: "VideoUr")
        let player = AVPlayer(url: videoURL! as URL)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }


    }
ReactiveRaven
  • 7,203
  • 2
  • 29
  • 38
shaiju mathew
  • 433
  • 5
  • 8
9

Import the libraries:

import AVKit
import AVFoundation
import MediaPlayer
import AudioToolbox   

Set delegaete like this

AVPlayerViewControllerDelegate

Wrtie pretty code where you want to play like button action:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: videoURL!)
    let playervc = AVPlayerViewController()
    playervc.delegate = self
    playervc.player = player
    self.present(playervc, animated: true) {
        playervc.player!.play()
    }

100% working and test

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
2

I just was having the same issues..

MPMoviePlayerController Stops Playing After 5 seconds - Swift

The issue is your var moviePlayer is going out of scope. By declaring it outside of viewDidLoad like @Victor-Sigler did above, you prevent the black screen issue from happening.

Community
  • 1
  • 1
Jace Sparks
  • 121
  • 2
  • 11