AVAudioPlayer音频播放器
- (void)viewDidLoad {
[super viewDidLoad];
_player = [self playForFile:@"guitar"];
}
- (AVAudioPlayer *)playForFile:(NSString *)file{
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:file withExtension:@"mp3"];
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
if (player) {
player.numberOfLoops = -1;//无限循环播放
player.enableRate = YES;//允许设置播放速率
[player prepareToPlay];
}else{
NSLog(@"Error creating player : %@",[error localizedDescription]);
}
return player;
}
- (void)play{
if (![_player isPlaying]) {
[_player play];
}
}
- (void)stop{
if ([_player isPlaying]) {
[_player stop];
_player.currentTime = 0.0;
}
}
此时运营程序并调用play方法,音乐会响起。但是如果此时切换手机左侧的“铃音/静音”开关,或者点击手机锁屏按钮,正在播放的声音会消失。现在我们要对AVAudioSession进行配置,可在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中配置以下代码
AVAudioSession *sessioin = [AVAudioSession sharedInstance];
if (![sessioin setCategory:AVAudioSessionCategoryPlayback error:nil]) {
NSLog(@"error....");
}
if (![sessioin setActive:YES error:nil]) {
NSLog(@"error.....");
}
此时再切换“铃音/静音”开关时音乐播放已经不受影响了,但是点击锁屏按钮时音乐还是会暂停,接下来设置info.plist文件
Required background modes中添加App plays audio or streams audio/video using AirPlay
或者在Source Code中添加
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
接下来再次切换“铃音/静音”、点击锁屏按钮时音乐播放不会再暂停了
处理中断事件
当接到来电或者其它应用占用音频播放时,音频播放会消失暂停。当正在播放的音频被暂停时,AVAudioSession会发出通知AVAudioSessionInterruptionNotification,接下来处理AVAudioSessionInterruptionNotification通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruption:) name:AVAudioSessionInterruptionNotification object:nil];
- (void) audioInterruption:(NSNotification *)notifi{
AVAudioSessionInterruptionType type = [[notifi.userInfo objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
AVAudioSessionInterruptionOptions options = [[notifi.userInfo objectForKey:AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
if (type == AVAudioSessionInterruptionTypeBegan) {
//中断音乐播放
[self stop];
}else if (type == AVAudioSessionInterruptionTypeEnded && options == AVAudioSessionInterruptionOptionShouldResume) {
//中断结束,继续播放音乐
[self play];
}
}
AVAudioSessionInterruptionType、AVAudioSessionInterruptionOptions是一个枚举类型
typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)
{
AVAudioSessionInterruptionTypeBegan = 1, /* the system has interrupted your audio session */
AVAudioSessionInterruptionTypeEnded = 0, /* the interruption has ended */
};
/* For use with AVAudioSessionInterruptionNotification */
typedef NS_OPTIONS(NSUInteger, AVAudioSessionInterruptionOptions)
{
AVAudioSessionInterruptionOptionShouldResume = 1
};
对线路改变的影响
当插拔耳机、连接断开蓝牙耳机等操作时,AVAudioSession会发出音频线路变化通知AVAudioSessionRouteChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil];
- (void) audioRouteChanged:(NSNotification *)notifi{
AVAudioSessionRouteChangeReason reason = [[notifi.userInfo objectForKey:AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
[self stop];
}
}
AVAudioSessionRouteChangeReason枚举类型
typedef NS_ENUM(NSUInteger, AVAudioSessionRouteChangeReason)
{
AVAudioSessionRouteChangeReasonUnknown = 0,
AVAudioSessionRouteChangeReasonNewDeviceAvailable = 1,
AVAudioSessionRouteChangeReasonOldDeviceUnavailable = 2,
AVAudioSessionRouteChangeReasonCategoryChange = 3,
AVAudioSessionRouteChangeReasonOverride = 4,
AVAudioSessionRouteChangeReasonWakeFromSleep = 6,
AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory = 7,
AVAudioSessionRouteChangeReasonRouteConfigurationChange = 8 // added in iOS 7
};
/* values for AVAudioSessionRouteChangeReasonKey in AVAudioSessionRouteChangeNotification userInfo dictionary
AVAudioSessionRouteChangeReasonUnknown
The reason is unknown.
AVAudioSessionRouteChangeReasonNewDeviceAvailable
A new device became available (e.g. headphones have been plugged in).
AVAudioSessionRouteChangeReasonOldDeviceUnavailable
The old device became unavailable (e.g. headphones have been unplugged).
AVAudioSessionRouteChangeReasonCategoryChange
The audio category has changed (e.g. AVAudioSessionCategoryPlayback has been changed to AVAudioSessionCategoryPlayAndRecord).
AVAudioSessionRouteChangeReasonOverride
The route has been overridden (e.g. category is AVAudioSessionCategoryPlayAndRecord and the output
has been changed from the receiver, which is the default, to the speaker).
AVAudioSessionRouteChangeReasonWakeFromSleep
The device woke from sleep.
AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory
Returned when there is no route for the current category (for instance, the category is AVAudioSessionCategoryRecord
but no input device is available).
AVAudioSessionRouteChangeReasonRouteConfigurationChange
Indicates that the set of input and/our output ports has not changed, but some aspect of their
configuration has changed. For example, a port's selected data source has changed.
*/
同时也可以根据AVAudioSessionPort判断
if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
AVAudioSessionRouteDescription *routeDescription = [notifi.userInfo objectForKey:AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *portDes = routeDescription.outputs[0];
NSString *portType = portDes.portType;
//判断播放音频的设备
if ([portType isEqualToString:AVAudioSessionPortHeadphones]) {
[self stop];
}
}
/* output port types */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortLineOut API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Line level output on a dock connector */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortHeadphones API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Headphone or headset output */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortBluetoothA2DP API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Output on a Bluetooth A2DP device */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortBuiltInReceiver API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* The speaker you hold to your ear when on a phone call */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortBuiltInSpeaker API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Built-in speaker on an iOS device */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortHDMI API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Output via High-Definition Multimedia Interface */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortAirPlay API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Output on a remote Air Play device */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortBluetoothLE API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Output on a Bluetooth Low Energy device */
/* port types that refer to either input or output */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortBluetoothHFP API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Input or output on a Bluetooth Hands-Free Profile device */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortUSBAudio API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Input or output on a Universal Serial Bus device */
AVF_EXPORT AVAudioSessionPort const AVAudioSessionPortCarAudio API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos); /* Input or output via Car Audio */