Using MPMoviePlayerController as texture in SceneK

2019-01-28 15:29发布

I created a cube in scenekit and tried to use an instance of an MPMoviePlayerController as its material. It kind-ish works but not exactly well: the video seems to be very jumpy, like it would be jumping between the video frames (basically replaying frames from the beginning till the last point played). The sound is ok.

I made a short screencapture of what's happening, I guess it is obvious from the video: Youtube vid

This is the code that handles the mapping to the cube and the creation of the player:

var moviePlayer: MPMoviePlayerController?

func startPlayingVideo(){
    let mainBundle = NSBundle.mainBundle()
    let url = mainBundle.URLForResource("Sample", withExtension: "m4v")
    moviePlayer = MPMoviePlayerController(contentURL: url)

    if let player = moviePlayer{
        /* Listen for the notification that the movie player sends us whenever it finishes playing */
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoHasFinishedPlaying:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
        println("Successfully instantiated the movie player")
        player.scalingMode = .AspectFit

        var materials = [SCNMaterial]()
        for i in 1...6 {
            let material = SCNMaterial()
            material.diffuse.contents = player.view.layer
            player.view.frame = CGRectMake(0, 0, 200, 200)
            materials.append(material)
        }
        boxGeometry.materials = materials

        player.controlStyle = MPMovieControlStyle.None
        player.play()
    }
    else {
        println("Failed to instantiate the movie player")
    }
}

Any ideas how to fix this frame-jumping and why it occurs? Thanks a lot

2条回答
聊天终结者
2楼-- · 2019-01-28 16:22

In recent versions of SceneKit you can directly set an AVPlayer as the contents of a SCNMaterialProperty instance.

查看更多
小情绪 Triste *
3楼-- · 2019-01-28 16:24

I opened a radar about AVPlayerLayer not working as a SceneKit texture (on device... works on simulator!). Apple was kind enough to reply saying this was working as intended, and supplied an alternative:

AVPlayerLayer are rendered out of process on the device and can’t be used as a texture. Please use a SKVideoNode for this instead.

查看更多
登录 后发表回答