2016-04-14 14 views
4

.play()を呼び出すと.pause()が呼び出されてもそれは継続しますが、.pause()の後に30-60秒待ってから.playAVPlayerは一時停止してから再開できない+若干の待ち時間がある

  • AVPlayerStatus.Failedは偽

  • AVPlayerStatus.ReadyToPlayが、私はそれを動作させるために、URLでプレイヤーを再初期化する必要があり

trueを返す返され、再生するために失敗します。 プレイヤーが再生できる場合は、.play()を呼び出すだけですが、そうでなければ再初期化したいのですが、プレイヤーが再生可能かどうかを検出する方法はありますか?ところで、それは.pls拡張子を持つラジオリンクです

+0

ステータスチェックは、その60秒の遅延の後に行われると思います。 – sailens

+0

はい正確に私はこれらの両方のチェックを60秒の休止の後に行います – turushan

+0

私はこれがなぜ起こっているのか分かりません:S – sailens

答えて

1

実際には、長い時間の後に再開するとプレーヤーがリムボーになっていることを示していません。 (私のテストでは、この状況でAVPlayerItemから受け取ったメタがnullであることがわかりました)

とにかく..私はインターネットから集まったものから50〜60秒後に再開しようとすると、プレイヤーはバックグラウンドでバッファリングし続けます。停止機能はここで良いでしょう。

私の解決方法: それが50秒経過したかどうかを確認する単純なタイマーで、再開メソッドが呼び出されたときに新しいプレーヤーを開始することを知るフラグを更新します。

func pausePlayer() { 
    .. 
    player.pause() 
    .. 

    // Will count to default 50 seconds or the indicated interval and only then set the bufferedInExcess flag to true 
    startCountingPlayerBufferingSeconds() 
    bufferedInExcess = false 
} 


func startCountingPlayerBufferingSeconds(interval: Double = 50) { 
    timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: Selector("setExcessiveBufferedFlag"), userInfo: nil, repeats: false) 
} 

func setExcessiveBufferedFlag() { 
    if DEBUG_LOG { 
     print("Maximum player buffering interval reached.") 
    } 
    bufferedInExcess = true 
} 

func stopCountingPlayerBufferingSeconds() { 
    timer.invalidate() 
} 

func resumePlayer() { 
    if haveConnectivity() { 
     if (.. || bufferedInExcess) { 
      startPlaying(true) 
     } else { 
      .. 
      player.play 
     } 
     .. 
    } 
} 

func startPlaying(withNewPlayer: Bool = false) { 
    if (withNewPlayer) { 
     if DEBUG_LOG { 
      print("Starting to play on a fresh new player") 
     } 

     // If we need another player is very important to fist remove any observers for 
     // the current AVPlayer/AVPlayerItem before reinitializing them and add again the needed observers 
     initPlayer() 

     player.play() 
     ... 
    } 
    ... 
} 
0

私は目的を達成するために私のソリューションを投稿しています。だから私は、一時停止ボタンを押すと、タイマーを追加し、そのタイマが85秒後(それらの秒後にバッファリングが停止している)、私はYESにブール値を設定し、オーディオを再開すると、そのブール値がYESならば私は新しいプレーヤーを作成します。

-(void)play{ 
if (self.player){ 

    if (self.bufferingExpired){ 
     self.bufferingExpired = NO; 
     [self initializePlayerWithStation:self.currentStation]; 
    }else{ 
     [self.player play]; 
    } 

    [self.resumeTimer invalidate]; 
    self.resumeTimer = nil; 
    } 

} 

-(void)pause{ 

    if (self.player){ 
     [self.player pause]; 
     [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationAudioPaused object:nil]; 
     self.resumeTimer = [NSTimer scheduledTimerWithTimeInterval:kResumeStreamingTimer target:self selector:@selector(addNewPlayerItemWhenResuming) userInfo:nil repeats:NO]; 
    } 

} 

-(void)addNewPlayerItemWhenResuming{ 
    self.bufferingExpired = YES; 
} 

-(void)initializePlayerWithStation:(RadioStation*)station{ 
    _currentStation = station; 
    _stream_url = station.url; 

    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:_stream_url]; 

    if (self.player != nil){ 
     [[self.player currentItem] removeObserver:self forKeyPath:@"status"]; 

    } 

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

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playerItemFailedToPlayToEndTime:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:item]; 

    self.player = [AVPlayer playerWithPlayerItem:item]; 

}