Playing a video file from a server in Swift

前端 未结 4 1788
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 12:42

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

I have imported MediaPlayer framework, here is my code:

import UIKit
import M         


        
4条回答
  •  日久生厌
    2020-12-07 13:09

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

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

提交回复
热议问题