📜  iOS-音频和视频

📅  最后修改于: 2020-12-08 06:22:18             🧑  作者: Mango


音频和视频在最新设备中非常普遍。 iOS分别在AVFoundation.frameworkMediaPlayer.framework的帮助下支持它。

涉及的步骤

步骤1-创建一个简单的基于View的应用程序

步骤2-选择您的项目文件,选择目标,然后我们应该添加AVFoundation.frameworkMediaPlayer.framework

步骤3-在ViewController.xib中添加两个按钮,并分别创建一个播放音频和视频的操作。

步骤4-更新ViewController.h如下-

#import 
#import 
#import 

@interface ViewController : UIViewController {
   AVAudioPlayer *audioPlayer;
   MPMoviePlayerViewController *moviePlayer;
}
-(IBAction)playAudio:(id)sender;
-(IBAction)playVideo:(id)sender;
@end

步骤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

注意

我们需要添加音频和视频文件,以确保获得预期的输出。

输出

运行应用程序时,将获得以下输出-

iOS教程

当我们点击播放视频时,我们将得到如下所示的输出-

iOS教程

当我们单击播放音频时,您将听到音频。