Youtube 播放器似乎发送虚假的用户输入事件以防止屏幕保护程序启动(我在 osx/safari 上,但在 Windows 中也会发生同样的事情)。嵌入式播放器和 yt 网站上的播放器都会发生这种情况。我猜这是设计使然,这样屏幕保护程序就不会在您尝试观看视频时惹恼您。
但是,我正在尝试从屏幕保护程序中播放 youtube 视频,因此我想捕获并忽略或关闭这些事件。 当我的屏幕保护程序优雅退出时,它可以正常工作 6-8 秒,就像用户移动鼠标一样。我认为在 Windows 下我可以捕获它并忽略它,但我正在为 OSX 编写它,所以我似乎无法将它捕获我的代码,因为它永远不会到达我的代码 - 它由 ScreenSaverEngine 应用程序处理。Mac 屏幕保护程序是此应用程序的插件,因此它们不能覆盖主事件循环(调用 NSApplication::sendEvent)。我第一次听说这个事件是在 applicationShouldTerminate() 委托中。我可以要求应用程序稍后终止,但这只会将关闭延迟大约 1 秒。堆栈跟踪如下 - 从我可以看出它是正在发送的 MouseExit 苹果事件。
我看了又看,我找不到在可可或javascript中禁用或捕获这些播放器事件的方法。我的实际视频被加载到 WebView 并使用 youtube javascript api 运行,但下面的精简测试代码会产生相同的结果。通过执行以下操作,我也可以在不运行任何代码的情况下获得相同的结果:
(1)在 youtube 中播放视频(无论是否嵌入)(2)通过热角或其他方式手动启动屏幕保护程序(3)等待几秒钟
长期以来,我一直认为这是我的代码中的一个错误,因为我是 Objective-c 和 javascript 的新手,但现在我认为这是设计使然,因为上述步骤完全不使用我的代码即可重现问题。
屏保测试代码-----.h文件
#import <WebKit/WebKit.h>
#import <ScreenSaver/ScreenSaver.h>
@interface YtTestView : ScreenSaverView
{
WebView * webView;
}
@end
屏保测试代码 ----- .m 文件 - 我认为大多数参数对于这个例子来说都是多余的,但这是改编自 Alastair Tse 的一个很好的例子(https://github.com/liquidx/webviewscreensaver)
#import "YtTestView.h"
@implementation YtTestView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
[self setAnimationTimeInterval:1/30.0];
}
return self;
}
- (void)startAnimation
{
[super startAnimation];
webView = [[WebView alloc] initWithFrame:[self bounds]];
[webView setFrameLoadDelegate:self];
[webView setShouldUpdateWhileOffscreen:YES];
[webView setPolicyDelegate:self];
[webView setUIDelegate:self];
[webView setEditingDelegate:self];
[webView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[webView setAutoresizesSubviews:YES];
[webView setDrawsBackground:NO];
[webView setMaintainsBackForwardList:NO];
[self addSubview:webView];
NSString * url= @"http://www.youtube.com/embed/NleT--L6fNo?autoplay=1";
[webView setMainFrameURL:url];
}
- (void)stopAnimation
{
[super stopAnimation];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
}
- (void)animateOneFrame
{
return;
}
- (BOOL)hasConfigureSheet
{
return NO;
}
- (NSWindow*)configureSheet
{
return nil;
}
@end
-------- applicationShouldTerminate() 中的堆栈跟踪 - 仅供参考
2013-05-20 23:41:20.558 ScreenSaverEngine[2143:c07] 事件:NSEvent: type=Kitdefined loc=(0,800) time=21467.6 flags=0 win=0x0 winNum=0 ctxt=0x0 subtype=1 data1=619 data2 =0 2013-05-20 23:41:20.558 ScreenSaverEngine[2143:c07] 窗口类名:(null) 2013-05-20 23:41:20.559 ScreenSaverEngine[2143:c07] 事件:type=13 2013-05-20 23:41:20.561 ScreenSaverEngine[2143:c07] 0:0 Ytss 0x0000000106167327 -[YtssView(NSApplicationDelegate) applicationShouldTerminate:] + 343 2013-05-20 23:41:20.562 ScreenSaverEngine[2143:3007] 1:1: 1 AppKit NSApplication _docController:shouldTerminate:] + 75 2013-05-20 23:41:20.562 ScreenSaverEngine[2143:c07] 2:2 AppKit 0x00007fff85623d7e __91-[NSDocumentController(NSInternal) _closeAllDocumentsWithDelegate:shouldTerminateSelector:]_block_invoke_0 + 159 2013-05-20 23:41:20.562 ScreenSaverEngine[2143:c07] 3:3 AppKit 0x00007fff85623c1a -[NSDocumentController(NSInternal) _closeAllDocumentsWithDelegate:shouldTerminateSelector:] + 15032 3:41:-2 ScreensAverEngine [2143:C07] 4:4 AppKit 0x00007FFF856235DE- [NSDocumentController(nsinternal)__closealLdocumentsWithDelegate:shoreTermInateSelector:shosserinateSelector:] + 265 2013-05-20 2013-05-20 23:41:41:41:20.563 eppsensymit 7 f.21 fodyn.f.21 fof 3.21 fof 3.21 fof 3.21 fof。 + 772 2013-05-20 23:41:20.563 ScreenSaverEngine[2143:c07] 6:6 AppKit 0x00007fff8565127f -[NSApplication(NSAppleEventHandling) _handleAEQuit] + 403 2013-05-20 23:41:20.5606 ScreenSaverEngine[2143]: :7 AppKit 0x00007fff854001b1-[NSApplication(NSAppleEventHandling)_handleCoreEvent:withReplyEvent:] + 660 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 8: 8 Foundation 0x00007fff8ea2370b-[NSAppleEventManager dispatchRawCon:] +R308Reply20handler1RefAppleEvent:with -05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 9: 9 Foundation 0x00007fff8ea2356d _NSAppleEventManagerGenericHandler + 106 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 10: 10 AE 0x00007fff906fa078 _Z20aeDispatchAppleEventPK6AEDescPS_jPh + 307 2013-05 -20 23:41:20.567 ScreenSaverEngine[2143:c07] 11:11 AE 0x00007fff906f9ed911 AE 0x00007fff906f9ed911 AE 0x00007fff906f9ed9] + 660 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 8:8 Foundation 0x00007fff8ea2370b -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 308 2013-05-20 23:41:20.561 ScreenSaverEngine[2 c07] 9: 9 Foundation 0x00007fff8ea2356d _NSAppleEventManagerGenericHandler + 106 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 10: 10 AE 0x00007fff906fa078 _Z20aeDispatchAppleEventPK6AEDescPS_jPh + 307 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 11:11 AE 0x00007fff906f9ed9] + 660 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 8:8 Foundation 0x00007fff8ea2370b -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 308 2013-05-20 23:41:20.561 ScreenSaverEngine[2 c07] 9: 9 Foundation 0x00007fff8ea2356d _NSAppleEventManagerGenericHandler + 106 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 10: 10 AE 0x00007fff906fa078 _Z20aeDispatchAppleEventPK6AEDescPS_jPh + 307 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 11:11 AE 0x00007fff906f9ed9567 ScreenSaverEngine[2143:c07] 9: 9 Foundation 0x00007fff8ea2356d _NSAppleEventManagerGenericHandler + 106 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 10: 10 AE 0x00007fff906fa078 _Z20aeDispatchAppleEventPK6AEDescPS_jPh + 307 2013-05-20 23:41:20.567 ScreenSaverEngine [2143:c07] 11:11 AE 0x00007fff906f9ed9567 ScreenSaverEngine[2143:c07] 9: 9 Foundation 0x00007fff8ea2356d _NSAppleEventManagerGenericHandler + 106 2013-05-20 23:41:20.567 ScreenSaverEngine[2143:c07] 10: 10 AE 0x00007fff906fa078 _Z20aeDispatchAppleEventPK6AEDescPS_jPh + 307 2013-05-20 23:41:20.567 ScreenSaverEngine [2143:c07] 11:11 AE 0x00007fff906f9ed9ZL25dispatchEventAndSendReplyPK6AEDescPS17 AppKit 0x00007fff853f31d3 -[NSApplication 运行] + 517 2013-05-20 23:41:20.571 ScreenSaverEngine[2143:c07] 18:18 ScreenSaverEngine 0x0000000104b6e5a3 主 + 551 2013-03:571 23:41:4 ScreenSaverEngine] 19:19 libdyld.dylib 0x00007fff907b47e1 开始 + 0 2013-05-20 23:41:20.571 ScreenSaverEngine[2143:c07] 20:20 ??? 0x0000000000000001 0x0 + 1 20 ???0x0000000000000001 0x0 + 1 20 ???0x0000000000000001 0x0 + 1