ios-音乐播放器-(3)后台播放

我们在做软件开发的时候 会产生很多需求

就音乐播放器而言 我们需要后台播放 这是最基本的需求

当我们正放着音乐 有事儿要做则要将音乐放到后台播放

当我们再打开软件的时候音乐还可以继续播放 并不影响我们的使用

我们播放音乐的时候 可以同时启动了两个音乐播放器

但是如果两个音乐播放器同时播放音乐的话 那么会产生一些混合音

会造成很杂乱的声音 这个我们也可以设置

可以设置音乐播放的分类

下面是苹果给开发者提供的一些 方法

我们可以根据自己的需求 选择使用

#pragma mark -- Values for the category property --

/*  Use this category for background sounds such as rain, car engine noise, etc.  
 Mixes with other music. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAmbient;
    
/*  Use this category for background sounds.  Other music will stop playing. */
AVF_EXPORT NSString *const AVAudioSessionCategorySoloAmbient;

/* Use this category for music tracks.*/
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayback;

/*  Use this category when recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;

/*  Use this category when recording and playing back audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;

/*  Use this category when using a hardware codec or signal processor while
 not playing or recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAudioProcessing;


下面讲一下 后台播放音乐的基本方法

首先 我们要在 代理里面告诉它 我们要开启后台任务

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    //开启后台任务,让程序保持运行状态
    [application beginBackgroundTaskWithExpirationHandler:nil];
    
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
然后我们要在info.plist 中添加音乐播放类型

最后我们要设置会话类型

+(void)initialize
{
    _soundIDDict = [NSMutableDictionary dictionary];
    _audioPlayDict = [NSMutableDictionary dictionary];
    
    //设置音频会话类型
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategorySoloAmbient  error:nil];
    [session setActive:YES error:nil];
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值