gpt4 book ai didi

ios - 使在 extern "C" block 中定义的类能够引用外部方法

转载 作者:行者123 更新时间:2023-11-29 10:58:52 24 4
gpt4 key购买 nike

我正在尝试为 Unity3d iOS 编写一个简单的插件,您可能听说过该流视频。我实际上设法做到了,流媒体视频位有效。

现在我正在尝试添加作为插件一部分的功能来检测滑动手势并向 Unity 发送消息。我没有使用 Objective C 的经验,目前对学习来龙去脉不感兴趣,因为我只是想为这个特定问题找到解决方案。

所以我已经设法用谷歌搜索了流式传输实际视频所需的所有内容,以及一些用于注册滑动手势的代码。问题是,在定义 UISwipeGestureRecognizer 时,您应该为其分配一个操作方法。但是执行视频流的功能是在一个外部“C” block 中定义的,这是必需的,以便它可以在 Unity 中引用。

虽然必须在 iOS 应用程序的常规框架中定义(我认为)分配给手势识别器的方法,但我怀疑这会造成手势识别器类不知道在外部定义的方法的问题外部“C” block 。

现在,当我运行它时,视频开始流式传输,但一旦我开始滑动屏幕,它就会全部崩溃。大概是因为分配的方法不能被引用是我的猜测。

我的问题是...我如何实现这一点,也许有一些我没有意识到的显而易见的事情?重要的是让它在外部“C” block 中定义的函数中工作,因为毕竟 Unity 需要它。

这是我到目前为止整理的实际代码:

http://www.hastebin.com/ragocorola.m <-- 完整代码

推测 loadLevel 方法应该如何声明?

extern "C" {


void _playVideo(const char *videoFilepath)
{

NSURL *url = [NSURL URLWithString:CreateNSString(videoFilepath)];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
initWithContentURL:url];

player.controlStyle = MPMovieControlStyleFullscreen;
player.view.transform = CGAffineTransformConcat(player.view.transform,
CGAffineTransformMakeRotation(M_PI_2));

UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];

[player.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:player.view];



UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:swipe action:@selector(loadLevel:)];

[swipe setDirection:(UISwipeGestureRecognizerDirectionUp |
UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft
|UISwipeGestureRecognizerDirectionRight)];


[player.view addGestureRecognizer:swipe];

[player play];


}


}

最佳答案

你的问题是当你将它作为目标传递时 swipe 是未定义的。谁知道它通过时堆栈上有什么?这会导致在您滑动时将方法发送到内存中的错误位置。

UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] 
initWithTarget:swipe action:@selector(loadLevel:)];

这相当于:

id undefinedTarget;
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:undefinedTarget action:@selector(loadLevel:)];

您的目标需要是定义 loadLevel: 方法的类的实例。


编辑(在找到您的链接之后):即 VideoPlugin 的一个实例。

虽然您会遇到的第二个问题是方法 loadLevel:loadLevel 不同。确保它们一致。

关于ios - 使在 extern "C" block 中定义的类能够引用外部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947469/

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