【IOS学习】Core Audio 学习笔记

应用场景
正在播放的应用上面叠加一个声音
应用中的内容替换正在播放的歌曲

获取系统音乐
<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

本文遵循“署名-非商业用途-保持一致”创作公用协议



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值