I have a function in swift UIview that makes 2 mp4`s loop. The first mp4 is playing fine but the second one is not, it only seems to play once, how could i fix this?
let videoURL: NSURL = NSBundle.mainBundle().URLForResource(instrumentaimp4[skaicius], withExtension: "mp4")!
let sakeleURL: NSURL = NSBundle.mainBundle().URLForResource("sakele_blikas", withExtension: "mp4")!
player = AVPlayer(URL: videoURL)
player?.actionAtItemEnd = .None
player?.muted = true
sakele = AVPlayer(URL: sakeleURL)
sakele?.actionAtItemEnd = .None
sakele?.muted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = 1
let playerLayer2 = AVPlayerLayer(player: sakele)
playerLayer2.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer2.zPosition = 1
view.layer.addSublayer(playerLayer2)
view.layer.addSublayer(playerLayer)
player?.play()
sakele?.play()
//loop video
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "loopVideo:",
name: AVPlayerItemDidPlayToEndTimeNotification,
object:nil)
func loopVideo(notification: NSNotification) {
if let finishedPlayer = notification.object as! AVPlayerItem!{
if finishedPlayer == self.sakele {
self.sakele?.seekToTime(kCMTimeZero)
self.sakele?.play()
NSLog("1")
}else{
self.player?.seekToTime(kCMTimeZero)
self.player?.play()
NSLog("2")}
}}
NSLog ("2") never happens. Where is my mistake? any help apreciated
Your sakele is an AVPlayer. But your notification.object is claimed to be (and is cast to) an AVPlayerItem, which is then assigned to finishedPlayer. This makes no sense. When you come to compare if finishedPlayer == self.sakele, it can never succeed, because they are not the same kind of object, let alone the same object.
Related
I'm trying to run animated "DAE" model in SceneKit:
let url = Bundle.main.url(forResource: "art.scnassets/Player(walking)", withExtension: "dae")
let sceneSource = SCNSceneSource(url: url!, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.playRepeatedly] )
let animationIds: NSArray = sceneSource?.identifiersOfEntries(withClass: CAAnimation)
for eachId in animationIds {
let animation: CAAnimation = (sceneSource?.entryWithIdentifier(eachId as! String, withClass: CAAnimation.self))!
animations.add(animation)
}
character?._walkAnimations = animations
But compiler It throws on the line:
let animationIds: NSArray = sceneSource?.identifiersOfEntries(withClass: CAAnimation)
and writes an error:
Cannot convert value of type '[String]?' to specified type 'NSArray'
Please help me to fix that problem.
Thanks in advance.
Why you are converting [String]? to NSArray and then convert each element to String again, no need of it simply use Swift native Array and wrapped the optional value with if let or guard let.
guard let animationIds = sceneSource?.identifiersOfEntries(withClass: CAAnimation) else {
return
}
for eachId in animationIds {
if let animation = sceneSource?.entryWithIdentifier(eachId, withClass: CAAnimation.self) {
animations.add(animation)
}
}
character?._walkAnimations = animations
I am new to swift, and I am trying to build a game which will include a top 5 players name and score, if i just add the players to an array and than restart the game it "deletes" the players, so i am trying to use NSUserDefaults(i need to store just 5 strings and 5 integers), it does not work no matter what,
the code is:
class scoreController: UITableViewController {
var playersArray:[Player] = [Player]()
var nameFromGame = "" //name from game vc
var timeFromGame = 0 //time from game vc
let tmpPlayer = Player(playerName: "", scoreTime: 0)
let playerDefaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
let player1 = Player(playerName: "Bil", scoreTime: 50)
let player2 = Player(playerName: "Bob", scoreTime: 100)
playersArray.append(player1)
playersArray.append(player2)
tmpPlayer.playerName = nameFromGame
tmpPlayer.scoreTime = timeFromGame
playersArray.append(tmpPlayer)
playerDefaults.setObject(playersArray[11], forKey: "players")
print(playersArray )
}
}
I am just trying to save this for now and it crashes, does anyone know why? and also how can i store this in my app so it will save the data?
thank you!
Your player objects probably are the problem here. You must implement two methods in your player class (I am no swift master but it's probaby the same mistake):
- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder;
That should work for you.
Hope it helps!!
PD: check this answer
How to store custom objects in NSUserDefaults
Your Player class needs to conform to NSCoding, and you'll need to store an archived data of your players array and unarchive it when extracting out the data.
Class:
class Player: NSObject, NSCoding {
var playerName: String
var scoreTime: Int
init(playerName: String, scoreTime: Int) {
self.playerName = playerName
self.scoreTime = scoreTime
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeInteger(scoreTime, forKey: "score_time")
aCoder.encodeObject(playerName, forKey: "player_name")
}
required convenience init?(coder decoder: NSCoder) {
guard let playerName = decoder.decodeObjectForKey("player_name") as? String else {
return nil
}
self.init(playerName: playerName, scoreTime: decoder.decodeIntegerForKey("score_time"))
}
}
NSUserdefaults & Archiving/Unarchiving :
let player1 = Player(playerName: "Bil", scoreTime: 50)
let player2 = Player(playerName: "Bob", scoreTime: 100)
let playersArray = [player1, player2]
let playersData = NSKeyedArchiver.archivedDataWithRootObject(playersArray)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(playersData, forKey: "players")
defaults.synchronize()
if let playersArrayData = defaults.objectForKey("players") as? NSData {
let unarchivedPlayers = NSKeyedUnarchiver.unarchiveObjectWithData(playersArrayData) as! [Player]
print(unarchivedPlayers)
}
Hope this helps, please remember to choose answer and up-vote if this solves your question.
You need to could call
playerDefaults.synchronize()
to save the data in the defaults.
EDIT
As facumenzella says, your other problem is storing custom class objects in the defaults. To solve that you need to implement methods in the custom Player class that tell it how to be converted to data. Then you should serialize the object before you store it in the defaults.
I am trying to share a video from a URL using UIActivityViewController.
If I try with an Image I have not problems.
Share Image Works:
let imageThump = NSURL(string: "https://example.com/image.png")
let imageData = NSData(contentsOfURL: imageThump!)
let objectsToShare = [comment!, imageData!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
Share Video is not working. Why?
let videoShare = NSURL(string: "https://example.com/video.mp4")
let videoData = NSData(contentsOfURL: videoShare!)
let objectsToShare = [comment!, videoData!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
THIS IS HOW YOU DO IT ...
guard let urlData = NSData(contentsOfURL: NSURL(string:"https://example.com/video.mov)")!) else {
return
}
print(urlData)
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docDirectory = paths[0]
let filePath = "\(docDirectory)/tmpVideo.mov"
urlData?.writeToFile(filePath, atomically: true)
// File Saved
let videoLink = NSURL(fileURLWithPath: filePath)
let objectsToShare = [videoLink] //comment!, imageData!, myWebsite!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.setValue("Video", forKey: "subject")
// New Excluded Activities Code
if #available(iOS 9.0, *) {
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
} else {
// Fallback on earlier versions
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
}
self.presentViewController(activityVC, animated: true, completion: nil)
Remember that a video is usually a big file, so it'll take some time for it to be downloaded and saved. Here I used part of the first answer but adding some GCD. You'll get better results.
This is how I did it to avoid the App to stay frozen until the video is downloaded and saved:
let url = NSURL(string: "https://example.com/video.mov")
//Show activity indicator
DispatchQueue.global(qos: .background).async {
guard let urlData = NSData(contentsOf: url) else { return }
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath="\(documentsPath)/tempFile.mov"
DispatchQueue.main.async {
urlData.write(toFile: filePath, atomically: true)
//Hide activity indicator
let activityVC = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: filePath)], applicationActivities: nil)
activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact]
self.present(activityVC, animated: true, completion: nil)
}
}
It is not working because this class does not allow NSData to be passed to the Facebook activity type.
"UIActivityTypePostToFacebook
The object posts the provided content to the user’s wall on Facebook.
When using this service, you can provide NSString, NSAttributedString, UIImage, ALAsset, and NSURL objects as data for the activity items. You may also specify NSURL objects whose contents use the assets-library scheme."
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivity_Class/index.html#//apple_ref/doc/constant_group/Built_in_Activity_Types
I have been trying to get this to work as well. Haven't figured out how to get it to work; however, this is why your code is not working.
I'm having an issue getting my video to play via AVPlayer. For some reason I'm getting the error 'Cannot invoke initializer for type 'AVPlayer' with an argument list of type '(URL:NSURL)'
The weird thing is, this exact code snippet works in another one of my projects. I do have the AVKit and AVFoundation frameworks imported.
Well, I realized my dumb mistake. I had a local variable (player), so it died instantly before it could even play. I moved it up a level to be an instance variable so it persists as long as the view controller persists.
var MoviePlaying:Bool = false
let path = NSBundle.mainBundle().pathForResource("big_buck_bunny_720p_50mb", ofType:"mp4")
var player: AVPlayer = AVPlayer()
let playerController = AVPlayerViewController()
#IBAction func PlayTapped(sender: UIButton) {
let moviePlayerController = AVPlayerViewController()
let path = NSBundle.mainBundle().pathForResource("big_buck_bunny_720p_50mb", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
let playerVC = AVPlayerViewController()
var player: AVPlayer = AVPlayer()
playerVC.player = AVPlayer(URL: url)
self.presentViewController(playerVC, animated: true, completion: nil)
All ,
I need to create a sample audio player and recorder, its not erroring but its not playing and perhaps not recording.. any ideas ?
#IBAction func Play(AnyObject)
{
println ("Play")
let path = NSBundle.mainBundle().pathForResource("171010", ofType:"m4a")
let fileURL = NSURL(fileURLWithPath: path)
player = AVAudioPlayer(contentsOfURL: outputFileURL, error: nil)
player.prepareToPlay()
player.delegate = self
player.play()
}
#IBAction func Record(AnyObject)
{
filename = "171010.m4a"
let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var pathComponents = [paths.lastObject as String, filename as String]
outputFileURL = NSURL.fileURLWithPathComponents(pathComponents)
settings.setValue(kAudioFormatMPEG4AAC, forKey: AVFormatIDKey)
settings.setValue(44100.0, forKey: AVSampleRateKey)
settings.setValue(2, forKey: AVNumberOfChannelsKey)
recorder = AVAudioRecorder(URL: outputFileURL, settings: settings, error: &error)
recorder.delegate = self;
recorder.prepareToRecord()
recorder.record()
}
#IBAction func Stop(AnyObject)
{
recorder.stop()
println("Stop")
}
and the globals at the top are :
class MainMenu: UIViewController , AVAudioRecorderDelegate , AVAudioPlayerDelegate {
var filename: String!
var outputFileURL: NSURL!
var recorder: AVAudioRecorder!
var error: NSError?
var settings: NSMutableDictionary = NSMutableDictionary()
var player : AVAudioPlayer! = nil
Any ideas ? I don't know if its recording and creating the file. As I am getting URL is nill on the playing function. This is wired up to a storyboard with buttons. Any help within this simple stuff in swift would be brilliant.
I have changed it to this and this should work ? Can anyone advise ?
#IBAction func Play(AnyObject)
{
println ("Play")
var audioPlayer = AVAudioPlayer()
filename = "171010.m4a"
let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var pathComponents = [paths.lastObject as String, filename as String]
outputFileURL = NSURL.fileURLWithPathComponents(pathComponents)
audioPlayer = AVAudioPlayer(contentsOfURL: outputFileURL, error: nil)
audioPlayer.prepareToPlay()
audioPlayer.delegate = self
audioPlayer.play()
}
#IBAction func Record(AnyObject)
{
filename = "171010.m4a"
let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var pathComponents = [paths.lastObject as String, filename as String]
outputFileURL = NSURL.fileURLWithPathComponents(pathComponents)
settings.setValue(kAudioFormatMPEG4AAC, forKey: AVFormatIDKey)
settings.setValue(44100.0, forKey: AVSampleRateKey)
settings.setValue(2, forKey: AVNumberOfChannelsKey)
recorder = AVAudioRecorder(URL: outputFileURL, settings: settings, error: &error)
recorder.delegate = self;
recorder.prepareToRecord()
recorder.record()
}
The player needs to be an ivar so it isn't popped off the stack before playing.
Here is a github project with a functioning recorder if you need more info.
Also, the convention is to start function names with a lowercase character.