开发者

using BeginReceivingRemoteControlEvents in Monotouch

开发者 https://www.devze.com 2023-01-15 20:24 出处:网络
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 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 apprecia开发者_JAVA百科ted.


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)


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消