07-音效的播放 远程控制事件
2015-07-08 18:55
603 查看
一、简单介绍
简单来说,音频可以分为2种
(1)音效
又称“短音频”,通常在程序中的播放时长为1~2秒
在应用程序中起到点缀效果,提升整体用户体验
(2)音乐
比如游戏中的“背景音乐”,一般播放时间较长
框架:播放音频需要用到AVFoundation.framework框架
二、音效的播放
三、AVAudioPlayer音乐的播放
四、AVPlayer流媒体播放音乐
五、远程控制事件
简单来说,音频可以分为2种
(1)音效
又称“短音频”,通常在程序中的播放时长为1~2秒
在应用程序中起到点缀效果,提升整体用户体验
(2)音乐
比如游戏中的“背景音乐”,一般播放时间较长
框架:播放音频需要用到AVFoundation.framework框架
二、音效的播放
1.获得音效文件的路径 NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil]; 2.加载音效文件,得到对应的音效ID SystemSoundID soundID = 0; AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); 3.播放音效 AudioServicesPlaySystemSound(soundID); 注意:音效文件只需要加载1次 4.音效播放常见函数总结 加载音效文件 AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID) 释放音效资源 AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID) 播放音效 AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 播放音效带点震动 AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) |
一、简单说明 (1)音乐播放用到一个叫做AVAudioPlayer的类 (2)AVAudioPlayer常用方法 加载音乐文件 - (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError; - (id)initWithData:(NSData *)data error:(NSError **)outError; 准备播放(缓冲,提高播放的流畅性) - (BOOL)prepareToPlay; 播放(异步播放)- (BOOL)play; 暂停 - (void)pause; 停止 - (void)stop; 是否正在播放 @property(readonly, getter=isPlaying) BOOL playing; 时长 @property(readonly) NSTimeInterval duration; 当前的播放位 @property NSTimeInterval currentTime; 播放次数(-1代表无限循环播放,其他代表播放numberOfLoops+1次 @property NSInteger numberOfLoops; 音量 @property float volume; 是否允许更改速率@property BOOL enableRate; 播放速率(1是正常速率,0.5是一般速率,2是双倍速率) @property float rate; 有多少个声道 @property(readonly) NSUInteger numberOfChannels; 如果要播放多个音乐文件可以封装一个播放音乐的工具类: 具体博客http://blog.csdn.net/gaojq_ios/article/details/46404831 --------------------------------------------------------实现代码-------------------------------------------------- 导入头文件 #import <AVFoundation/AVFoundation.h> /**播放本地音乐*/ /**创建全局对象 创建播放器(注意:一个AVAudioPlayer只能播放一个url)*/ NSURL *url = [[NSBundle mainBundle] URLForResource:@"遇见.mp3" withExtension:nil]; _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; /**缓冲*/ [_player prepareToPlay]; /**播放*/ [_player play]; /**暂停*/ [_player pause]; /**停止/ //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题 [_player stop]; _player=Nil; --------------------------------------------------------设置代理-------------------------------------------------- /**播放/暂停*/ BOOL isPlayer = _player.playing; if (isPlayer) { [_player pause]; } else { /**暂停*/ [_player play]; } |
/**流媒体*/ NSURL *url = [[NSBundle mainBundle] URLForResource:@"遇见.mp3" withExtension:nil]; _avPlayer = [[AVPlayer alloc] initWithURL:url]; /**播放/暂停*/ - (IBAction)AVPlayer:(UIButton *)sender{ _isPlayer = !_isPlayer; if (_isPlayer) { [_avPlayer play]; } else { [_avPlayer pause]; } } |
-------------------------------------------------------启用远程控制事件---------------------------------------------- //1.启用远程控制事件 - (void)startRemoteCtrl { //启用远程控制事件3个条件 //1.启用远程事件接收 (可以写到视图将要显示时的方法里) [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; //停止接收远程事件 //[[UIApplication sharedApplication] endReceivingRemoteControlEvents]; //2.成为第一响应者 (可以写到视图将要显示时的方法里) [self becomeFirstResponder]; //不是第一响应者 //[self resignFirstResponder]; //3.应用程序必须是当前音频的控制者,也就是在iOS 7中通知栏中当前音频播放程序必须是我们自己开发程序 //创建播放对象 NSURL *url = [[NSBundle mainBundle] URLForResource:@"遇见.mp3" withExtension:nil]; _player = [[AVPlayer alloc] initWithURL:url]; [_player play]; //将音频会话设置为支持后台 //在info.plist中添加UIBackgroundModes并且添加一个元素值为audio。 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; } //2.在视图控制器中添加远程控制事件并音频播放进行控制 - (void)remoteControlReceivedWithEvent:(UIEvent *)event { if(event.type == UIEventTypeRemoteControl) { switch (event.subtype) { case UIEventSubtypeRemoteControlPlay: [_player play]; break; case UIEventSubtypeRemoteControlTogglePlayPause: if (_isPlay) { [_player pause]; }else{ [_player play]; } _isPlay=!_isPlay; break; case UIEventSubtypeRemoteControlNextTrack: NSLog(@"下一首"); break; case UIEventSubtypeRemoteControlPreviousTrack: NSLog(@"上一首"); break; default: break; } } } //3.设置锁屏情况下显示的数据 - (void)configInfo { //创建专辑图 MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"pig"]]; NSDictionary *dic = @{ MPMediaItemPropertyAlbumTitle:@"第一夫人", MPMediaItemPropertyArtist:@"张杰", MPMediaItemPropertyArtwork:artWork }; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dic]; } ------------------------------------------------------UIEventSubtype-------------------------------------------------- typedef NS_ENUM(NSInteger, UIEventSubtype) { // 不包含任何子事件类型 UIEventSubtypeNone = 0, // 摇晃事件(从iOS3.0开始支持此事件) UIEventSubtypeMotionShake = 1, //远程控制子事件类型(从iOS4.0开始支持远程控制事件) //播放事件【操作:停止状态下,按耳机线控中间按钮一下】 UIEventSubtypeRemoteControlPlay = 100, //暂停事件 UIEventSubtypeRemoteControlPause = 101, //停止事件 UIEventSubtypeRemoteControlStop = 102, //播放或暂停切换【操作:播放或暂停状态下,按耳机线控中间按钮一下】 UIEventSubtypeRemoteControlTogglePlayPause = 103, //下一曲【操作:按耳机线控中间按钮两下】 UIEventSubtypeRemoteControlNextTrack = 104, //上一曲【操作:按耳机线控中间按钮三下】 UIEventSubtypeRemoteControlPreviousTrack = 105, //快退开始【操作:按耳机线控中间按钮三下不要松开】 UIEventSubtypeRemoteControlBeginSeekingBackward = 106, //快退停止【操作:按耳机线控中间按钮三下到了快退的位置松开】 UIEventSubtypeRemoteControlEndSeekingBackward = 107, //快进开始【操作:按耳机线控中间按钮两下不要松开】 UIEventSubtypeRemoteControlBeginSeekingForward = 108, //快进停止【操作:按耳机线控中间按钮两下到了快进的位置松开】 UIEventSubtypeRemoteControlEndSeekingForward = 109, }; |
相关文章推荐
- java音频播放示例分享(java如何播放音频)
- iOS开发之路--微博“更多”页面
- python通过wxPython打开一个音频文件并播放的方法
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- [总结]视音频编解码技术零基础学习方法
- 跨平台音频项目
- ios开发之环境搭建-01
- processing Audio
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- Power Converter┊音频互转工具
- 映泰主板前置音频设置
- 学习Objective-C:入门手册
- 语音编解码标准