MPMoviePlayerController - detect pressing Next/Prev buttons
Asked Answered
R

5

11

I'm using MPMoviePlayerController and I need to detect pressing Next/Prev buttons. I tried several things, none of which seem to works.

Here is what I tried:

  • remote control events
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}  
-(void) viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}  
-(BOOL)canBecomeFirstResponder
{
    return YES;
}  
-(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
    // stuff
}

The problem is remoteControlReceivedWithEvent method is never called. I've read that this will not work in iOS version higher than 6 - I'm working on iOS 7

  • notifications

I tried using MPMoviePlayerPlaybackStateDidChangeNotification and check against MPMoviePlaybackStateSeekingForward or MPMoviePlaybackStateSeekingBackward - unfortunatelly, these playback state are set when dragging the playback bar, not when pressing Next/Prev buttons.

Any ideas?

Rouble answered 19/8, 2014 at 11:6 Comment(6)
#3594183Hicks
as You can see in my post, thats the first thing I tried and it doesn't workRouble
Would it be that when MPMoviePlayerController shows, viewWillDisappear is called and so the receive remote control is ended? Please try to comment out the viewWillDisappear and try again.Littman
@Littman Thanks for the suggestion, I thought the same thing, no such luck! :(Baba
Have you had a look at the MPMoviePlayerNowPlayingMovieDidChangeNotification? It tells you when the current movie playing changes, and if you have a dictionary or array with the movies you can determine whether the movie that's being played is the next or previous one. developer.apple.com/library/ios/documentation/MediaPlayer/…Sorn
@Sorn Yep, tried that, the notification doesn't fire after tapping previous or next.Baba
G
0

You change MPMoviePlayerController overlayView just like change UIImagePickerController overlayView to implement the function you need.

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
    initWithContentURL:someUrl];

moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];

NSArray *windows = [[UIApplication sharedApplication] windows];

if ([windows count] > 1) {

     UIWindow *moviePlayerWindow = [[UIApplication sharedApplication] keyWindow];
     [moviePlayerWindow addSubview:yourCustomOverlayView];
}
Gwenngwenneth answered 19/8, 2014 at 11:6 Comment(0)
A
0

Sorry I don´t understand your problem very well, but if you want use the controls out your App in the Control Center, you can use:

      // You need cath the singleton
MPRemoteCommandCenter *myRemote = [MPRemoteCommandCenter sharedCommandCenter];
//And add the selector you can fire depends on the button, a couple of examples:
[myRemote.playCommand addTarget:self action:@selector(myPlayMethods)];
[myRemote.nextTrackCommand addTarget:self action:@selector(myNextTrackMethods)];
Amends answered 8/11, 2014 at 21:19 Comment(0)
U
0

try registering for event in :

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // Turn on remote control event delivery
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Set itself as the first responder
    [self becomeFirstResponder];
}

Also Don't set kAudioSessionProperty_OverrideCategoryMixWithOthers property

Undemonstrative answered 8/11, 2014 at 22:16 Comment(1)
there are also solution which say to subclass Uiapplication and impelment -(void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent there did you try that?Undemonstrative
M
0

Have you tried MPMoviePlayerNowPlayingMovieDidChangeNotification? If this does not work then i would suggest moving to a lower level API i.e. AVPlayer. It provides fine grained control over all the actions while video playing and otherwise.

Mulley answered 16/2, 2015 at 14:27 Comment(0)
R
0

You need to register to handle a notification for moviePlayerLoadStateChanged. When you press the next/prev buttons moviePlayerLoadStateChanged will be called and the loadState will be MPMovieLoadStateUnknown

-(void)registerMyStuff {
    [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(moviePlayerLoadStateChanged:)
                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                           object:self.mpc];
}

- (void)moviePlayerLoadStateChanged:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayer = notification.object;
    MPMovieLoadState loadState = moviePlayer.loadState;

    if(loadState == MPMovieLoadStateUnknown)
    {
        // this is where the next/prev buttons notify
        // there is no video in this state so load one
        // just reload the default movie

        NSLog(@"MPMovieLoadStateUnknown");
        self.mpc.contentURL = self.fileURL;
        [self.mpc prepareToPlay];

        return;
    }
    else if(loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"MPMovieLoadStatePlayable");
    }
    else if(loadState & MPMovieLoadStatePlaythroughOK)
    {
        NSLog(@"MPMovieLoadStatePlaythroughOK");
    } else if(loadState & MPMovieLoadStateStalled)
    {
        NSLog(@"MPMovieLoadStateStalled");
    }
}
Ruthenious answered 20/5, 2015 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.