IOS Audio and Video (Audio & Video)
Introduction
Audio and video are quite common in the latest devices.
Adding iosAVFoundation.framework and MediaPlayer.framework to your Xcode project allows IOS to support audio and video (Audio & Video).
Example Steps
- Create a simple View based application.
- Select the project file, choose the target, and then add AVFoundation.framework and MediaPlayer.framework.
- Add two buttons in ViewController.xib and create actions to play audio and video respectively.
Update ViewController.h as follows:
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController : UIViewController { AVAudioPlayer *audioPlayer; MPMoviePlayerViewController *moviePlayer; } -(IBAction)playAudio:(id)sender; -(IBAction)playVideo:(id)sender; @end
Update ViewController.m as follows:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(IBAction)playAudio:(id)sender{ NSString *path = [[NSBundle mainBundle] pathForResource:@"audioTest" ofType:@"mp3"]; audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL]; [audioPlayer play]; } -(IBAction)playVideo:(id)sender{ NSString *path = [[NSBundle mainBundle]pathForResource: @"videoTest" ofType:@"mov"]; moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]]; [self presentModalViewController:moviePlayer animated:NO]; } @end
Notes
You need to add audio and video files to ensure the expected output.
Output
Running the program yields the following output:
When we click play video, the following is displayed: