How to play audio and video in iPhone ? and Wanna Play Multiple videos simultaneously. ???


MPMoviePlayerController plays only one video at a time.
But, you can play multiple video simultaneously using AVPlayer
Tutorial link for playing multiple video simultaneously - Play video simultaneously


Download  MPMoviePlayerController code


. Code for .h file.


#import
#import
#import

@interface MultiMediaViewController : UIViewController {

    MPMoviePlayerViewController *player;
    AVAudioPlayer *audioPlayer;
}

- (IBAction)btnPlayVideo_Clicked:(id)sender;
- (IBAction)btnPlayAudio_Clicked:(id)sender;
- (IBAction)btnPlayBackgroundAudio_Clicked:(id)sender;
- (IBAction)btnStopAudio_Clicked:(id)sender;

@end



2. Code for .m file.


#import "MultiMediaViewController.h"


@implementation MultiMediaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (IBAction)btnPlayVideo_Clicked:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"mp4" inDirectory:nil];
    NSURL *movieURL = [NSURL fileURLWithPath:path];
    player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:player];
}

- (void)mediaPlayerPlaybackDidFinish:(id)sender
{   
    [player.moviePlayer stop];
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)btnPlayAudio_Clicked:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"mp3" inDirectory:nil];
    NSURL *audioURL = [NSURL fileURLWithPath:path];
    player= [[ MPMoviePlayerViewController alloc] initWithContentURL:audioURL];
    [self presentMoviePlayerViewControllerAnimated:player];
}

- (IBAction)btnPlayBackgroundAudio_Clicked:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"mp3" inDirectory:nil];
    NSURL *audioURL = [NSURL fileURLWithPath:path];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    audioPlayer.numberOfLoops = -1;
    [audioPlayer play];
}

- (IBAction)btnStopAudio_Clicked:(id)sender
{
    [audioPlayer stop];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end


***************************



Comments

  1. Great post with great info. Had a good look around on your site and I will be back!

    iphon application development

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Make REST api call in Objective-C.

Launch iPhone simulator without Xcode.

Hacker Rank problem solution