There seems to be a problem with the MPMoviePlayerController where once you're in fullscreen mode and you hold down the fast forward button, letting it seek forward (playing at fast speed) all the way to the end of the video.
Thereafter the you just get a black screen and it's stuck. In other words it does not respond to any taps gestures and you can not get out of this situation. Has anyone else encountered this problem?
Is there anyway to work around it in code?
It seems it's an iOS bug since fast backward to the very beginning won't cause the black screen but fast forward to the end will, and after that the 'play'/'pause' call to the video player never works. I temporarily fix this by adding protected logic into the scrubber refresh callback:
let's assume that monitorPlaybackTime will be called in 'PLAY_BACK_TIME_MONITOR_INTERVAL' seconds period to refresh the scrubber, and in it I add a check logic:
NSTimeInterval duration = self.moviePlayer.duration;
NSTimeInterval current = self.moviePlayer.currentPlaybackTime;
if (isnan(current) || current > duration) {
current = duration;
} else if (self.moviePlayer.playbackState == MPMoviePlaybackStateSeekingForward) {
if (current + self.moviePlayer.currentPlaybackRate*PLAY_BACK_TIME_MONITOR_INTERVAL > duration) {
[self.moviePlayer endSeeking];
}
}
A workaround to solve the black screen, not perfect, hope it can help.
I'm guessing you are not handling the MPMoviePlayerPlaybackDidFinishNotification. You really should if you're not.
Still its unexpected for me that the movie player would go into a "stuck" state like you describe. I would more readily expect it to stop playback automatically and reset when it reaches the end. Anyway, I think your problem will go away if you observe the MPMoviePlayerPlaybackDidFinishNotification and handle the movie controller appropriately.
Ran into the same issue on iOS6. Managed to fix it by registering for the MPMoviePlayerPlaybackDidFinishNotification (as suggested by Leuguimerius) with the following implementation:
- (void)playbackDidFisnish:(NSNotification *)notif {
if (self.player.currentPlaybackTime <= 0.1) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.player stop];
[self.player play];
[self.player pause];
});
}
}
Where self.player is the associated MPMoviePlayerController instance. The check against currentPlaybackTime serves to distinguish the more standard invocations of playbackDidFinish (where the movie is allowed to play at normal speed until its end) from those scenarios where the user fast forwards until the end. Stopping then playing and pausing results in a usable, visually consistent interface even when fast-forwarding to the end.
None of the aforementioned solutions worked for me, so this is what I ended up doing:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("moviePlayerLoadStateDidChange"), name: MPMoviePlayerLoadStateDidChangeNotification, object: nil)
func moviePlayerLoadStateDidChange() {
let loadState = moviePlayerController?.loadState
if loadState == MPMovieLoadState.Unknown {
moviePlayerController?.contentURL = currentmovieURL
moviePlayerController?.prepareToPlay()
}
}
I think the issue is that when the seek foraward button is single pressed, it wants to skip to the next video, that's why a loading indicator appears. Listening for the load state change event, you can specify what the next video should be, and if you don't have any, you can just give it the same url.
Related
When I want to pause the session, this was the only solution for me:
func pauseSession () {
self.sessionQueue.async {
if self.session.isRunning {
self.session.stopRunning()
}
}
}
func resumeSession () {
self.sessionQueue.async {
if !self.session.isRunning {
self.session.startRunning()
}
}
}
This seems to completely stop the session, which is fine, yet looks expensive.
The issue I seem to have is if pause and resume are called near each other in time, the whole app freezes for about 10 seconds, till going back to being responsive. This is mostly due to it still hasn't finished the last process (whether to stop or start).
Is there a solution to this?
The native camera app seems to do this fine. If you open it, open the last photo, you can see the green indicator on the top right going off, meaning the session has paused/stopped. If you swipe down on the photo, the session resumes. If you swipe and let it get canceled, quickly swipe again you can see the session pauses and resumes quickly over and over without any issues.
You may need to change async to sync
self.sessionQueue.sync {
I need to send notifications when the AVPlayer is Play/Paused and Stopped.
For play and pause below is the code
if (self.player.rate > 0.0f) {
NSLog(#" Playing ..")
}
if (self.player.rate == 0.0f) {
NSLog(#" Paused ..")
}
But for stopped also the rate = 0.0 then is there any other property or way to identify the difference between paused and stopped.
For both, paused and stopped the rate = 0.0 and hence need another way for it.
Thanks
There is no stop command for an AVPlayer. So there is no such thing as stopped as distinct from paused. Either the rate is zero (not playing) or it is more than zero (playing).
You can distinguish where the player is within its item (currentTime) so you can tell whether we are at the start, end, or middle; and you can arrange to be notified periodically during play or when the end is reached.
Apart from that, there are no distinctions to be drawn.
I have a SpriteKit game where I have a number of different Sprites Lasers, Planes, etc. A number of these Sprites have a sounds that will play whenever they are visible. Right now I play the sound in the SKSpriteNode itself. This probably isn't what I want because if I have I have 50 of the same sprites, they'll all be playing the same sound and gobbing resources. However, I'm not really sure where would be the best place to play the the sound, because for some of these sprites I would need to check if it should be playing or not. I suppose I could put the sounds in the the GameScene and loop through all sprites during every update to determine what sounds should and should not still be playing, that way there would only be one instance of every sound. I was hoping to keep all the sprite code within the sprite itself, but this would solve the issue with multiple instances of the same sound playing. Is this the correct way to handle sounds?
It is hard to give you a proper code example without seeing your code. You can however get the gist of what you can try with what I have below.
In MyScene add a property BOOL propSound.
Whenever you create a new plane and wonder about adding the prop sound do something like this:
if(propSound == false)
{
propSound = true;
// add your code to play prop sound
}
If you add every newly created plane sprite into a NSMutableArray like this [yourArrayName addObject:newPlane];, you can check the array count every time you remove a plane in order to know if the last plane was removed and the sound needs to be stopped.
if([yourArrayName count] == 0)
{
propSound = false;
// add your code to stop prop sound
}
Maybe you are looking for something like this?
NSMutableArray *coin = [[NSMutableArray alloc] initWithCapacity:1];
SKAction *_coinSound = [SKAction playSoundFileNamed:#"Pickup_Coin4.wav" waitForCompletion:NO];
[coin addObject:_coinSound];
SKAction *_playCoinSound = [SKAction sequence:coin];
Then I just check if the "character" in my case touched the coin. If so it will play the sound.
if([_adventurer intersectsNode:coin]){
[coin runAction:_playCoinSound];
}
Edit:
Saw your comment on your question. I guess you could check if there is at least 1 plane on the screen, if so then you could play a loop of the sound. If there is less than 1 no sound.
Do not play the same sound again if there already are playing. You could use a BOOL to see if the sound is already playing.
BOOL isPlaneSoundOn = false;
if(!isPlaneSoundOn && _planeNode.count > 0){
//play the sound
isPlaneSoundOn = true;
} else if(_plane.count == 0) {
//stop the sound
isPlaneSoundOn = false;
}
Something like that. Maybe you get the idea :)
I would personally play any community sounds off the scene using the SKAction playSoundWithFilename, and assign this sound a key.
Then I would check if the key exists, and if it does not, then play the sound
I use: MPMoviePlayerController to show video.
Below I put list of thumbs from the video.
When pressing a thumb I want to jump to a specific place in the video using: setCurrentPlaybackTime.
I also have a timer updating the selected thumb according to the location of the video using: currentPlaybackTime.
My problem: when calling: setCurrentPlaybackTime, the player keeps giving the seconds before seeking to the specific second. It take few seconds to the player to reflect the new seconds. In the mean time the experience of the user is bad: Pressing a thumb shows it selected for a show time, then the timer updates to the previous thumb, then it jumps back to the thumb I selected.
I tried using (in the timer):
if (moviePlayer.playbackState != MPMoviePlaybackStatePlaying && !(moviePlayer.loadState & MPMovieLoadStatePlaythroughOK)) return;
In order to prevent from the timer to update the selected thumb as long the player is in a transition phase between showing the previous thumb and the new thumb, but it doesn't seem to work. The "playbackState" and "loadState" seems to be totally inconstant and unpredictable.
For solving this issue, this how I have implemented this nasty state coverage in one of my projects. This is nasty and fragile but worked good enough for me.
I used two flags and two time intervals;
BOOL seekInProgress_;
BOOL seekRecoveryInProgress_;
NSTimeInterval seekingTowards_;
NSTimeInterval seekingRecoverySince_;
All of the above should be defaulted to NO and 0.0.
When initiating the seek:
//are we supposed to seek?
if (movieController_.currentPlaybackTime != seekToTime)
{ //yes->
movieController_.currentPlaybackTime = seekToTime;
seekingTowards_ = seekToTime;
seekInProgress_ = YES;
}
Within the timer callback:
//are we currently seeking?
if (seekInProgress_)
{ //yes->did the playback-time change since the seeking has been triggered?
if (seekingTowards_ != movieController_.currentPlaybackTime)
{ //yes->we are now in seek-recovery state
seekingRecoverySince_ = movieController_.currentPlaybackTime;
seekRecoveryInProgress_ = YES;
seekInProgress_ = NO;
seekingTowards_ = 0.0;
}
}
//are we currently recovering from seeking?
else if (seekRecoveryInProgress_)
{ //yes->did the playback-time change since the seeking-recovery has been triggered?
if (seekingRecoverySince_ != movieController_.currentPlaybackTime)
{ //yes->seek recovery done!
seekRecoveryInProgress_ = NO;
seekingRecoverySince_ = 0.0;
}
}
In the end, MPMoviePlayerController simply is not really meant for such "micro-management". I had to throw in at least half a dozen flags for state coverage in all kinds of situations and I would never recommend to repeat this within other projects. Once you reach this level, it might be a great idea to think about using AVPlayer instead.
I have 5 AVPlayerItems in my AVQueuePlayer, which is set to AVPlayerActionAtItemEndAdvance. I hit play on my UI, and play the first, the second and then start playing the third. Then I hit my rewind button. What I want to happen is that the third video rewinds to its start, and I then get a notification that allows me to stop. What I'm seeing is that I get a status of ready to play for the 4th item, followed by a current item changed to the 4th item - then the 4th item plays.
Why does the 4th item become the current item after the 3rd item has rewound to its start
Is the only way I can stop this to set the player to not auto advance (AVPlayerActionAtItemEndPause), observe the end of each item, and hope I get an "end of play" notification for the rewinding of the 3rd item as well as when it plays to its end naturally. Then in my end observer code, I can check the rate of the player, and if rewinding, not advance to the next item.
The way I handled this was in the begin seeking code, set the actionAtItemEnd to AVPlayerActionAtItemEndNone, and then reset it back to AVPlayerActionAtItemEndAdvance when the seeking ends. On iOS6, it appears that one can seek past the start of the track. In the "end seeking" code, I reset the rate and current time before beginning normal playback.
The following method is called by a long press gesture recognizer.
- (IBAction)fastRewind:(id)sender
{
UIGestureRecognizer *recog = (UIGestureRecognizer*)sender;
if (recog.state == UIGestureRecognizerStateBegan) {
if (_player.rate == 1) {
NSLog(#"fastRewind begin\n%#", sender);
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
_player.rate = -2;
}
} else if (recog.state != UIGestureRecognizerStateChanged) {
// Ended/Canceled
NSLog(#"fastRewind end\n%#", sender);
if (_player.rate < 0) {
_player.rate = 0;
if (CMTimeGetSeconds(_player.currentTime) < 0) {
[_player seekToTime:CMTimeMake(0, 1)];
}
_player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
_player.rate = 1;
}
}
}
One may wish to speed up the seek if it is held for a longer period of time.