2016-02-05 22 views
17

Ziel ist die Wiedergabe von Videodateien (* .mp4) in einer UIView ohne Steuerelemente.Wie Video in einer UIView ohne Steuerelemente, wie ein Hintergrund/ein Hintergrundbild wiedergeben?

Es dient als Hintergrund/Hintergrundbild auf dem ViewController und anderen Steuerelementen, d. H. Tabellenansicht, Textfelder, Bilder werden über der Ansicht mit Videowiedergabe angezeigt.

Was ist der bessere Weg, dies zu tun? Danke

+0

Ist es lokales Video oder You Tube Video mit URL? – Vidhyanand

+0

wird eine lokale Datei in das Projekt importiert. Tatsächlich gibt es mehrere Dateien (Videos) jeweils für separate ViewController –

+0

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

Antwort

38

ich das Ziel mit dem nativen AVPlayer

1.Used AVFoundation erreicht haben:

#import <AVFoundation/AVFoundation.h> 

2.Used Eigenschaft für Spieler:

@property (nonatomic) AVPlayer *avPlayer; 

3.Added Videodatei in den Ordner "Video" und hinzugefügt "Video" in das Projekt

den Spieler

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 für Event 4.Initialized - Video bis zum Ende gespielt hat

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

6.Resumed Video

- (void)itemDidFinishPlaying:(NSNotification *)notification { 
    AVPlayerItem *player = [notification object]; 
    [player seekToTime:kCMTimeZero]; 
} 
1
in verwandten Verfahren von Anfang an spielen

Swift

In Swift ist es ähnlich. Fügen Sie das Video zu Ihrem Ressourcenpaket hinzu. Meine vollere Antwort ist 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() 
    } 
} 
Verwandte Themen