0

我添加了这段代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoviePlayerStateChanged:)
                                             name:MPMoviePlayerLoadStateDidChangeNotification
                                           object:self.mp];

它将我的 MPMoviePlayer 的每个状态更改触发到此函数中:

- (void) loadMoviePlayerStateChanged:(NSNotification*)notification
{

    MPMoviePlayerController *Player = notification.object;

    MPMoviePlaybackState playbackState = Player.playbackState;
    if (playbackState == MPMoviePlaybackStateSeekingForward)
    {
        NSLog(@"Forward");
    }
    else if (playbackState == MPMoviePlaybackStateSeekingBackward)
    {
        NSLog(@"Backward");
     }
}

它进入这个功能...

但问题在于 MPMoviePlaybackState

当我启动播放器时,playerState 为 1,而所有其他更改为 0。 - 那怎么可能?

编辑:

每次也执行此操作会得到 nil:

NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

if ([reason intValue] == MPMoviePlaybackStateSeekingForward) {

    // done button clicked!

}
4

1 回答 1

1

问题是您正在为电影播放器​​的加载状态更改添加通知,并尝试访问其播放状态,如果您想要播放状态更改通知,您需要为播放更改通知添加观察者

//用于播放更改

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackChanged)
                                                     name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.mp];




- (void)playbackChanged {

switch ([self.mp playbackState]) {
    case MPMoviePlaybackStateStopped:
        NSLog(@"Stopped")
        break;
    case MPMoviePlaybackStatePlaying:
        NSLog(@"Playing");
        break;
    case MPMoviePlaybackStatePaused:
        NSLog(@"Paused");
        break;
    case MPMoviePlaybackStateInterrupted:
        NSLog(@"Interrupted");
        break;
    case MPMoviePlaybackStateSeekingForward:
       NSLog(@"Seeking Forward");
        break;
    case MPMoviePlaybackStateSeekingBackward:
       NSLog(@"Seeking Backward");
        break;
    default:
        break;
}
}
于 2013-10-24T08:13:01.213 回答