Sunday, 2 February 2014

Audio and Video

1. ADD AVFoundation and Mediaplayer  Framworks
2.Import header file <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

3.ViewController.h
@interface ViewController : UIViewController{
AVAudioPlayer *audioPlayer;
    MPMoviePlayerViewController *moviePlayer;

}
-(IBAction)playAudio:(id)sender;

-(IBAction)playVideo:(id)sender;

4.ViewController.M
-(IBAction)playAudio:(id)sender{
    NSString *path = [[NSBundle mainBundle]
                      pathForResource:@"song" ofType:@"mp3"];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:
                   [NSURL fileURLWithPath:path] error:NULL];
    [audioPlayer play];
}
-(IBAction)playVideo:(id)sender{
    NSString *path = [[NSBundle mainBundle]pathForResource:
                      @"video" ofType:@"mov"];
    moviePlayer = [[MPMoviePlayerViewController
                    alloc]initWithContentURL:[NSURL fileURLWithPath:path]];

    [self presentModalViewController:moviePlayer animated:YES];
}

No comments:

Post a Comment