我如何知道用户点击 iPhone 播放控件上的快进和快退按钮

发布于 2024-09-16 13:19:04 字数 326 浏览 4 评论 0原文

我想实现以下内容,

  1. 应用程序正在后台运行音乐或视频(使用 MPMoviePlayerController)。
  2. 用户双击主页按钮并转到显示播放控件的第一个屏幕(快退、播放或暂停、快进按钮)
  3. 用户单击快退或快进按钮。 然后应用程序播放上一个或下一个音乐或视频。

对于第三步,我应该知道单击了哪个按钮。 (我自然知道,当前播放的项目已暂停、停止......使用 MPMoviePlayerPlaybackStateDidChangeNotification 通知)。

我应该注册哪个通知?或者还有其他方法吗?

I want to implement the following things,

  1. App is running a music or video (using MPMoviePlayerController) in background.
  2. User double clicks the home button and go to the first screen showing playback controls (fast rewind, play or pause, fast forward buttons)
  3. User click fast rewind or fast forward button.
    Then app play previous or next music or video.

For the 3rd step, I should know which button is clicked.
(As I naturally know, the currently playing item is paused, stopped.. using MPMoviePlayerPlaybackStateDidChangeNotification notification).

Which notification should I register? Or are there any other approaches?

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

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

发布评论

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

评论(2

一个人的旅程 2024-09-23 13:19:04

我自己得到了答案。

那是使用 UIApplication 的 beginReceivingRemoteControlEvents。

在适当的地方(如viewWillAppear:)放置以下代码

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

并且视图控制器应该实现以下方法返回YES

- (BOOL)canBecomeFirstResponder {
    return YES; 
}

然后您可以在以下方法中接收远程控制器事件。

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

    if( event.type == UIEventTypeRemoteControl ) {
        NSLog(@"sub type: %d", event.subtype);
    }
}

event.subtype 如下,

typedef enum {
    // available in iPhone OS 3.0
    UIEventSubtypeNone                              = 0,

    // for UIEventTypeMotion, available in iPhone OS 3.0
    UIEventSubtypeMotionShake                       = 1,

    // for UIEventTypeRemoteControl, available in iPhone OS 4.0
    UIEventSubtypeRemoteControlPlay                 = 100,
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;

I got the answer by myself.

That is using UIApplication's beginReceivingRemoteControlEvents.

In an appropriate place (like viewWillAppear:) put the following code

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

And the view controller should implement the following method returning YES

- (BOOL)canBecomeFirstResponder {
    return YES; 
}

And then you can receive remote controller event in the following method.

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

    if( event.type == UIEventTypeRemoteControl ) {
        NSLog(@"sub type: %d", event.subtype);
    }
}

And event.subtype is as below,

typedef enum {
    // available in iPhone OS 3.0
    UIEventSubtypeNone                              = 0,

    // for UIEventTypeMotion, available in iPhone OS 3.0
    UIEventSubtypeMotionShake                       = 1,

    // for UIEventTypeRemoteControl, available in iPhone OS 4.0
    UIEventSubtypeRemoteControlPlay                 = 100,
    UIEventSubtypeRemoteControlPause                = 101,
    UIEventSubtypeRemoteControlStop                 = 102,
    UIEventSubtypeRemoteControlTogglePlayPause      = 103,
    UIEventSubtypeRemoteControlNextTrack            = 104,
    UIEventSubtypeRemoteControlPreviousTrack        = 105,
    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
    UIEventSubtypeRemoteControlEndSeekingForward    = 109,
} UIEventSubtype;
情栀口红 2024-09-23 13:19:04

这可能是一个很晚的答案,但正如我注意到的,关于音频播放和远程控制的问题并不多,所以我希望我的答案可以帮助其他有同样问题的人:

我正在使用 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" fastforwarding in this method.
        }

    }
}

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

注意! 此代码目前工作正常,但我看到另外两个子类型,分别为 UIEventSubtypeRemoteControlEndSeekingBackwardUIEventSubtypeRemoteControlEndSeekingBackward。我不确定它们是否必须实施,如果有人知道,请告诉我们。

This might be a very late answer, but as I notice, there aren't many Q/As about audio playing and remote controls, so I hope my answer helps the others who have the same problem:

I'm using AVAudioPlayer at the moment, but the remote controlling method which is - (void)remoteControlReceivedWithEvent:(UIEvent *)event must not be involved with the type of the player you're using.

To get the forward and rewind buttons on lock screen work, follow this:

In your view controller's viewDidLoad method add the following code:

//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];

Then add these methods:
viewDidAppear:: (if not implemented already)

- (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: (if not implemented already)

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

    //End recieving events
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

And:

//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" fastforwarding in this method.
        }

    }
}

This is working fine in my app, however if you want to be able to receive remote control events in all view controllers, then you should set it in the AppDelegate.

NOTE! This code is working fine at the moment, but I see two more subtypes called UIEventSubtypeRemoteControlEndSeekingBackward and UIEventSubtypeRemoteControlEndSeekingBackward. I'm not sure if they have to be implemented or not, if someone knows about it, let us know.

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