Most effective method for video as background in iOS

前端 未结 5 1667
不知归路
不知归路 2020-12-24 03:30

Perhaps you have noticed one of the latest trend in iOS-apps: Using videos as backgrounds - mainly at login- or \"first launch\" screens. Yesterday I attempted to mimic this

5条回答
  •  孤城傲影
    2020-12-24 03:36

    Best way is to use AVFoundation then you control the video layer itself

    In header file declare @property (nonatomic, strong) AVPlayerLayer *playerLayer;

    - (void)viewDidLoad {
          [super viewDidLoad];
    
    
          [self.view.layer addSublayer:self.playerLayer];
    
          // loop movie
          [[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(replayMovie:)
                                                 name: AVPlayerItemDidPlayToEndTimeNotification 
                                                 object:nil];
    }
    -(AVPlayerLayer*)playerLayer{
          if(!_playerLayer){
    
             // find movie file
             NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"arenaVideo" ofType:@"mp4"];
             NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
             _playerLayer = [AVPlayerLayer playerLayerWithPlayer:[[AVPlayer alloc]initWithURL:movieURL]];
             _playerLayer.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
             [_playerLayer.player play];
    
          }
        return _playerLayer
    }
    -(void)replayMovie:(NSNotification *)notification
    {
        [self.playerLayer.player play];
    }
    

    Swift 2.0

    lazy var playerLayer:AVPlayerLayer = {
    
        let player = AVPlayer(URL:  NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("LaunchMovie", ofType: "mov")!))
        player.muted = true
        player.allowsExternalPlayback = false
        player.appliesMediaSelectionCriteriaAutomatically = false
        var error:NSError?
    
        // This is needed so it would not cut off users audio (if listening to music etc.
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
        } catch var error1 as NSError {
            error = error1
        } catch {
            fatalError()
        }
        if error != nil {
            print(error)
        }
    
        var playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.frame
        playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"
        playerLayer.backgroundColor = UIColor.blackColor().CGColor
        player.play()
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"playerDidReachEnd", name:AVPlayerItemDidPlayToEndTimeNotification, object:nil)
        return playerLayer
        }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.layer.addSublayer(self.playerLayer)
    }
    override func viewWillDisappear(animated: Bool) {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    // If orientation changes
    override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
        playerLayer.frame = self.view.frame
    }
    func playerDidReachEnd(){
        self.playerLayer.player!.seekToTime(kCMTimeZero)
        self.playerLayer.player!.play()
    
    }
    

    Tested on iOS7 - iOS9

提交回复
热议问题