Sunday, December 21, 2014

Make your first Swift Video App

Want to know how to play a simple video in Swift?

Create your application:

XCode->File->New->Project


Select Single View Application.

Give a suitable name to your project.  Select language as Swift.  For right now, we select the device as iPhone.  We won't be using Core Data for this application.


You will be displayed the project properties page and under the targets ->build phases tab ->Copy Resources sub menu ->insert your video



Add your video here .. mine is called cat.mp4.

Then go to your ViewController.swift

Import the MediaPlayer package to get all video playing functions.  The bolded lines below is what you need to add:

 //  
 // ViewController.swift  
 // SampleVideo  
 //  
 // Created by APARNA SRIDHAR on 12/22/14.  
 // Copyright (c) 2014 APARNA SRIDHAR. All rights reserved.  
 //  
   
 import UIKit  
 import MediaPlayer  
   
 class ViewController: UIViewController {  
     
   var moviePlayer : MPMoviePlayerController?  
   override func viewDidLoad() {  
     super.viewDidLoad()  
     playVideo()  
   }  
   
   override func didReceiveMemoryWarning() {  
     super.didReceiveMemoryWarning()  
     // Dispose of any resources that can be recreated.  
   }  
   
   func playVideo() {  
       
     //Get the Video Path  
     //You need to put this in Project->Target->copy bundle resource for this to work  
     let videoPath = NSBundle.mainBundle().pathForResource("cat", ofType:"mp4")  
       
     //Make a URL from your path  
     let url = NSURL.fileURLWithPath(videoPath!)  
       
     //Initalize the movie player  
     moviePlayer = MPMoviePlayerController(contentURL: url)  
       
     if let player = moviePlayer {  
         
       //Make the player scale the entire view  
       player.view.frame = self.view.bounds  
       player.scalingMode = .AspectFill  
         
       //Add it as a subView to your currentView  
       self.view.addSubview(player.view)  
         
       //Play the video  
       player.prepareToPlay()  
   
     }  
     else {  
       println("Movie player couldn't be initialized")  
     }  
   }  
   
 }  
   

Compile and project and see your video being played !

Download complete project here - https://github.com/aparnasridhar/SampleVideo

3 comments:

  1. Nero has indeed left a mark in their specific niche, CD/DVD burning software. As far as 1997, the Nero program is the one stop shop for burning ISO and DMG files, ripping CDs and DVDs Burn video app promo code, among others.

    ReplyDelete
  2. Thanks for the always useful information. This is great information to help peoples and nice article written by writer. CnX Player is a powerful & efficient 4K ultra HD enabled video player for Windows 10 PC & Tablet, Android and iOS – iPhone & iPad.

    Download Media Player for Windows 10 - Microsoft Store
    Download Video Player for Android from Google Play
    Download Video Player for iPhone/iPad from Apple App Store

    ReplyDelete