6.19 Media 音频&视频
2015-06-19 18:04
337 查看
1,Audio
2,MediaPlayer
// 引入相应的框架 @import ***Foundation; @interface MusicViewController () <***AudioPlayerDelegate> @property(strong,nonatomic)***AudioPlayer *player;
- (void)initlializeDataSource { //1,创建URL NSURL *url = [[NSBundle mainBundle] URLForResource:@"Release My Soul" withExtension:@"mp3"]; //2,创建player self.player = [[***AudioPlayer alloc] initWithContentsOfURL:url error:nil]; //放入缓存池 [self.player prepareToPlay]; //设置代理 self.player.delegate = self; //设置音量(0-1) self.player.volume = 0.1; }
- (void)initlializeUserInterface { //按钮1:音效播放 //1,创建BUTTON UIButton *Button = [UIButton buttonWithType:UIButtonTypeSystem]; //2. 设置标题 [Button setTitle:@"音效播放" forState:UIControlStateNormal]; //3. 设置大小 [Button setBounds:CGRectMake(0, 0, 80, 100)]; //4. 设置中心点 [Button setCenter:CGPointMake(100, 200)]; //5. 添加到视图 [self.view addSubview:Button]; // 按钮2:音乐播放 //1. 创建button UIButton *musicButton = [UIButton buttonWithType:UIButtonTypeCustom]; //2. 设置标题 [musicButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [musicButton setTitle:@"音乐播放" forState:UIControlStateNormal]; // 3. 设置大小 [musicButton setBounds:CGRectMake(0, 0, 80, 100)]; // 4. 设置中心点 [musicButton setCenter:CGPointMake(300, 200)]; // 5. 添加到视图 [self.view addSubview:musicButton]; // 6. 添加事件 [Button addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside]; [musicButton addTarget:self action:@selector(handleMusicButtonEvent:) forControlEvents:UIControlEventTouchUpInside]; }
- (void)handleButtonEvent:(UIButton *)sender { //1,获取URL //NSString *path = [[NSBundle mainBundle] pathForResource:@"SystemSound" ofType:@"wav"]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"SystemSound" withExtension:@"wav"]; //2.创建音乐播放ID变量 SystemSoundID systemID = 0; //3. 进行函数调用创建系统音乐ID AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url),&systemID); //4.进行音效播放 AudioServicesPlaySystemSound(systemID); } #pragma mark - ***AudioPlayerDelegate //***AudioPlayerDelegate - (void)audioPlayerDidFinishPlaying:(***AudioPlayer *)player successfully:(BOOL)flag { // 音乐播放完毕的时候调用该方法 } //音乐点击按钮 - (void)handleMusicButtonEvent:(UIButton *)sender { if ([self.player isPlaying]) { [self.player pause]; } else { [self.player play]; } }
2,MediaPlayer
#import "MovieViewController.h" @import MediaPlayer; @interface MovieViewController () @property (nonatomic, strong) MPMoviePlayerController *play; @property (nonatomic, strong) MPMoviePlayerViewController *playerVC; - (void)initlializeUserInterface; @end
@implementation MovieViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self initlializeUserInterface]; } - (void)initlializeUserInterface { // 按钮1:直接播放 // 1. 创建button UIButton *Button = [UIButton buttonWithType:UIButtonTypeSystem]; // 2. 设置标题 [Button setTitle:@"直接播放" forState:UIControlStateNormal]; // 3. 设置大小 [Button setBounds:CGRectMake(0, 0, 80, 100)]; // 4. 设置中心点 [Button setCenter:CGPointMake(200, 500)]; // 5. 添加到视图 [self.view addSubview:Button]; // 按钮2:全屏播放 // 1. 创建button UIButton *musicButton = [UIButton buttonWithType:UIButtonTypeSystem]; // 2. 设置标题 [musicButton setTitle:@"全屏播放" forState:UIControlStateNormal]; // 3. 设置大小 [musicButton setBounds:CGRectMake(0, 0, 80, 100)]; // 4. 设置中心点 [musicButton setCenter:CGPointMake(100, 500)]; // 5. 添加到视图 [self.view addSubview:musicButton]; // 6. 添加事件 [Button addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside]; [musicButton addTarget:self action:@selector(handleMovieButtonEvent:) forControlEvents:UIControlEventTouchUpInside]; } //直接播放 - (void)handleButtonEvent:(UIButton *)sender { //1,创建URL NSURL *url = [[NSBundle mainBundle] URLForResource:@"宣传资料" withExtension:@"mp4"]; //2,创建player MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url]; //3,设置player视图 player.view.frame = CGRectMake(0, 20, 375, 667); //4,设置player视图缩放 player.scalingMode = MPMovieScalingModeAspectFit; //5,添加到视图 [self.view addSubview:player.view]; //6,播放plyer [player play]; //7, 对象持有一次,否则无法播放 self.play = player; } //全屏播放 - (void)handleMovieButtonEvent:(UIButton *)sender { //注册通知(当视频播放完毕和用户播放完毕的时候发送通知) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMovieEvent:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; //1,创建URL NSURL *url = [[NSBundle mainBundle] URLForResource:@"宣传资料" withExtension:@"mp4"]; //2,创建playVC self.playerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url]; //3,推送 [self presentMoviePlayerViewControllerAnimated:self.playerVC]; } - (void)handleMovieEvent:(NSNotification *)not { }
相关文章推荐
- Linux gdb调试器用法全面解析
- Leetcode: Search a 2D Matrix
- 高小平:政府改革把大数据战略引入公共治理
- studio 编译错误记录
- UDP Server/Client
- java读取properties配置文件
- 《一个程序猿的生命周期》读后感
- 将博客搬至CSDN
- Python学习笔记(4)--- Flask开发入门
- 编程学习网站
- SAP F1 无法显示该网页 完美解决方案(不用重装SAP)
- 记录一个JS
- 通过PHP自带的$_SERVER判断 手机访问网站自动跳转到手机版
- C++ 类的大小计算
- 在C#中使用C++编写的类
- Nginx + Tomcat + HTTPS 配置原来不需要在 Tomcat 上启用 SSL 支持
- g++命令行详解
- (转)织梦cms首页添加digg顶一下的修改方法
- CentOS Linux iptables 防火墙
- MySQL行级锁、表级锁、页级锁详细介绍