2016-02-05 23 views
17

L'obiettivo è riprodurre il file video (* .mp4) all'interno di un UIView senza controlli.Come riprodurre video all'interno di un UIView senza controlli, come uno sfondo/sfondo?

servirà come sfondo/sfondo sul ViewController e altri controlli, vale a dire Tableview, campi di testo, le immagini verranno mostrate sopra la vista con il video playedback.

Qual è il modo migliore per fare questo? Grazie

+0

È Video locale o YouTube Video che utilizza URL? – Vidhyanand

+0

sarà il file locale importato nel progetto. In realtà ci saranno più file (video) ciascuno per ViewController separata –

+0

Fare riferimento http://stackoverflow.com/questions/17922017/display-video-inside-the-uiwebview-not-in-device-full-screen – Vidhyanand

risposta

38

ho raggiunto l'obiettivo con il nativo AVPlayer

1.Used AVFoundation:

#import <AVFoundation/AVFoundation.h> 

2.Used proprietà per il giocatore:

@property (nonatomic) AVPlayer *avPlayer; 

3.Added file video in "Video" cartella e ha aggiunto "Video" in progetto

4.Initialized il giocatore

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"shutterstock_v885172.mp4" ofType:nil inDirectory:@"Video"]; 
NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
self.avPlayer = [AVPlayer playerWithURL:fileURL]; 
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer]; 
videoLayer.frame = self.view.bounds; 
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
[self.view.layer addSublayer:videoLayer]; 

[self.avPlayer play]; 

5.Subscribed per l'evento - il video ha giocato fino alla fine

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]]; 

il video 6.Resumed giocare fin dall'inizio in modo correlato

- (void)itemDidFinishPlaying:(NSNotification *)notification { 
    AVPlayerItem *player = [notification object]; 
    [player seekToTime:kCMTimeZero]; 
} 
1

Swift

In Swift è simile. Aggiungi il video al tuo gruppo di risorse. La mia risposta più completa è here.

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    var player: AVPlayer? 

    @IBOutlet weak var videoViewContainer: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     initializeVideoPlayerWithVideo() 
    } 

    func initializeVideoPlayerWithVideo() { 

     // get the path string for the video from assets 
     let videoString:String? = Bundle.main.path(forResource: "SampleVideo_360x240_1mb", ofType: "mp4") 
     guard let unwrappedVideoPath = videoString else {return} 

     // convert the path string to a url 
     let videoUrl = URL(fileURLWithPath: unwrappedVideoPath) 

     // initialize the video player with the url 
     self.player = AVPlayer(url: videoUrl) 

     // create a video layer for the player 
     let layer: AVPlayerLayer = AVPlayerLayer(player: player) 

     // make the layer the same size as the container view 
     layer.frame = videoViewContainer.bounds 

     // make the video fill the layer as much as possible while keeping its aspect size 
     layer.videoGravity = AVLayerVideoGravity.resizeAspectFill 

     // add the layer to the container view 
     videoViewContainer.layer.addSublayer(layer) 
    } 

    @IBAction func playVideoButtonTapped(_ sender: UIButton) { 
     // play the video if the player is initialized 
     player?.play() 
    } 
}