AVPlayer连接丢失重启

时间:2014-12-18 22:05:16

标签: ios objective-c

我正在使用AVPlayer对象在我的iOS应用中播放远程无线电流。流可以正常工作并在后台播放。

进行一些连接测试我遇到了一些问题。一旦玩家连接丢失,玩家就会停止(正如您所期望的那样)但是当连接恢复时我无法再让玩家重新开始。

我已经粗略地设置了一个计时器,每秒钟点击[玩家游戏]以强制它开始但没有运气。我最好的猜测是基于所要求的数据不足而已经死亡。

我确实有一个观察者设置来监控播放器何时准备好开始或者是否有错误但看起来在初始时间之后根本没有被调用。

我的问题是如何强制AVPlayer在可用时再次开始获取流。

我已将音频播放器细分为BCRadio

-(BCRadio*)initWithRadioOneAndAutoPlay:(BOOL)autoPlay whenReady:(void(^)(void))ready whenFailed:(void(^)(void))failed whenBecameActive:(void(^)(void))active {

    // Current item
    self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://69.64.92.79:8276"]];

    // Super it
    self = [super initWithPlayerItem:self.playerItem];

    // Observe for it become ready
    [self addObserver:self forKeyPath:@"status" options:0 context:nil];

    // If app comes from background and if app goes inactive
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBecameActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

    // Set blocks
    self.playerReady = ready;
    self.playerFailed = failed;
    self.playerActive = active;
    self.willAutoPlay = autoPlay;

    // Register for remote notifications
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Play is pressed
    [[NSNotificationCenter defaultCenter] addObserverForName:@"TogglePlay" object:nil queue:NULL usingBlock:^(NSNotification *notification){
        [self doPlay:YES];
    }];

    // Pause is pressed
    [[NSNotificationCenter defaultCenter] addObserverForName:@"TogglePause" object:nil queue:NULL usingBlock:^(NSNotification *notification){
        [self doPlay:NO];
    }];


    // Check if playing
    NSTimer *timer = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer){
        [self statusMonitor];
    } repeats:YES];
    [timer fire];

    // Monitor reachability and pause when needed
    Reachability* reach = [Reachability reachabilityWithHostname:@"69.64.92.79"];
    reach.reachableBlock = ^(Reachability*reach){
        if(self.willAutoPlayOnResume && !self.playing){
            self.willAutoPlayOnResume = NO;
            [self doPlay:YES];
        }
    };
    reach.unreachableBlock = ^(Reachability*reach){
        if(self.playing){
            self.willAutoPlayOnResume = YES;
            [self doPlay:NO];
        }
    };
    [reach startNotifier];

    return self;

}

0 个答案:

没有答案