iOS - Audio & Video

 Movie and sound is quite typical in the newest gadgets. It is reinforced in iOS with the help of AVFoundation.framework and MediaPlayer.framework respectively.

Steps Involved

Step 1. Create a simple View based application.
Step 2. Select your project file, select targets, and then we should addAVFoundation.framework and MediaPlayer.framework.
Step 3. Add two control buttons in ViewController.xib and make an action for enjoying video and sound respectively.
Step 4. 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

Step 5. 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

Note

We need to add video and audio clips for guaranteeing that we get the expected outcome.

Output

When we run the application, we'll get the following output −
When we click on play video, we will get an output as shown below −

1 comments :

Write comments
veera
AUTHOR
20 August 2020 at 22:39 delete

Thank you for sharing this awesome blog with us.

Keep updating...

swift course

Reply
avatar