Last active
September 18, 2020 08:58
-
-
Save jacks205/2fc1d9ba0f2ded613440 to your computer and use it in GitHub Desktop.
Video Blur Background in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// citizencrossfit | |
// | |
// Created by Mark Jackson on 17/11/2014. | |
// Copyright (c) 2014 Mark Jackson. All rights reserved. | |
// | |
import UIKit | |
import Alamofire | |
import MediaPlayer | |
import AVFoundation | |
class ViewController: UIViewController { | |
var moviePlayer : MPMoviePlayerController? | |
var avPlayer : AVPlayer? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
println(self.view) | |
// Do any additional setup after loading the view, typically from a nib | |
// NSNotificationCenter.defaultCenter().addObserver(self, selector: "myMovieFinishedCallback:", name: MPMoviePlayerPlaybackDidFinishNotification, object: self.moviePlayer) | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: "myMovieFinishedCallback:", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.avPlayer) | |
playVid("iphone", type:"MOV") | |
// playVideo("iphone", type: "MOV") | |
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light)) as UIVisualEffectView | |
visualEffectView.frame = self.view.bounds | |
visualEffectView.autoresizingMask = .FlexibleHeight | .FlexibleWidth | |
self.view.addSubview(visualEffectView) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
//With Audio | |
func playVideo(videoName : String, type: String) { | |
let path = NSBundle.mainBundle().pathForResource(videoName, ofType:type) | |
let url = NSURL.fileURLWithPath(path!) | |
moviePlayer = MPMoviePlayerController(contentURL: url) | |
if let player = moviePlayer { | |
player.view.frame = self.view.bounds | |
player.controlStyle = MPMovieControlStyle.None | |
player.prepareToPlay() | |
player.scalingMode = .AspectFill | |
self.view.addSubview(player.view) | |
} | |
println("it started!") | |
} | |
//Without Audio | |
//http://stackoverflow.com/questions/15948661/how-to-mute-unmute-audio-when-playing-video-using-mpmovieplayercontroller | |
func playVid(videoName: String, type: String){ | |
let filePath = NSBundle.mainBundle().pathForResource(videoName, ofType: type) | |
let asset : AVURLAsset? = AVURLAsset(URL: NSURL(fileURLWithPath: filePath!), options: nil) | |
var audioTracks = NSArray() | |
audioTracks = asset!.tracksWithMediaType(AVMediaTypeAudio) | |
var allAudioParams : NSMutableArray = NSMutableArray() | |
// Mute all the audio tracks | |
for track in audioTracks{ | |
var audioInputParams : AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters() | |
audioInputParams.setVolume(0, atTime: kCMTimeZero) | |
audioInputParams.trackID = track.trackID | |
allAudioParams.addObject(audioInputParams) | |
} | |
let audioZeroMix : AVMutableAudioMix = AVMutableAudioMix() | |
audioZeroMix.inputParameters = allAudioParams | |
// Create a player item | |
let playerItem : AVPlayerItem = AVPlayerItem(asset: asset) | |
playerItem.audioMix = audioZeroMix | |
// Create a new Player, and set the player to use the player item | |
// with the muted audio mix | |
let player : AVPlayer = AVPlayer.playerWithPlayerItem(playerItem) as AVPlayer | |
self.avPlayer = player | |
self.avPlayer?.play() | |
let layer = AVPlayerLayer(player: self.avPlayer) | |
self.avPlayer?.actionAtItemEnd = .None | |
layer.frame = self.view.bounds | |
layer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
self.view.layer.insertSublayer(layer, below: self.view.layer) | |
} | |
func myMovieFinishedCallback(note : NSNotification){ | |
println("movie ended!") | |
let p : AVPlayerItem = note.object as AVPlayerItem | |
p.seekToTime(kCMTimeZero) | |
// if(note.object as MPMovieController == self.moviePlayer!){ | |
// let reason : NSInteger = note.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]!.integerValue | |
// if (reason == MPMovieFinishReason.PlaybackEnded.rawValue){ | |
// self.moviePlayer?.play() | |
// } | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment