udemy the professional paid from development courses beginner app ios swift video mpmovieplayercontroller playback

ios - the - Reproducción de un archivo de video desde un servidor en Swift



udemy 7 (4)

Intento reproducir un video de un servidor usando Swift.

He importado el framework MediaPlayer , aquí está mi código:

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

Solo obtengo una caja negra cuando corro en el simulador pero no se reproduce ningún video, sin importar de dónde trate de cargar un video.

ACTUALIZAR

Aquí está el código actual

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

¡Este código, de manera interesante, reproduce ~ 2 segundos de video antes de volverse negro!


Importar las bibliotecas:

import AVKit import AVFoundation import MediaPlayer import AudioToolbox

Establecer delegado como este

AVPlayerViewControllerDelegate

Wrtie código bonito donde quieres jugar como acción de botón:

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% de trabajo y prueba


Resuelto

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



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