AVPlayer play SKVideoNode video ,I just can not control the playback - sprite-kit

I am using the SpriteKit to play video,this is my code to create the SKVideoNode from AVPlayer:
func CreateVideoViewWithAVPlayer(player:AVPlayer)
{
video = SKVideoNode(AVPlayer: player)
let frameLayer=AVPlayerLayer(player: player)
//video?. = CGRectMake(0, 0, 100, 100)
video?.size = CGSize(width: 1024, height: 768)
println(player.currentItem)
video?.anchorPoint = CGPoint(x: 0, y: 0)
video?.position = CGPoint(x: 0, y: 0)
backgroundColor = SKColor.blackColor()
self.addChild(video!)
}
And I create a AVPlayer give the function:
let scene01 = GameScene(size: view.bounds.size)
scene01.CreateVideoViewWithAVPlayer(player!)
But When I call this "player.play()" ,I just show the first frame and not going any more. but if I use the "video" in the "CreateVideoViewWithAVPlayer" function ,It can play.
Another: if I use this code"
skView.presentScene(scene01)
//self.view.addSubview(skView)
(Comments the second) And user "player.play()" it can play (I can hear the sound).
But if I use this:
skView.presentScene(scene01)
self.view.addSubview(skView)
I also can see the first frame.and can not run anymore.
Is there something I do is wrong? Help Me! Thanks A lot.

For no reason. You just can't use AVPlayer to control SKVideoNode's play or pause. but you can control monitor its play state via AVPlayer.
To play or pause a SKVideoNode, you have to use
video.play() / video.pause() // (video you declared here is the instance of SKVideoNode)

Related

SpriteKit; How to loop a SKVideoNode?

I am currently trying to play a video in a loop via SpriteKit. I know that I need AVFoundation and the AVPLayer() for this, however I don't know how to initialize and use this...
Could someone help me with this?
Here is my current code block:
let mapSeriallyVideo = SKVideoNode(fileNamed: "map_serially.mp4")
mapSeriallyVideo.position = CGPoint(x: 850, y: 1100)
addChild(mapSeriallyVideo)
mapSeriallyVideo.play()

Cannot play video using SKVideoNode but sound is audible

While I am testing this app Xcode Simulator, I am unable to see video in SKVideoNode but the sound is audible.
The same issue for both .mp4 and .mov files
func showVideo()
{
let strVideoFile = "Beach.mov"
let spriteVideo : SKVideoNode = SKVideoNode(fileNamed: strVideoFile)
spriteVideo.position = CGPoint(x: 0, y: 0)
spriteVideo.setScale(0.5)
spriteVideo.zPosition = 10
spriteVideo.alpha = 1.0
self.addChild(spriteVideo)
spriteVideo.play()
print("Playing \(strVideoFile)")
}
No error messages displayed. I know that the node is added because I can see the increase in the node count.

How do I change the pitch of a song thats playing using AVPlayer?

Im using avplayer to play my music from the music library and I would like to change the pitch of the music using a UISlider. Is there a pitch property that I can change every-time I move my slider? I tried using audioTimePitchAlgorithm but that didnt work for me. Thanks!
pitchSlider = UISlider(frame:CGRectMake(175 * scaleFactor, 310 * scaleFactor2, 75, 20))
pitchSlider.tintColor = UIColor.redColor()
pitchSlider.maximumTrackTintColor = UIColor.whiteColor()
pitchSlider.transform = CGAffineTransformRotate(pitchSlider.transform, CGFloat(180/360*M_PI))
pitchSlider.value = 1.0
self.view!.addSubview(pitchSlider)
you have to use an AVAudioEngine

Endless scrolling background

I am trying to get my background to move endless from right to left.
My let:
let background = SKSpriteNode(imageNamed: "background")
let background2 = SKSpriteNode(imageNamed: "background")
my didMoveToView
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
background.zPosition = 2
background2.position = CGPointMake(CGRectGetMidX(self.frame)+(background.position.x/2),CGRectGetMidY(self.frame))
background2.zPosition = 2
background.setScale(2)
background2.setScale(2)
self.addChild(background)
self.addChild(background2)
var backgroundMovement = SKAction.moveByX(background.size.width, y: 0, duration: 1)
background.runAction(backgroundMovement, completion: {() -> Void in
self.background.position = CGPointZero
})
var backgroundMovement2 = SKAction.moveByX(background2.size.width, y: 0, duration: 1)
background2.runAction(backgroundMovement2, completion: {() -> Void in
self.background2.position = CGPointZero
})}
My update func is empty.
I have uploaded a picture of how it looks like when running on device: http://s18.postimg.org/kbn83tvmx/i_OS_Simulator_Screen_Shot_09_Aug_2015_23_33_39.png
The image is still on half of the screen, and the image does not move now either.
The reason your background only takes up half of the screen is because you are using the regular size of the image in pixels. You need to edit the xScale and yScale properties in order to make it make it bigger. What you need is:
background.setScale(2)
Next, to move the background sprites, use SKAction instead since you have more control over timing. The motion within update won't be consistant because, unless you have an algorithm to control exactly when update: is called, you will see that the movement speeds up or slows down. To move the background, use code that looks something like this after your didMoveToView method:
var backgroundMovement = SKAction.moveByX(background.size.x, y: 0, duration: someDuration)
background.runAction(background, completion: {() -> Void in
//place background image at start again
background.position = ...
})
Loop this code if you want the background to move continuously and edit it in any way you'd like to make it look better (like having both backgrounds move with SKAction). Hope this helps
I'm going to copy and paste my code from another game I've made. This code moves a background from right-left forever. I'll do best to explain this code.
func createBackground () {
var backgroundTexture = SKTexture(imageNamed: "bg")
var moveBackgroundByX = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: backgroundSpeed)
var replaceBackground = SKAction.moveByX(backgroundTexture.size().width, y: 0, duration: 0)
var moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackgroundByX, replaceBackground]))
for var i:CGFloat = 0; i < 3; i++ {
background = SKSpriteNode(texture: backgroundTexture)
background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: CGRectGetMidY(self.frame))
background.size.height = self.frame.height
background.runAction(moveBackgroundForever)
movingObjects.addChild(background)
}
}
What you need to do is:
Move background (moveBackgroundByX). This moves the background in the X direction. You need to set a duration.
Replace background1 (replaceBackground).
Create an ACTION forever that moves background1, then replaces it automatically with NO delay.
Run a LOOP that decides how many backgrounds you will need. Maybe you might need more than 3.
Inside that loop, adjust the background.position.
Call runAction on your SKSpriteNode instance.
Add the new SKSpriteNode into your another SKView.
If you have any questions please let me know.

MPMoviePlayerController ignores MPMovieControlStyle.None in Swift

I'm trying to autoplay a video in my app. The video needs to play without the controls.
I've set up the video and the settings, including MPMovieControlStyle.None but the video controls appear for about 2 seconds before disappearing. I have no idea why.
I've used this code (exact code) for another project and it works well, but here for some reason it will not.
override func viewDidLoad() {
super.viewDidLoad()
generateVideo()
}
func generateVideo () {
let movieURL = NSBundle.mainBundle().pathForResource("video", ofType: "mp4")
let videoFilePath = NSURL(fileURLWithPath: movieURL!)
self.view.addSubview(MoviePlayerViewController.moviePlayer.view)
self.view.sendSubviewToBack(MoviePlayerViewController.moviePlayer.view)
MoviePlayerViewController.moviePlayer.contentURL = videoFilePath
MoviePlayerViewController.moviePlayer.shouldAutoplay = true
MoviePlayerViewController.moviePlayer.prepareToPlay()
MoviePlayerViewController.moviePlayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
MoviePlayerViewController.moviePlayer.fullscreen = true
MoviePlayerViewController.moviePlayer.controlStyle = MPMovieControlStyle.None
MoviePlayerViewController.moviePlayer.play()
MoviePlayerViewController.moviePlayer.repeatMode = MPMovieRepeatMode.One
MoviePlayerViewController.moviePlayer.scalingMode = MPMovieScalingMode.AspectFit
}
Any idea why this is happening?
After checking how the view is loaded I'm sure the problem is there.
I used a different layout with one Storyboard calling another storyboard and that was the source of the problem.