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
In recent versions of SceneKit you can directly set an
AVPlayer
as the contents of aSCNMaterialProperty
instance.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: