在extern“C”块中定义一个能够引用外部方法的类

时间:2013-06-05 18:41:33

标签: ios methods mpmovieplayercontroller extern

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

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

所以我设法向Google提供了流式传输实际视频所需的所有内容,以及一些用于注册滑动手势的代码。问题是,在定义UISwipeGestureRecognizer时,您需要为其分配一个操作方法。但是,执行视频流的功能正在外部“C”块中定义,这是必需的,因此可以在Unity中引用它。

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

所以现在当我运行它时,视频开始流式传输但是一旦我开始刷屏幕,它就会崩溃。大概是因为无法引用分配的方法是我的猜测。

我的问题是......我如何实现这一点,也许有一些我不知道的明显事物?重要的是使它在外部“C”块中定义的函数中工作,因为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];


    }


}

1 个答案:

答案 0 :(得分:2)

您的问题是,当您将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与{{1}}不同。确保它们一致。