2

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

Fygo
  • 4,555
  • 6
  • 33
  • 47
  • `MPMoviePlayerController` seems an odd way to get video content texture-mapped onto SceneKit content. Have you tried `AVPlayerLayer` or `SKVideoNode`? – rickster Apr 22 '15 at 19:57
  • Also, if you're expecting the cube to have the same content on all six faces, you're probably better off having a single material for the whole cube instead of six copies of the same material. – rickster Apr 22 '15 at 19:59
  • @rickster thanks for the suggestion. I have just tried it with AVPlayerLayer instead of MPMoviePlayerController and the result is the same. I am beginning to think that it may be the simulator acting out and unluckily I don't have an ios8 device with me, I will check it from home. (btw, yes, I know about the cube /it is not even necessary to have a for cycle there - it was just a fast test/, at the end I would like to map it on a sphere anway, I just see it better on a cube). – Fygo Apr 22 '15 at 21:30
  • The simulator is not anywhere near an accurate portrayal of device performance, especially when GPU-related stuff like video and SceneKit are involved. – rickster Apr 22 '15 at 23:23
  • @rickster so in the device it doesn't work at all... I tried it with a sphere - only black sphere, green sphere with memory warning (the simulator shows approx 80MB memory consumption) or the sphere doesn't appear at all. – Fygo Apr 23 '15 at 15:21

2 Answers2

8

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.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
0

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

mnuages
  • 13,049
  • 2
  • 23
  • 40