在 Monotouch 中使用 BeginReceivingRemoteControlEvents

发布于 2024-09-18 20:00:11 字数 149 浏览 2 评论 0原文

我已经让我的应用程序在后台播放音乐,我还通过调用 BeginReceivingRemoteControlEvents 成功使其成为媒体播放器。但是,RemoteControlReceived 方法永远不会被调用。 Objective C 中的相同逻辑运行良好。任何样品或指南表示赞赏。

I've made my app play music in the background, I also successfully made it become the media player by calling BeginReceivingRemoteControlEvents. however, the RemoteControlReceived method never gets called. the same logic in Objective C is working fine. Any samples or guidelines appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-09-25 20:00:11

您需要确保将其放置在第一响应者视图上,如果没有,则事件需要沿着响应者链向上传递,直到到达远程事件。尝试将远程控制事件想象为与键盘上的按键相同。例如,如果应用程序专注于按钮,并且您按下某些键盘按键,则不会发生任何事情,因为按钮不会监听按键操作。
您的代码中也出现了类似的情况。尝试使用 BeginReceivingRemoteControlEvents 和方法重写来创建一个基本的单视图项目以接收事件(不记得它是什么,RemoteEventReceived() 或类似的东西。)当您按下远程按钮时,应该会触发该事件。

(抱歉,我无法提供示例代码,目前不在 mac 前面)

You need to ensure it is placed on the First responder view, and if not, the event needs to be passed up along the chain of responders until it reaches your remote events. Try to imagine remote control events the same as keypresses on the keyboard. For example, If the app is focused on a button, and you press some keyboard keys, nothing will happen, as a button isn't listening for key presses.
A similar situation is occurring in your code. Try to create a basic, single viewed project with the BeginReceivingRemoteControlEvents and the method override to receive the event (cant remember what it is, RemoteEventReceived() or something similar.) This should get fired when you press a remote button.

(sorry I cant give sample code, not in front of mac at the minute)

无人问我粥可暖 2024-09-25 20:00:11

您可能想尝试使用稍后的机制来侦听远程控制事件。例如,如果您想听耳机按钮:

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];

或者,如果您更喜欢使用方法而不是块:

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

要停止:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

或:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

您需要将其添加到文件的包含区域:

@import MediaPlayer;

这适用于背景,仅当您是在后台播放的音频应用程序时(即您必须在应用程序功能中设置该背景模式)。

You might want to try using a later mechanism to listen for remote control events. For example, if you want to listen to the headset button:

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"toggle button pressed");
        return MPRemoteCommandHandlerStatusSuccess;
    }];

or, if you prefer to use a method instead of a block:

    [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];

To stop:

    [commandCenter.togglePlayPauseCommand removeTarget:self];

or:

    [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];

You'll need to add this to the includes area of your file:

@import MediaPlayer;

This works in the background, ONLY if you are an audio app playing in the background (i.e. you have to set that background mode in your app capabilities).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文