Assertion failure in -[MPMoviePlayerControllerNew _moviePlayerDidBecomeActiveNotification:] - ios

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

Related

AVAudioSequencer - How to be notified when playback ends

I am using AVAudioSequencer with AVAudioEngine and I am having just one single problem: how do I get the sequencer to notify when a track or file has reached its end?
I know there is an AVAudioPlayerNodeCompletionHandler thing, but then, AVAudioSequencer is not derived from AVAudioPlayerNode.
So far, the only thing I could think of was periodically comparing the currentPositionInBeats property in AVAudioSequencer with the lengthInBeats property in AVMusicTrack (the isPlaying property in AVAudioSequencer just doesn't go false when the sequencer goes beyond this limit,) but I don't like polling.
Any help is appreciated.

Can I use replaceCurrentItemWithPlayerItem with AVQueuePlayer?

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

Should I set object to nill in Swift

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.

Access an AVAudioPlayer on its current thread

I'm using a series of AVAudioPlayers concurrent with the main thread using dispatch_async. Before playing each one I must query, whether it is playing using its isPlaying method.
I am generally unfamiliar with multithreading, so how would I go about calling the isPlaying method on the thread that the AVAudioPlayer is currently working on?
You can access the property playing directly on main thread because the operation is read, so it is safe I think :)
But you can't use it to judge if playback has completed as document says : Important: Do not poll this property to determine when playback has completed, instead implement the specified delegate method
You should use delegate and implement the method - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag, if the method is called, it means the player stop.

endturnwithnextparticipants, how does it work?

I am making a turnbased game for iOS with game center, 2 participants per match. I would like to implement a time limit on every turn, so that a player don't have to wait forever for the other player to finish its turn. I have tried:
currentMatch endTurnWithNextParticipants:[[NSArray alloc] initWithObjects:nextParticipant,nil] turnTimeout:GKTurnTimeoutDefault matchData:data completionHandler:^(NSError *error)
but nothing happens, the player still has forever to do their turn, so I am obviously missing something here.
What happens when the time limit is reached? How does gamecenter handle this, and where should I handle this?
That method updates the data stored on Game Center for the current match.
According to Apple Docs:
If the next player to act does not take their turn in the specified interval, the next player in the array receives a notification to act. This process continues until a player takes a turn or the last player in the list is notified.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
I think you need to also end the players go on their end programatically.

Resources