如何添加倒带按钮来锁定屏幕?

 血红中国心_686 发布于 2023-02-13 20:25

我已经实现了我AVPlayer在iPhone锁屏上的表示MPNowPlayingInfoCenter.但我无法找到如何在标准音乐应用程序中添加±15秒倒带按钮.所以问题是如何在锁定屏幕上添加此按钮?

1 个回答
  • 我现在正在使用AVAudioPlayer,但远程控制方法- (void)remoteControlReceivedWithEvent:(UIEvent *)event不得涉及您正在使用的播放器类型.

    按照这个:

    在视图控制器的viewDidLoad方法中添加以下代码:

    //Make sure the system follows our playback status - to support the playback when the app enters the background mode.
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    

    然后添加以下方法: viewDidAppear::(如果尚未实现)

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        //Once the view has loaded then we can register to begin recieving controls and we can become the first responder
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }
    

    viewWillDisappear: (如果没有实施)

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    
        //End recieving events
        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
        [self resignFirstResponder];
    }
    

    和:

    //Make sure we can recieve remote control events
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    
    - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
        //if it is a remote control event handle it correctly
        if (event.type == UIEventTypeRemoteControl)
        {
            if (event.subtype == UIEventSubtypeRemoteControlPlay)
            {
                [self playAudio];
            }
            else if (event.subtype == UIEventSubtypeRemoteControlPause)
            {
                [self pauseAudio];
            }
            else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
            {
                [self togglePlayPause];
            }
    
            else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward)
            {
                [self rewindTheAudio]; //You must implement 15" rewinding in this method.
            }
            else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward)
            {
                [self fastForwardTheAudio]; //You must implement 15" fast-forwarding in this method.
            }
    
        }
    }
    

    这在我的应用程序中工作正常,但是如果您希望能够在所有视图控制器中接收远程控制事件,那么您应该在其中设置它AppDelegate.

    2023-02-13 20:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有