迅速。接收远程控制事件以使用MPNowPLayingInfoCenter [英] Swift. Receive remote control events to work with MPNowPLayingInfoCenter

查看:268
本文介绍了迅速。接收远程控制事件以使用MPNowPLayingInfoCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,为了在锁定屏幕上显示音乐播放器,编写以下代码是不够的。

As I understand, in order to show music player on lock screen, writing the following code is not enough.

override func viewDidAppear(animated: Bool) {    
var mpic = MPNowPlayingInfoCenter.defaultCenter()
    mpic.nowPlayingInfo = [
        MPMediaItemPropertyTitle:"This Is a Test",
        MPMediaItemPropertyArtist:"Matt Neuburg"
    ]
}

我的应用也应该能够接收遥控事件

My app also should be able to receive remote control events

那么,如何在Swift中做到这一点?

So, how to do that in Swift?

我是从 Apple文档,但它适用于Objective-C。

I found this from Apple Documentation, but it's for Objective-C.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // Turn on remote control event delivery
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Set itself as the first responder
    [self becomeFirstResponder];
}


- (void)viewWillDisappear:(BOOL)animated {

    // Turn off remote control event delivery
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    // Resign as first responder
    [self resignFirstResponder];

    [super viewWillDisappear:animated];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playOrStop: nil];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self previousTrack: nil];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self nextTrack: nil];
                break;

            default:
                break;
        }
    }
}


推荐答案

刚刚在GitHub找到解决方案
https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch14p643ducking/ch27p912ducking

Just found solution on GitHub https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch14p643ducking/ch27p912ducking

这篇关于迅速。接收远程控制事件以使用MPNowPLayingInfoCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆