Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

ios - Playing a video file from a server in Swift

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...