iOS - ऑडियो और वीडियो

नवीनतम उपकरणों में ऑडियो और वीडियो काफी सामान्य है। की मदद से इसे iOS में सपोर्ट किया गया हैAVFoundation.framework तथा MediaPlayer.framework क्रमशः।

कदम शामिल किए गए

Step 1 - एक सरल बनाएँ View based application

Step 2 - अपनी परियोजना फ़ाइल का चयन करें, लक्ष्य का चयन करें, और फिर हमें जोड़ना चाहिए AVFoundation.framework तथा MediaPlayer.framework

Step 3 - ViewController.xib में दो बटन जोड़ें और क्रमशः ऑडियो और वीडियो चलाने के लिए एक क्रिया बनाएं।

Step 4 - अपडेट ViewController.h निम्नानुसार है -

#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 - अपडेट ViewController.m निम्नानुसार है -

#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

ध्यान दें

हमें यह सुनिश्चित करने के लिए ऑडियो और वीडियो फ़ाइलों को जोड़ना होगा कि हमें अपेक्षित आउटपुट मिले।

उत्पादन

जब हम एप्लिकेशन चलाते हैं, तो हमें निम्न आउटपुट प्राप्त होंगे -

जब हम play वीडियो पर क्लिक करेंगे, तो हमें नीचे दिखाया गया आउटपुट मिलेगा -

जब हम प्ले ऑडियो पर क्लिक करेंगे, तो आप ऑडियो सुनेंगे।