• iOS开发--QQ音乐练习,后台播放和锁屏界面


    一.设置后台播放

    • 首先允许程序后台播放
    • 代码实现
       1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       2     
       3     // 设置后台播放的代码,步骤
       4     // 1.获取音频的会话
       5     AVAudioSession *session = [AVAudioSession sharedInstance];
       6     // 2.设置后台播放类型
       7     [session setCategory:AVAudioSessionCategoryPlayback error:nil];
       8     // 3.激活会话
       9     [session setActive:YES error:nil];
      10     
      11     return YES;
      12 }

    二.锁屏界面

    • 适当的时机调用这个方法                                                                              
      #pragma mark - 设置锁屏界面的信息
      - (void)setupLockScreenInfo
      {
          // 1.获取当前正在播放的歌曲
          ChaosMusic *playingMusic = [ChaosMusicTool playingMusic];
          // 2.获取锁屏界面中心
          MPNowPlayingInfoCenter *playingCenter = [MPNowPlayingInfoCenter defaultCenter];
          // 3.设置展示的信息
          NSMutableDictionary *playingInfo = [NSMutableDictionary dictionary];
          
          playingInfo[MPMediaItemPropertyAlbumTitle] = playingMusic.name;
          playingInfo[MPMediaItemPropertyArtist] = playingMusic.singer;
          MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:playingMusic.icon]];
          playingInfo[MPMediaItemPropertyArtwork] = artwork;
          playingInfo[MPMediaItemPropertyArtist] = @(self.player.currentTime);
          
          playingCenter.nowPlayingInfo = playingInfo;
          // 4.让应用程序可以接受远程事件
          [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
      }
    • 实现了锁屏界面,没有实现监听远程事件的话,锁屏界面的下一首之类的按钮没有反应.实现下面的方法
       1 // 监听远程事件
       2 - (void)remoteControlReceivedWithEvent:(UIEvent *)event
       3 {
       4     switch (event.subtype) {
       5         case UIEventSubtypeRemoteControlPlay:
       6         case UIEventSubtypeRemoteControlPause:
       7             [self startOrPause:nil];
       8             break;
       9             
      10         case UIEventSubtypeRemoteControlNextTrack:
      11             [self nextMusic:nil];
      12             break;
      13             
      14         case UIEventSubtypeRemoteControlPreviousTrack:
      15             [self previousMusic:nil];
      16             break;
      17             
      18         default:
      19             break;
      20     }
      21 }
  • 相关阅读:
    关于委托的一篇不错的文章(C# 中的委托和事件)
    李建忠老师的《.net框架程序设计(修订版)》电子书下载地址,超级推荐
    普通无线路由变成纯AP模式
    CLR到底是什么?是怎么工作的?
    HTTP协议的三个问题
    桌面战争——揭秘中国互联网的里程碑之战
    B2C这点事儿
    不用baidu,不用google,你有bing啊
    让.net程序脱离.NET Framework在Linux下运行
    哥乃一介光棍
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5582571.html
Copyright © 2020-2023  润新知