gpt4 book ai didi

ios - 暂停/播放按钮监听器

转载 作者:行者123 更新时间:2023-11-29 02:23:03 26 4
gpt4 key购买 nike

所以我一直在做很多关于这个问题的研究,但我似乎无法让这段代码检测何时按下耳机上的播放/暂停按钮。我目前让我的代码监听按下的音量按钮,这些按钮可以完美地向我的应用程序输出一个 Action 。

我面临的问题是我的应用程序中没有合并 mp3 或多媒体,所以我几乎只需要应用程序检测物理播放/暂停按钮何时被按下。

我评论了http://www.sagorin.org/ios-playing-audio-in-background-audio/ ,由于在应用程序内未检测到多媒体,因此我没有成功。我还尝试执行急救功能,但没有成功。

任何想法或如果有人有类似的问题,将不胜感激。谢谢

最佳答案

请查看Remote Control-segment of the documentation 。需要满足三个条件才能收到耳机按钮按下的通知:

  • 成为第一响应者。呈现多媒体内容的 View 或 View Controller 必须是第一响应者。
  • 开启远程控制事件的传送。您的应用必须明确请求开始接收远程控制事件。
  • 开始播放音频。您的应用程序必须是“正在播放”应用程序。重申一下,即使您的应用是第一响应者并且您已开启事件传递,您的应用在开始播放音频之前也不会接收远程控制事件。

满足这些要求后,您就可以开始编码。来自文档:

Listing 5-1 Preparing to receive remote control events

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

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

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

When the view or view controller is no longer managing audio or video, it should turn off the delivery of remote control events. It should also resign first-responder status in the viewWillDisappear: method, as shown in Listing 5-2.

Listing 5-2 Ending the receipt of remote control events

- (void)viewWillDisappear:(BOOL)animated {

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

// Resign as first responder
[self resignFirstResponder];

[super viewWillDisappear:animated];
}

Handling Remote Control Events

To handle remote control events, the first responder must implement the remoteControlReceivedWithEvent: method declared by UIResponder. The method implementation should evaluate the subtype of each UIEvent object passed in and then, based on the subtype, send the appropriate message to the object that presents the audio or video content. Listing 5-3 sends play, pause, and stop messages to an audio object. Other remote control UIEvent subtypes are possible, see UIEvent Class Reference for details.

Listing 5-3 Handling remote control events

- (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;
}
}
}

关于ios - 暂停/播放按钮监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27891496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com