多媒体音频视频与iPad基本使用
一、多媒体播放
1.配置库 --> AVFoundation.framework
头文件 #import <AVFoundation/AVFoundation.h>
2.播放音频
(1)播放本地音频
@interface ViewController () { // AVAudioPlayer *_player; UISlider *_volumeSlider; //音量控制 // UIProgressView *_playProgressView; //播放进度 } @property (nonatomic,strong) AVAudioPlayer *player; @property (nonatomic,strong) UIProgressView *playProgressView;
#pragma mark - 播放本地音乐文件 -(void)playLocationAudio{ NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lalala.mp3" ofType:nil]]; _player = [[AVAudioPlayer alloc] initWithData:data error:nil]; //初始化 __weak typeof(self) weakSelf = self; [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放" action:^(UIButton *button) { // [weakSelf.player play]; }]; //加按钮 --> 播放 [self.view addSystemButtonWithFrame:CGRectMake(100, 150, 100, 30) title:@"stop" action:^(UIButton *button) { // [weakSelf.player stop]; //注:stop , pause 暂停 weakSelf.player.currentTime = 0; //设置从头播放 weakSelf.playProgressView.progress = 0; }]; //加按钮 --> stop //进度控制(UIProgressView) _playProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 200, 280, 20)]; [self.view addSubview:_playProgressView]; [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dealTimer:) userInfo:nil repeats:YES]; //添加手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap:)]; [_playProgressView addGestureRecognizer:tap]; //音量控制(UISlider) _volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 250, 280, 20)]; [self.view addSubview:_volumeSlider]; _volumeSlider.maximumValue = 10; _volumeSlider.minimumValue = 0; [_volumeSlider addTarget:self action:@selector(dealSlider:) forControlEvents:UIControlEventValueChanged]; } #pragma mark - 音量控制 -(void)dealSlider:(UISlider *)silder{ _player.volume = _volumeSlider.value; //调节当前音量与slider对应 } #pragma mark - 进度控制 -(void)dealTimer:(NSTimer *)timer{ if(_player.duration != 0){ //进度 = 当前时间/总共时间 _playProgressView.progress = _player.currentTime/_player.duration; } } -(void)dealTap:(UITapGestureRecognizer *)tap{ CGPoint point = [tap locationInView:_playProgressView]; NSLog(@"point.x = %f",point.x); //设置点击到的值 double time = _player.duration * (point.x / _playProgressView.frame.size.width); NSLog(@"time = %f",time); _player.currentTime = time; // }
(2)播放在线音频
#pragma mark - 播放在线的music -(void)audioPalyURLMusicUseDemo{ //下载地址 http://yinyueshiting.baidu.com/data2/music/239130183/1226741191429509661128.mp3?xcode=401ee63dcc7ece3e28ee0f6f3c6ea015c3ba3c8b80064e3b NSString *urlStr = @"http://yinyueshiting.baidu.com/data2/music/239130183/1226741191429509661128.mp3?xcode=401ee63dcc7ece3e28ee0f6f3c6ea015c3ba3c8b80064e3b"; _streamer = [[AudioStreamer alloc] initWithURL:[NSURL URLWithString:urlStr]]; __weak typeof(self) weakSelf = self; [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放" action:^(UIButton *button) { // [weakSelf.streamer start]; }]; //加按钮 --> 播放 [self.view addSystemButtonWithFrame:CGRectMake(100, 150, 100, 30) title:@"stop" action:^(UIButton *button) { // [weakSelf.streamer stop]; }]; //加按钮 --> stop //.......... }
(3)播放视频
//----------------3.播放视频----------------------------------- //播放本地视频文件 //1.加库-->MediaPlayer.framework ,头文件
#pragma mark - 播放视频文件 -(void)audioMedioUseDemo{ //播放本地视频文件 __weak typeof(self) weakSelf = self; [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放本地文件" action:^(UIButton *button) { //获取本地视频文件-->播放 NSString *path = [[NSBundle mainBundle] pathForResource:@"dzs.mp4" ofType:nil]; MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]]; [weakSelf presentViewController:moviePlayer animated:YES completion:^{ // }]; }]; //加按钮 --> 播放 } #pragma mark - 播放url视频文件 -(void)audioURLMedioUseDemo{ //播放网络视频文件 __weak typeof(self) weakSelf = self; [self.view addSystemButtonWithFrame:CGRectMake(100, 100, 100, 30) title:@"播放URL文件" action:^(UIButton *button) { // NSString *urlString = @"http://k.youku.com/player/getFlvPath/sid/842953390607212440d37_00/st/mp4/fileid/0300080400552FC1BC6E56087AC0925DEDEBDD-A820-CF65-A99F-02EF5D8626DD?K=d576e3f832ea0e35261e4bfe&ctype=12&ev=1&oip=1931322792&token=8474&ep=eiaWE0yOVccA4yPYij8bYyjiIHQIXP4J9h%2BFidJmALshTerJ6j%2BjtJvFS%2FlCHv5oASIPEu6F3qSSGDRgYYFLq2gQrDiuPPqR9%2FmV5a1RseYFEhEwcMujwFSWRzf1"; MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]]; [weakSelf presentViewController:moviePlayer animated:YES completion:^{ // }]; }]; //加按钮 --> 播放 /******播放直播文件**********/ //直播网址: http://219.232.160.141:5080/hls/c64024e7cd451ac19613345704f985fa.m3u8 [self.view addSystemButtonWithFrame:CGRectMake(100, 200, 100, 30) title:@"播放直播文件" action:^(UIButton *button) { // NSString *urlString = @"http://219.232.160.141:5080/hls/c64024e7cd451ac19613345704f985fa.m3u8"; MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlString]]; [weakSelf presentViewController:moviePlayer animated:YES completion:^{ // }]; }]; //加按钮 --> 播放 }
二、iPad编程
// 1.ipad ,iphone 的区别 --大小,缺少硬件(电话,短信...),适配,多了2个控件 //ipad -->size CGSize size = [UIScreen mainScreen].bounds.size; NSLog(@"%f, %f",size.width,size.height); //ipad2 : 768*1024 //iPad Air : 768*1024 //iPad Retina : 768*1024 //适配问题: 最好弄个iPad版本 /* 2.iPad 特有控件 (2个) UISplitViewController -- 分割视图控制器 ---界面间的传值-----反向传值
UIPopoverController -- 弹出控制器
*/
#import "AppDelegate.h" #import "MasterViewController.h" #import "DetailViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. MasterViewController *master = [[MasterViewController alloc] init]; //主视图 DetailViewController *dvc = [[DetailViewController alloc] init]; UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:dvc]; UISplitViewController *svc = [[UISplitViewController alloc] init]; svc.viewControllers = @[master,nvc]; svc.delegate = dvc; self.window.rootViewController = svc; return YES; }