0

我不知道我哪里出错了。[audioPlayer 播放]、[audioPlayer 停止] 不起作用。因为 audioPlayer 为零。但是,如果我尝试在状态检查 AVPlayerStatusReadyToPlay 中播放 audioPlayer,那么它可以正常工作,但其他按钮仍然无法正常工作,同样的 audioPlayer nil。

自定义类.h

@property (nonatomic, retain) AVPlayer *audioPlayer;

自定义类.m

-(void)prepareAudioPlayerFor:


    NSURL *urlPath = [NSURL URLWithString:@"http://freshmp3music.ru/music/04.2013/chris_brown_-_fine_china_(www.freshmp3music.ru).mp3"];

   AVAsset *asset = [AVURLAsset URLAssetWithURL:urlPath options:nil];
   AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];

   AVPlayer *player = [AVPlayer playerWithPlayerItem:anItem];

    audioPlayer = player;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[audioPlayer currentItem]];

    [audioPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if (object == audioPlayer && [keyPath isEqualToString:@"status"]) {
        if (audioPlayer.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");

        } else if (audioPlayer.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayerStatusReadyToPlay");
            if(audioPlayer.currentItem == nil)
            {
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"No Audio Anthem Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];

            }else
            {

                NSLog(@"readyToPlay");
              //  [audioPlayer play];  // Here Play works fine but still other buttons not work
            }

        } else if (audioPlayer.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");

        }
    }
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
    NSLog(@"finished");

   // [self playPauseBtn:nil];
    CMTime fiveSecondsIn = CMTimeMake(0, 1);
    [audioPlayer seekToTime:fiveSecondsIn];
    [audioPlayer pause];
}

- (IBAction)playBtn:(id)sender {
    if (audioPlayer == nil) {
        NSLog(@"Player Nil");
    }
    [audioPlayer play]; // this don't work why

}

OUTPUT : AVPlayerStatusReadyToPlay
OUTPUT : readyToPlay
OUTPUT : Player Nil
4

0 回答 0