Play Audio Video With AVFoundation in iOS Swift 3.0

Play Audio Video With AVFoundation in iOS Swift 3.0

OverView:-  AVFoundation

AVFoundation is a framework in iOS that lets you performs a video play, create, edit, or reencode media files.  AVFoundation framework that you can create play Audio and Video visual media play in swift iOS.

AVFoundatiin allow you to work on detail level with time based audiovisual data with this framework in iOS swift , you can create, edit ,analyze and re-encoded data media files in swift application , it llok like pretty basic feature set. However, with this AVFoundation framework you can do some usual things, like capture a stream   and   in real time, manipulate and play back the same capture media.

Getting Start:-

Let’s now start an your Application design;

  1. Open your xcode
  2. Create a new xcode project
  3. Chose a single View Application then
  4. Click on next button then,
  5. Give the product name as you like then
  6. Click again next button then,
  7. Save in your directory and click on the create button
  8. Now ready your empty project then,
  9. Select you device that you want run your project

AVFoundation

Now click on the this you seen iphone 7 plus then select your simulator device you want run your project application seen like this

AVFoundation

Now goto the project navigator and select the main.Storyboard from the project navigator

  • Now select your View Controller and goto the editor menu select the navigation controller you will see that add a navigation controller in your main.stroyboard
  • Then now select the navigation controller you will seen that your navigation controller automatically make a initial view controller
  • Let’s come on the design your view controller select the object library and search a UIButton then drag  a four uibutton  like this

AVFoundation

Now drag four UIButon in the view controller you can seen

AVFoundation

Now come on the change the text name of our button in the view controller we have two option change the name of the view the button

  1. Select the button and double your button text will come on edit mode.
  2. If you have second one option select the button and goto the attribute inspector change the text name of your button

AVFoundation

Change the button name with as you need like this

AVFoundation

You see on the scren shot text name button change with AudioPlayer

  • Now ready your design for the click button paly the audio video and repeat the song and stop the song

AVFoundation

  • Now goto the project navigator select the view controller

AVFoundation

 

  • Now make object for audioPlayer with AVAudioPlayer() like this:

AVFoundation

// Now created an object for Boolean value

AVFoundation

 

  • Now goto the object library and drag an AVKItPlayer View Controller

AVFoundation

Now goto the project navigator and select the main project directory click on right open a list select the new file like this

AVFoundation

Now create a new swift class that class will be cocoa touch class like this and click on the next button and give the class name VideoPlayerViewController and subclass should be AVPlayerViewController like this

AVFoundation

After click next button open new window like seen this

AVFoundation

Now create a new swift class with name will be VideoPlayerViewController import with these two libraries

AVFoundation

now do a thing that goto the click on main.storyboard select the AVKit player view controller and select the identity inspector give the class name VideoPlayerViewController and copy this class name paste the storyboard id box and Check the use storyboarb id

This will work for the identifier

Now once again goto the view controller and create a uiimageaPickerControllerDelegate method

AVFoundation

Now one important thing add a string library into a plist  because if not add this stirng  library application give the run time error

Select the info plist in the project navigator click on the add button  like this

AVFoundation

and tap on the pulse button select the srting library this

AVFoundation

And now last and final step connect all @iboutlet, @ibAction with view controller

  • Now run your project it will run successfully

AVFoundation

Now click on the ok button your simulator video will be show in the list and select those you chose and play the video like this after click on the ok button

AVFoundation

Select video click that your video play successfully

AVFoundation

Also Read- Custom Tab Bar Controller in swift 3.0

If you are, looking to Develop and design for your project then feel free to contact us at any time

Call Us- +91 9910781148, +1 8033353593
Email: company@tecorb.com
Skype- tecorb

“We maintain transparency with our customers. Our values and business ethics has given us repeated customers. We tend are proud to be a reliable outsourcing partner for many clients across the World.”

                                                                 

 Published By- Dinesh Saini (IOS Developer)