如何清理缓冲区或从AVPlayer停止和启动播放器 - iOS

时间:2015-06-30 19:33:33

标签: ios objective-c avplayer playback

我有这个问题,这并不像我想的那么容易......我已经失去了一些时间,但却没有。

我知道,我有一个方法暂停,但我想停下来开始并避免崩溃应用程序..

任何人都可以提供帮助,非常感谢。

//首先

-(void) setupAVPlayerForURL{    
    @try {
        NSURL *url = [[NSURL alloc] initWithString:URL_RADIO_STREAM];

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

        [anItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        [anItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
        [anItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

        player = [AVPlayer playerWithPlayerItem:anItem];
        //[player addObserver:self forKeyPath:@"status" options:0 context:nil];


    }
    @catch (NSException *exception) {

        NSLog(@"%@", exception.reason);
        [[self textDebug]setText : exception.description];

    }
}

//后。

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

    /*if (!player)
    {
        return;
    }*/

    if ([object isKindOfClass:[AVPlayerItem class]])
    {
        AVPlayerItem *playerItem = (AVPlayerItem *)object;

        if ([keyPath isEqualToString:@"status"])
        {   //yes->check it...
            switch(playerItem.status)
            {
                case AVPlayerItemStatusFailed:
                    NSLog(@"player item status failed");
                    self.BtnPlay.userInteractionEnabled = FALSE;
                    break;
                case AVPlayerItemStatusReadyToPlay:
                    NSLog(@"player item status is ready to play");
                    self.BtnPlay.userInteractionEnabled = TRUE;
                    break;
                case AVPlayerItemStatusUnknown:
                    NSLog(@"player item status is unknown");
                    self.BtnPlay.userInteractionEnabled = FALSE;
                    break;
            }
        }

        if ([keyPath isEqualToString:@"playbackBufferEmpty"])
        {
            NSLog(@"player item status playbackBufferEmpty");
            if (playerItem.playbackBufferEmpty) {
                //Your code here
                [[NSNotificationCenter defaultCenter] postNotificationName:@"message" object:@"Buffering..."];

                if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
                {
                    task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
                        [self setupAVPlayerForURL];
                    }];
                }
            }
        }

        if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"])
        {
            NSLog(@"player item status playbackLikelyToKeepUp");
            if (playerItem.playbackLikelyToKeepUp)
            {
                //Your code here
                [player play];

                if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
                {
                    [[UIApplication sharedApplication] endBackgroundTask:task];
                    task = 0;
                }
            }
        }

    }
}

//最后。

-(IBAction) BtnPlay:(id)sender {

    if(self.BtnPlay.touchInside){
        if (player.rate == 1.0) {
            [player pause];
            [BtnPlay setTitle:@"Play" forState:UIControlStateNormal];
        } else {
            [player play];
            [BtnPlay setTitle:@"Pause" forState:UIControlStateNormal];
        }
    }
}

//但我不知道如何停止此而不是暂停......

0 个答案:

没有答案