0

I have a video I'm trying to play using MPMoviePlayerController and it loads fine, but cuts out after 5 seconds. I found this post, but it isn't really applicable for swift.

MPMoviePlayerController stops playing the video after 5s

Here is my code.

import MediaPlayer




class ViewController: UIViewController  {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



    var moviePlayer: MPMoviePlayerController?



let url = NSURL(string: "http://path/to/video.m3u8")

    moviePlayer = MPMoviePlayerController(contentURL: url)

    if let player = moviePlayer {

        player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        player.view.sizeToFit()
        player.scalingMode = MPMovieScalingMode.None


        player.movieSourceType = MPMovieSourceType.Streaming
        //player.repeatMode = MPMovieRepeatMode.One


        player.play()

        self.view.addSubview(player.view)

        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "metadataUpdated",
            name: MPMoviePlayerTimedMetadataUpdatedNotification,
            object: nil)

    }

}
Community
  • 1
  • 1
Jace Sparks
  • 121
  • 2
  • 11
  • Is your video coming live or on demand? if not, use MPMovieSourceType.File in place of MPMovieSourceType.Streaming. – deepax11 Jun 16 '15 at 12:49

1 Answers1

2

Could your moviePlayer be going out of scope? Have you tried making it a member variable?

moviePlayer is a local variable of viewDidLoad, so once that function finishes, I don't see any reason why your player would not be deallocated.

If you instead make it a variable of the class, its lifetime will be extended to match your class's lifetime.

something like

class ViewController: UIViewController {

var player: MPMoviePlayerController?

    override func viewDidLoad() {
        // ...
        self.player = MPMoviePlayerController(contentURL: url) // won't go out of scope at end of viewDidLoad()
        // ...
    }
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • Rhythmic Fistman thank you so much for your reply. I'm self taught and still learning. I'm not sure what you mean. Could you please provide more context? I'd greatly appreciate any wisdom you can provide. – Jace Sparks Mar 31 '15 at 03:12
  • you're the man!!! That totally worked. I'm one step closer to achieving my goal of utilizing timed metadata in an m3u8 video. If you have a moment please stop by my [Other Thread](http://stackoverflow.com/questions/29310256/getting-timed-metadata-in-swift-ios-8-from-m3u8-streaming-video). I truly appreciate your time. – Jace Sparks Mar 31 '15 at 23:49
  • Sorry. I thought I did that already. Thank you for bringing it to my attention. Once again. THANK YOU! – Jace Sparks Apr 01 '15 at 00:04
  • No problem! I'll check out your other thread. Looks interesting. – Rhythmic Fistman Apr 01 '15 at 00:05
  • Rhthmic. I just wanted to update you that in my other post my code is now no longer crashing, but I still can't extract the metadata for anything useful. I feel like I'm super close. Thanks again for all of your help. – Jace Sparks Apr 02 '15 at 21:41