Domanda

Sto scrivendo un app che ci sarà un sacco video utilizzando il MPMoviePlayerController sul iPad. Il problema è l'applicazione stava lavorando bene e la riproduzione dei video in cui ho smesso di lavorare circa 15 ore fa, ma ora i video non vengono riprodotti. Il MPMoviePlayerController mostrerà il primo fotogramma del video, e in visualizzazione a schermo intero posso fregare attraverso il bel film, ma quando ho colpito giocare solo pause subito. Ho il codice qui sotto, quando il debug ho notato che quando chiamo giocare invia un MPMoviePlayerPlaybackStateDidChangeNotification con il playbackState essere MPMoviePlaybackStatePlaying e poi subito invia un'altra notifica MPMoviePlayerPlaybackStateDidChangeNotification con playbackState essere MPMoviePlaybackStatePaused. Non sono sicuro se questo aiuta ma per favore fatemi sapere se si vede qualcosa di sbagliato nel mio codice o avere un idea, grazie.

- (void)handleNotification:(NSNotification *)notification {
    if ([[notification name] isEqualToString:MPMoviePlayerPlaybackStateDidChangeNotification]) {
        if (_videoPlayer.playbackState == MPMoviePlaybackStatePlaying) {
            _playButtonLarge.hidden = YES;
            _scrubber.maximumValue = _videoPlayer.duration;
            [_playPauseButton setBackgroundImage:[UIImage imageNamed:@"video_controls_pause.png"] forState:UIControlStateNormal];
            if (_updateScrubberTimer == nil) {
                _updateScrubberTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateScrubber) userInfo:nil repeats:YES];
            }
        } else if (_videoPlayer.playbackState == MPMoviePlaybackStatePaused || _videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
            [_playPauseButton setBackgroundImage:[UIImage imageNamed:@"video_controls_play.png"] forState:UIControlStateNormal];
            _playButtonLarge.hidden = NO;
            if (_updateScrubberTimer != nil) {
                [_updateScrubberTimer invalidate];
                _updateScrubberTimer = nil;
            }
            if (_videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
                _scrubber.value = 0.0f;
                _timePlayedLabel.text = @"0:00";
                _timeRemainingLabel.text = @"-0:00";
                _videoPlayerBG.hidden = NO;
            }
        }
    } else if ([[notification name] isEqualToString:MPMoviePlayerPlaybackDidFinishNotification]) {
        NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        if ([reason intValue] == MPMovieFinishReasonPlaybackEnded) {
            _videoPlayerBG.hidden = NO;
        }
        _scrubber.value = _scrubber.maximumValue;
    }
}

- (void)playPause {
    if ([_videos count] > 0) {
        if (_videoPlayer.playbackState == MPMoviePlaybackStatePaused || _videoPlayer.playbackState == MPMoviePlaybackStateStopped) {
            _playButtonLarge.hidden = YES;
            _videoPlayerBG.hidden = YES;
            if ([_videoPlayer contentURL] == nil) {
                Video *video = [_videos objectAtIndex:0];
                [_videoPlayer setContentURL:video.videoURL];
            }
            if (![_videoPlayer isPreparedToPlay]) {
                [_videoPlayer prepareToPlay];
            }
            [_videoPlayer play];
        } else if (_videoPlayer.playbackState == MPMoviePlaybackStatePlaying) {
            _playButtonLarge.hidden = NO;
            [_videoPlayer pause];
        }
    }
}
È stato utile?

Soluzione

Credo di aver capito, ho messo il seguente codice prima di ogni chiamata a giocare

if (![_videoPlayer isPreparedToPlay]) { [_videoPlayer prepareToPlay]; }

ora funziona, se qualcun altro ha alcun input fatemelo sapere

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top