Playing a video file from a server in Swift

2019-01-16 05:33发布

问题:

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!

回答1:

Solved:

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

   }
}


回答2:

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()
        }


    }


回答3:

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



回答4:

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.