球体上的Swift视频,黑屏,但可以听到音频

时间:2016-02-11 14:14:31

标签: ios swift video avplayer scenekit

我正在尝试制作一个360度视频播放器,通过在球体和视频上投影视频。然后把法线翻到里面&将相机放在球体内,但是当我尝试在球体上播放视频时,我会看到黑屏,但我可以听到音频播放。

   override func viewDidLoad() {
        super.viewDidLoad()

        //create sphere
        makeSphere();

        //create user-interface
        resizeScreen();

    }

ViewDidLoad ^我呼叫resizeScreen();,让视频播放器“适应”屏幕。

 //set variables
        var player : AVPlayer? = nil;
        var playerLayer : AVPlayerLayer? = nil;
        var asset : AVAsset? = nil;
        var playerItem : AVPlayerItem? = nil;
        var timer = NSTimer();

        //start functions
        func resizeScreen() {

            //Get device screen width & height
            var screenSize: CGRect = UIScreen.mainScreen().bounds;

            //set screen width and height in variables
            var screenWidth = screenSize.width;
            var screenHeight = screenSize.height;

            //change size of video player
            playerLayer?.frame.size.height = screenHeight * 0.75;
            playerLayer?.frame.size.width = screenWidth * 1.0;

            //change size of button
            PlayVid.frame.size.height = screenHeight * 0.75;
            PlayVid.frame.size.width = screenWidth * 1;

            //set position of text label
            TemporaryURL.center.x = screenWidth * 0.5;
            TemporaryURL.center.y = screenHeight * 0.9

        }

resizeScreen();,如上所述,视频播放器和按钮会适应屏幕尺寸。

        func makeSphere() {
            let sceneView = SCNView(frame: self.view.frame);
            self.view.addSubview(sceneView);

            //Get device screen width & height
            var screenSize: CGRect = UIScreen.mainScreen().bounds;

            //set screen width and height in variables
            var screenWidth = screenSize.width;
            var screenHeight = screenSize.height;

            //set scene's height, width and x-position
            sceneView.frame.size.height = screenHeight * 0.75;
            sceneView.frame.size.width = screenWidth * 1;
            sceneView.center.x = screenWidth * 0.5;

            //create scene
            let scene = SCNScene();
            sceneView.scene = scene;

            //camera positioning
            let camera = SCNCamera();
            let cameraNode = SCNNode();
            cameraNode.camera = camera;
            cameraNode.position = SCNVector3(x: -3.0, y: 3.0, z: 3.0);

            //lighting
            let light = SCNLight();
            light.type = SCNLightTypeOmni;
            let lightNode = SCNNode();
            lightNode.light = light;
            lightNode.position = SCNVector3(x: 0, y: 0, z: 0);

            //create a sphere
            let sphereGeometry = SCNSphere(radius: 2.5);

            //create sphere node
            let sphereNode = SCNNode(geometry: sphereGeometry);

            //create constraint for sphere
            let constraint = SCNLookAtConstraint(target: sphereNode);
            //constraint.gimbalLockEnabled = true;
            cameraNode.constraints = [constraint];

            //set nodes
            //scene.rootNode.addChildNode(lightNode);
            scene.rootNode.addChildNode(cameraNode);
            scene.rootNode.addChildNode(sphereNode);


             inlineVideo();
             player!.play();

            //make variable
            let videoMaterial = SCNMaterial();
            var imgURL = NSURL(string: "http://imgurl");
            var videoURLWithPath = "http://videourl";
            var videoURL = NSURL(string: videoURLWithPath);

            //apply texture to variable
            videoMaterial.diffuse.contents = AVPlayerLayer(player: self.player);
            videoMaterial.specular.contents = UIColor.redColor();
            videoMaterial.shininess = 1.0;

            //set texture on object of name sphereGeomtetry
            sphereGeometry.materials = [videoMaterial];
        }

makeSphere();这创造了球体和为该球体初始化视频。

        func inlineVideo(){
            //play inline video

            //insert url of video into textfield
            var PasteBoard = UIPasteboard.generalPasteboard().string;
            TemporaryURL.text = PasteBoard;

            //get path of video
            //var videoURLWithPath = PasteBoard;
            var videoURLWithPath = "http://videourl";
            var videoURL = NSURL(string: videoURLWithPath);

            if (videoURL == nil){
                videoURL = NSURL(string: "http://videourl");
            }else{
                //do nothing
            }

            //get video url & set it
            asset = AVAsset(URL: videoURL!) as AVAsset;
            playerItem = AVPlayerItem(asset:asset!);

            //set target for video
            player = AVPlayer(playerItem: self.playerItem!);
            playerLayer = AVPlayerLayer(player: self.player);
        }

这是我用来初始化常规视频的视频播放器代码,我试图将PlayerLayer绑定到球体的纹理,但我得到的只是一个黑屏,即使我听到了声音,为什么不是球体上投射的视频?我认为这样的事情应该有效吗?

3 个答案:

答案 0 :(得分:1)

检查以下链接:

有3种不同的方法(例如OpenGL / SceneKit,着色器/固定功能管道,swift / obj-c,RGB / YCbCr),所以每个人都应该找到任何方便的代码。

答案 1 :(得分:0)

您需要使用SKVideoNode在球体上显示视频,请参阅SKVideoNode only on a small part of SCNSphere - 仍有小问题,会在修复后更新。

答案 2 :(得分:0)

设置播放器并开始播放,然后再将其分配为可使用的材料。

var player = AVPlayer(url: yourURL)
player.play()
node.geo.material.emissive.contents = player