1

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

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

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

分配给手势识别器的方法虽然必须在 iOS 应用程序的常规框架中定义(我认为),但我怀疑这会产生手势识别器类不知道在 extern "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];


    }


}
4

1 回答 1

2

Your problem is that swipe is undefined when you are passing it as a target. Who know's what's on the stack when it's passed? This results in a method being sent to a bad location in memory when you swipe.

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

this is equivalent to:

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

Your target needs to be an instance of a class that defines the loadLevel: method.


Edit (after chasing your link): i.e. an instance of VideoPlugin.

Though the second problem you will have is that method loadLevel: is different from loadLevel. Make sure they are consistent.

于 2013-06-05T20:50:12.757 回答