• iOS mp3播放锁屏界面展示和耳机拔出事件


    要实现后台播放的功能,在

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法添加如下代码

    //在静音模式下也能播放音频 进入后台也能播放
        // 1.获取音频会话
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        // 2.设置后台播放类别
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        // 3.激活会话
        [audioSession setActive:YES error:nil];
        // 4.注册响应后台控制
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    

    在播放界面添加代码:

    这样就能在锁屏时控制音乐播放了

    //开启锁屏处理多媒体事件
        [self configRemoteControlEvents];
    
    - (void)configRemoteControlEvents
    {
        //初始化远程控制中心
        MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    
        if (@available(iOS 9.1, *)) {
    
            //暂停
            [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
                
                self.isPlay = !self.isPlay;
    
                [self.palyerBtn setImage:[[UIImage imageNamed:@"bg.music.bofang.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                [self.player pause];
                [self.musicTimer setFireDate:[NSDate distantFuture]];
                NSLog(@"远程 暂停%@",self.player);
                
                return MPRemoteCommandHandlerStatusSuccess;
            }];
            
            //播放
            [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
                
                self.isPlay = !self.isPlay;
    
                [self.palyerBtn setImage:[[UIImage imageNamed:@"bg.music.zanting.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                [self.player play];
                [self.musicTimer setFireDate:[NSDate distantPast]];
                NSLog(@"远程 播放%@",self.player);
                
                return MPRemoteCommandHandlerStatusSuccess;
            }];
            
            
            //下一首
            [commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
                
                if (self.musicSlider.value<0.9) {
                    //1.slider的value值发生变化时,currentTime也在发生变化(在playerAction中定义了value的公式)
                    float current = self.musicSlider.value;
                    [self.musicSlider setValue:(current + 0.1) animated:YES];
                    //2.获取最终的currentTime
                    float nextCurrent = (current + 0.1) * CMTimeGetSeconds(self.player.currentItem.duration);
                    //3.拖动slider导致value的值改变时,player能够让正在进行的item追着时间走
                    [self.player seekToTime:CMTimeMakeWithSeconds(nextCurrent, 1.0)];
                }
                
                return MPRemoteCommandHandlerStatusSuccess;
            }];
            
            
            //上一首
            [commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
                
                if (self.musicSlider.value>0) {
                    //1.slider的value值发生变化时,currentTime也在发生变化(在playerAction中定义了value的公式)
                    float current = self.musicSlider.value;
                    [self.musicSlider setValue:(current - 0.1) animated:YES];
                    //2.获取最终的currentTime
                    float nextCurrent = (current - 0.1) * CMTimeGetSeconds(self.player.currentItem.duration);
                    //3.拖动slider导致value的值改变时,player能够让正在进行的item追着时间走
                    [self.player seekToTime:CMTimeMakeWithSeconds(nextCurrent, 1.0)];
                }
                
                return MPRemoteCommandHandlerStatusSuccess;
            }];
            
            
            //进度条拖动
            [commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
                
                //CGFloat seekTime = event.timestamp;
                
                CMTime totlaTime = self.player.currentItem.duration;
    
                MPChangePlaybackPositionCommandEvent * playbackPositionEvent = (MPChangePlaybackPositionCommandEvent *)event;
    
                [self.player seekToTime:CMTimeMake(totlaTime.value*playbackPositionEvent.positionTime/CMTimeGetSeconds(totlaTime), totlaTime.timescale) completionHandler:^(BOOL finished) {
                    
                    //NSLog(@"finished ===> %d",finished);
                }];
    
                return MPRemoteCommandHandlerStatusSuccess;
    
                //链接:https://www.jianshu.com/p/f7651c504cd9
    
            }];
        } else {
            // Fallback on earlier versions
        }
    
    }
    

     以上仅是部分代码截取记录,仅当参考

    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    CDH5.2安装更换hive元数据存储数据库遇到的问题
    SSH 互信
    【记录】Java NIO实现网络模块遇到的BUG
    Http2协议简介
    synchronized(this) 与 synchronized(class) 理解
    【记录】spring boot 图片上传与显示
    Cookie-Session机制
    linux利用用户组给用户赋予不同的权限
    java .equals()和==的区别
    String直接赋值和使用new的区别
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/12844161.html
Copyright © 2020-2023  润新知