Monday, October 14, 2019

How to use Video Player (AVPlayer) in iOS? Explain with example.

iOS Video Player

In iOS we can easily implement video player to play videos in swift applications by using AVPlayer method. By adding AVFoundation framework we will get a chance to use AVPlayer in our iOS swift applications. 

We will see how to use AVPlayer in iOS swift applications to play videos with example.

Create iOS Video Player App in Swift

To create new project in iOS open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In welcome window click on the second option “Create a new Xcode Project” or choose File à New à Project.

Xcode application to create ios project

After selecting “Create a new Xcode project” a new window will open in that we need to choose template.

The new Xcode window will contain several built-in app templates to implement common type of iOS apps like page based apps, tab-based apps, games, table-view apps, etc. These templates are having pre-configured interface and source code files. 

For this iOS Video example, we will use most basic template “Single View Application”. To select this one, Go to the iOS section in left side à select Application à In main area of dialog select “Single View Application” and then click on next button like as shown below.

Select single view application from ios xcode templates

After click Next we will get window like as shown below, in this we need to mention project name and other details for our application.

Product Name: “Video in iOS”

The name whatever we enter in Product Name section will be used for the project and app.

Organization Name: “Tutlane”

You can enter the name of your organization or your own name or you can leave it as blank.

Organization Identifier: “com.developersociety”

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

Language: “Swift”

Select language type as “Swift” because we are going to develop applications using swift.

Devices: “Universal”

Choose Devices options as Universal it means that one application is for all apple devices in case if you have any specific requirement to run app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

Use Core Data: Unselected

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

Include Unit Tests: Unselected

In case if you need unit tests for your application then select this option otherwise unselect it.

Include UI Tests: Unselected

In case if you need UI tests for your application then select this option otherwise unselect it.

Once you finished entering all the options then click on Next button like as shown below.

create new ios video player app in swift using xcode

Once we click on Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below.

Give path to save new ios application in xcode

After click on Create button the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files which we used to design app user interface and to maintain source code.

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface 

ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.

Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below.

ios video player app storyboard file in xcode

Now select ViewController.swift file in your project that view will be like as shown below.

ios video player app viewcontroller.swift file in xcode

Add iOS AVFoundation Framework to App

Now click on Project file à Go to the Build Phases à click on + button à Search for AVFoundation à Add AVFoundation framework to your project like as shown below.

ios video player app add avfoundation framework in xcode

Now drag mp4 file and drop into your project like as shown below.

ios video player app add music files in project

Now we will write code in ViewController.swift file to play video for that first add “AVFoundation” library then write the code like as shown below

ios video player app add code to play videos in xcode

Our ViewController.swift file code will be like as shown below


//  ViewController.swift
//  Video in iOS
//  Created by Tutlane on 09/08/2016.
//  Copyright © 2016 Tutlane. All rights reserved.

import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerViewController=AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidAppear(animated: Bool) {
let fileURL = NSURL(fileURLWithPath: "/Users/tutlane/Documents/Articles/Video in iOS/Introduction.mov")
playerView = AVPlayer(URL: fileURL)
playerViewController.player = playerView
self.presentViewController(playerViewController,animated: true){
self.playerViewController.player?.play()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If you observe above code, we added video path in our application if you know the path directly add it otherwise just drag and drop the video in fileURL to get path automatically.

Now we will run and check the output of application. To run application, select the required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.

Run video player app in xcode using simulator

Output of iOS Video Player App in Swift

Following is the result of iOS video app in swift. Now click on Play Video button it will start playing video like as shown below

ios video player app example result

This is how we can create iOS video player to play videos in swift application based on our requirement using AVFoundation framework.

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...