一、简单介绍
简单来说,音频可以分为2种
又称“短音频”,通常在程序中的播放时长为1~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;
如果要播放多个音乐文件可以封装一个播放音乐的工具类:
--------------------------------------------------------
实现代码--------------------------------------------------
导入头文件
#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, }; |