多媒体
来源:互联网 发布:淘宝卖高仿鞋技巧 编辑:程序博客网 时间:2024/06/02 16:37
一、音频
iOS里面有四种专门实现播放音频的方式:
1、system sound services(系统声音服务)
2、OpenAL(跨平台的开放音频处理借口)
3、audio queue services(播放和录制音频的服务)
4、AVAudioPlayer(高级音频播放器)
system sound services
适用场景:播放一些很小的提示,或者警告音
局限性:
1、声音长度要小于30秒
2、格式:IMA4
3、不能控制播放的进度
4、调用方法后立即播放声音
5、没有循环播放和立体声播放
实例代码
//路径NSString* path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"wav"]; //播放指定音频时,需要的路径为url NSURL *musicPath = [NSURL fileURLWithPath:path]; //创建soundID,播放系统提示音,或者指定的音频文件,系统都是根据soundID来寻找音频文件。大小范围为1000-2000 SystemSoundID mySoundID; //创建播放器并播放 AudioServicesCreateSystemSoundID((__bridge CFURLRef)musicPath, &mySoundID); AudioServicesPlaySystemSound(mySoundID);
还有另外一种播放的方法
//路径 CFBundleRef mainBundle; //系统声音 SystemSoundID soundID; mainBundle = CFBundleGetMainBundle(); CFURLRef URLRef = CFBundleCopyResourceURL(mainBundle, CFSTR("news"), CFSTR("wav"), NULL); AudioServicesCreateSystemSoundID(URLRef, &soundID); AudioServicesPlaySystemSound(soundID);
OpenAL
- OpenAL是一套跨平台的开源的音频处理接口
- 最适合开发游戏的音频
- OpenAL包括三个实体
- listener(听者)
- source(资源)
buffer(缓存)
OpenAL资料网址:
网址一
网址二
Audio Queue Services
audio queue services主要用来实现录制音频,为了简化音频文件的处理,通常哈需要使用到Audio file services。
相对底层的api,参考
http://blog.csdn.net/midfar/article/details/7233454
我们可以吧AVAudioPlayer看做是一个高级播放器,它支持的音频格式,如下:
* AAC
* AMR
* ALAC
* iLBC
* IMA4
* linearPCM
* MP3
开发步骤:
首先,导入AVFoundation框架
其次,AVAudioPlayer在初始化的时候要给一个播放文件
再然后,就是对一些属性进行设置了。
代码如下:
//得到音频资源 NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"112" ofType:@"mp3"]; //转化文件地址 NSURL* url = [NSURL fileURLWithPath:musicPath]; _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; //设置声音 [_player setVolume:1.0]; self.valumSlider.value = self.player.volume; self.volumValue.text = [NSString stringWithFormat:@"%.0f", self.player.volume*100]; //准备播放 [_player prepareToPlay]; //播放// [_player play];
播放暂停按钮的逻辑
if ([_play.titleLabel.text isEqualToString:@"播放"]) { [_play setTitle:@"暂停" forState:UIControlStateNormal]; BOOL isplay = [_player play]; if (isplay) { [self.timer setFireDate:[NSDate distantPast]]; }else{ NSLog(@"播放有误"); } NSLog(@"点击了播放"); } else{ [_play setTitle:@"播放" forState:UIControlStateNormal]; [_player pause]; [self.timer setFireDate:[NSDate distantFuture]]; NSLog(@"点击了暂停"); }
停止播放按钮的逻辑
NSLog(@"停止"); //归零 [_player setCurrentTime:0]; [_player stop]; [self.timer setFireDate:[NSDate distantFuture]]; self.progressSlider.value = 0; if ([self.play.titleLabel.text isEqualToString:@"暂停"]) { [self.play setTitle:@"播放" forState:UIControlStateNormal]; }
二、视频
iOS里面的视频播放用到的是AVPlayer(包含在AVFoundation里面)与AVAudioPlayer有点类似,但是其功能更加强大
直接上代码:
一些控件的初始化:
@property (nonatomic, retain)AVPlayer*player;@property (nonatomic, retain)AVPlayerItem*playerItem;//播放元素@property (nonatomic, retain)AVPlayerLayer*playerLayer;//显示视屏的层@property (nonatomic, assign)BOOL isreadtoplay;//判断是否已经开始视屏_isreadtoplay = NO; //监听通知中心中的某一条消息 //关心对象,回调方法,消息名称,标记 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videofinish:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; self.navigationItem.title = @"高级播放";// [self localMusicalPlay]; NSLog(@"进入页面"); //滑竿 UISlider *progressSilder = [[UISlider alloc]initWithFrame:CGRectMake(50, 400, 150, 80)]; [self.view addSubview:progressSilder]; progressSilder.tag = 1000; progressSilder.minimumValue = 0; progressSilder.value = 0; [progressSilder addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged]; [self localVadio];
-(void)localVadio{ NSString *stringPath = [[NSBundle mainBundle] pathForResource:@"9533522808" ofType:@"mp4"]; NSURL* url = [NSURL fileURLWithPath:stringPath]; //时间监听者监听事键 _playerItem = [AVPlayerItem playerItemWithURL:url]; _player = [AVPlayer playerWithPlayerItem:_playerItem]; //初始化layer _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player]; //将视屏层添加到当前界面的layer上 [self.view.layer addSublayer:_playerLayer]; _playerLayer.frame = CGRectMake(0, 60, 400, 300); _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; //为视屏添加观察者 [_playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];}
-(void)localMusicalPlay{ //先取得本地音频文件 NSString *stringPath = [[NSBundle mainBundle] pathForResource:@"113" ofType:@"mp3"]; NSURL* url = [NSURL fileURLWithPath:stringPath]; _playerItem = [AVPlayerItem playerItemWithURL:url]; //kvo 观察某对象属性的变化 [_playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; //初始化播放器 _player = [AVPlayer playerWithPlayerItem:_playerItem]; //播放 [_player play];
#pragma kvo 的回调方法-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ AVPlayerItem* item = (AVPlayerItem*)object;// AVPlayerStatus status = change[NSKeyValueChangeNewKey]; NSLog(@"%@",change[NSKeyValueChangeNewKey]); NSLog(@"old%@",change[NSKeyValueChangeOldKey]); NSLog(@"%@",item); //得到改变之后的属性 AVPlayerStatus status = [change [NSKeyValueChangeNewKey] intValue]; switch (status) { case AVPlayerStatusUnknown: NSLog(@"未知"); break; case AVPlayerStatusFailed: NSLog(@"失败"); break; case AVPlayerStatusReadyToPlay:{ NSLog(@"准备"); [_player play]; float sum = self.playerItem.duration.value/self.playerItem.duration.timescale*1.0; UISlider* temop = [self.view viewWithTag:1000]; temop.maximumValue = sum; _isreadtoplay = YES; NSLog(@"%f------",sum); } break; default: break; } //移除观察者; [item removeObserver:self forKeyPath:@"status"];}
0 0
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体
- 多媒体定时器
- 多媒体制作
- C语言结构体内含有数组,该结构体作为参数传递时需用指针,否则对数组的修改不会被记录
- Gitflow工作流
- android 心跳包的分析
- 利用NI Vision Assistant自动生成机器视觉代码
- Centos下Yum安装PHP5.5,5.6,7.0
- 多媒体
- 统计机器学习(机器学习) 概念
- 【剑指offer】懂二进制
- linux自旋锁和futex
- 疯狂的程序员--绝影
- Hibernate---一对多映射与多对一映射
- 进阶光照与材质之物体和材质
- 关于No resource found that matches the given name 'Theme.AppCompat.Light' No resource found that ma
- imageloder可接受的url