应用场景
正在播放的应用上面叠加一个声音
应用中的内容替换正在播放的歌曲
获取系统音乐
播放控制器 MPMusicPlayerController
application 播放器,会让iPod 停下来,程序退出或进入后台会停下来
音乐播放器通知
使用其他来源的音频
AVAudioSession(单例的) 类别和目的
AVAudioSessionCategoryAmbient
环境,锁屏和铃声静音时无声,不可做后台进程,可用于在后台进程上添加声音,实现混响
AVAudioSessionCategorySoloAmbient
独立环境,锁屏和铃声静音时无声,不可做后台进程,会停止后台层序
AVAudioSessionCategoryPlayback
回调,不理会铃声静音,可做后台进程
AVAudioSessionCategoryRecord
录音,不理会铃声静音,可做后台进程
AVAudioSessionCategoryPlayAndRecord
检测其他音频会话
处理中断
beginInterruption
endInterruptionWithFlags
创建新的AV Foundation
play
pause
stop
prepareToPlay
参考资料:IOS5核心框架
正在播放的应用上面叠加一个声音
应用中的内容替换正在播放的歌曲
获取系统音乐
<span style="font-size:14px;"><MPMediaPickerControllerDelegate>
// Create a new picker
MPMediaPickerController *picker = [[MPMediaPickerController alloc]
initWithMediaTypes:MPMediaTypeAnyAudio];
// Set the picker's delegate as self
[picker setDelegate:self];
// Present picker as child view controller
[self presentViewController:picker animated:YES completion:nil];
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker
didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{
[self dismissViewControllerAnimated:YES completion:nil];
[_player setQueueWithItemCollection:mediaItemCollection];
[_player play];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{
[self dismissViewControllerAnimated:YES completion:nil];
}</span>
播放控制器 MPMusicPlayerController
<span style="font-size:14px;">+ (MPMusicPlayerController *)applicationMusicPlayer;
+ (MPMusicPlayerController *)iPodMusicPlayer;
MPMusicPlayerController *ipodPlayer = [MPMusicPlayerController iPodMusicPlayer];</span>
iPod播放器,在应用间是通用的。设置该播放器会导致系统的播放器改变,应用程序退出任然继续播放application 播放器,会让iPod 停下来,程序退出或进入后台会停下来
音乐播放器通知
<span style="font-size:14px;">MPMusicPlayerControllerPlaybackStateDidChangeNotification
MPMusicPlayerControllerNowPlayingItemDidChangeNotification
MPMusicPlayerControllerVolumeDidChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateMainUI:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:nil];
//这个步骤是必要的,开始监听
[_player beginGeneratingPlaybackNotifications];</span>
使用其他来源的音频
- 建立AV Foundation 音频会话AVAudioSession
- 生成AF Foundation 音频播放器AVAudioPlayer
- 处理中断
AVAudioSession(单例的) 类别和目的
AVAudioSessionCategoryAmbient
环境,锁屏和铃声静音时无声,不可做后台进程,可用于在后台进程上添加声音,实现混响
AVAudioSessionCategorySoloAmbient
独立环境,锁屏和铃声静音时无声,不可做后台进程,会停止后台层序
AVAudioSessionCategoryPlayback
回调,不理会铃声静音,可做后台进程
AVAudioSessionCategoryRecord
录音,不理会铃声静音,可做后台进程
AVAudioSessionCategoryPlayAndRecord
回调和录音,不理会铃声静音,可做后台进程
检测其他音频会话
<span style="font-size:14px;">UInt32 otherAudioIsPlaying;
UInt32 propertySize = sizeof (otherAudioIsPlaying);
AudioSessionGetProperty (
kAudioSessionProperty_OtherAudioIsPlaying,
&propertySize,
&otherAudioIsPlaying
);
if (otherAudioIsPlaying) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[_ambientSoloSwitch setOn:NO];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
[_ambientSoloSwitch setOn:YES];
}</span>
处理中断
beginInterruption
endInterruptionWithFlags
创建新的AV Foundation
play
pause
stop
prepareToPlay
<span style="font-size:14px;">NSURL *soundEffectURL = [[NSBundle mainBundle] URLForResource:@"background-loop_01.aif" withExtension:nil];
// 创建音效
NSError *error = nil;
_backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundEffectURL error:&error];
if(error == nil){
[_backgroundPlayer setDelegate:self];
// 设置-1 重复
[_backgroundPlayer setNumberOfLoops:-1];
[_backgroundPlayer prepareToPlay];
NSString *catagory = [[AVAudioSession sharedInstance]category];
if ([catagory isEqualToString:AVAudioSessionCategorySoloAmbient]) {
AVAudioPlayer *bgMusic = nil;
bgMusic = [[AVAudioPlayer alloc]initWithContentsOfURL:musicURL error:&error];
[bgMusic setDelegate:self];
[bgMusic prepareToPlay];
[bgMusic play];
}
}
else{
NSLog(@"There was an error loading the background AVAudioPlayer\n%@",[error localizedDescription]);
}</span>
代理<span style="font-size:14px;">– audioPlayerDidFinishPlaying:successfully:
– audioPlayerDecodeErrorDidOccur:error:
– audioPlayerBeginInterruption:
– audioPlayerEndInterruption:withOptions:
– audioPlayerEndInterruption: Deprecated in iOS 6.0
– audioPlayerEndInterruption:withFlags: Deprecated in iOS 6.0</span>
参考资料:IOS5核心框架
Demo 下载地址:https://github.com/caigee/iosdev_sample
下的DemoAudio(此demo未做任何修改)
厚吾(http://blog.csdn.net/mangosnow)
本文遵循“署名-非商业用途-保持一致”创作公用协议