OC - 音频&视频

OC - 音频&视频 首页 / iOS入门教程 / OC - 音频&视频

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

涉及步骤

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

步骤2 - 选择项目文件,选择目标,然后添加 AVFoundation.framework 和 MediaPlayer.framework 。

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

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

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

当无涯教程点击播放视频时,无涯教程将得到如下所示的输出-

iOS Tutorial

当无涯教程单击播放音频时,您将听到音频。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

软件测试52讲 -〔茹炳晟〕

从0开发一款iOS App -〔朱德权〕

OpenResty从入门到实战 -〔温铭〕

Kafka核心技术与实战 -〔胡夕〕

编译原理之美 -〔宫文学〕

安全攻防技能30讲 -〔何为舟〕

Service Mesh实战 -〔马若飞〕

如何看懂一幅画 -〔罗桂霞〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

好记忆不如烂笔头。留下您的足迹吧 :)