I have used replaceCurrentItemWithPlayerItem: with AVPlayer but can it be also used with AVQueuePlayer?
In following apple doc, it is given that:
This method must only be invoked on player instances created without
queues. If the player is initialized with multiple items the method
throws an exception.
What does it means? Please guide.
If it signifies that it throws exception if we use it with AVQueuePlayer, then I tried using it without any exception but dont know if it is right way to use.
https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayer_Class/
No you can't, as per Apple docs:
Important
This method is not intended for use with AVQueuePlayer. If the player is initialized with multiple items this method throws an exception.
Here's the link: https://developer.apple.com/reference/avfoundation/avplayer/1390806-replacecurrentitemwithplayeritem?language=objc
Related
I used NSURLConnectionobject and called its method cancel sometimes.
Now I should replace NSURLConnection -> NSURLSession. NSURLSession operates with tasks which have cancel method too.
The problem is -[NSURLConnection cancel] just stop the handling of requests but if I use -[NSURLSessionTask cancel] it produces "cancelling error". So how to properly distinguish if cancel is called manually or if a real error is occurred?
I've found 3 solutions:
subclass task/session class
create a custom property via method swizzling in task/session class
the simplest but not very beautiful way - task has string property taskDescription and documentation says that developers are free to use it as they want.
Compare the error code to NSURLErrorCancelled. This error code is generated only when your code cancels the request.
I'm trying to remove a video that the user recorded and now has decided to delete. I have both a file URL and then obviously a path too.
I have tried using the removal methods of the NSFileManager class for both the file and the path, but I'm having trouble getting a completion result to confirm whether the file has actually been deleted or not.
Here is an example of how I'm trying to remove the file in Swift:
let deleted = try! NSFileManager.defaultManager().removeItemAtURL(self.fileURL)
This will give me a warning of Constant 'deleted' inferred to have type '()', which may be unexpected
Using removeItemAtPath produces the same warning. If I run the code, deleted simply logs as ()
If I look at the method signatures for these two methods it's clear that they do not return a result, but take for example the documentation for the removeItemAtURL method: true if the item was removed successfully or if URL was nil. Returns false if an error occurred. If the delegate aborts the operation for a file, this method returns true. However, if the delegate aborts the operation for a directory, this method returns false.
It also mentions taking an error parameter but doesn't have one. And then finally in the last sentence it says: Returns YES if the item was removed successfully or if URL was nil.
As a last resort I figured I could just become the delegate for NSFileManager, but it's delegate protocol does not offer any completion methods.
How can I properly remove a file or path and then verify that it has actually been deleted?
You're right that in Swift it returns Void (I believe the docs include the return value description for when you have Objective-C or Both turned on, as opposed to just Swift -- they've got a lot of work to do updating the docs).
If you continue reading the documentation for that function, you'll see a section titled "Handling Errors in Swift", which says:
In Swift, this method returns Void and is marked with the throws
keyword to indicate that it throws an error in cases of failure.
You call this method in a try expression and handle any errors in the
catch clauses of a do statement, as described in Error Handling in The
Swift Programming Language (Swift 2.1) and Error Handling in Using
Swift with Cocoa and Objective-C (Swift 2.1).
So wrap your call in a try/catch and handle the error if there is one. If there wasn't an error, it succeeded.
I am reading someone's code. He set AVAudioPlayer to nil after user clicking a button to stop the audio from playing. I am wondering should we set object to nil after we don't need it anymore? Or should we set AVAudioPlayer to nil after we are trying to stop playing the audio?
Usually, this is not needed but there are exceptions. When you have a local variable, you almost never need to set it to nil because when it goes out of scope, it will be destroyed anyway.
When you have a variable on instance scope (a property), it's more difficult because you often want to release the memory while the instance is still being used (for example, a property in a controller). In this case, setting to nil is completely correct because you have no other way to remove the object from memory.
The fact that it's a AVAudioPlayer instance shouldn't be relevant although the player usually takes a big chunk of memory so it's good to watch for its instances.
If you have a strong member variable, you may be able to free a significant amount of resources earlier on by deliberately disposing of objects by setting them to nil when no longer required. The benefit will depend on the specific type of object in question.
I've been trying to use AVAudioPlayer to play remote MP3 files in my app and I've read some of the other answers on Stack Overflow but I still cannot get it to work. The common suggestion I'm reading is to use AVPlayer instead of AVAudioPlayer. I don't know why this is. Also, in one of the questions the accepted answer mentioned that one needs to create an instance of AVPlayer in order to use it in the app. What should I do?
What you've read is correct. Creating an instance of AVPlayer will allow you to successfully run your code.
You should initialize your AVPlayer outside of where you want to call it.
var myPlayer = AVPlayer()
Now, in a separate place in your code, try something like this:
func playAudio() {
//initialize whatever you have to (you seem to have that correct)
//now call myPlayer.play(), and that should work correctly
}
Let me know if this helps.
I'm using multiple instances of MPMoviePlayerController,
Which does not allow playing more than one video at a time
(On different instances of course)
In order not to play two instances at the same time, i stop and dismiss previous player once a new one is playing.
After plying a few videos, it throws an exception which i can't catch because it caused by internal notifications sent between instances.
It seems that calling 'stop' method to a player which has loadState == MPMovieLoadStateUnknown, causes bad internal state and throws the exception.
So, the solution was not to allow stopping a player in that state (A player has this state for about a second when it initializes playback).
Here's a reference to the very helpful mail chain that helped me find it