2012-11-06 35 views
0

我正在製作一個無線電流媒體應用程序,我的應用程序工作正常。我的屏幕上有兩個按鈕,一個用於播放,另一個用於暫停,另外還有一個標籤,用於指示播放器的狀態。我沒有任何問題,使這個標籤顯示狀態「正在播放」或「已暫停」我的問題是,當我按下播放按鈕有一個時間緩衝區正在收集信息,我不能找出一種方式來顯示「緩衝......「標籤,然後播放器開始流式傳輸音頻。使用MPMoviePlayer緩衝流中的標籤

這是用於流式廣播電臺的代碼。

NSString *url = [NSString stringWithFormat:@"http://66.7.218:8816"]; 

     player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]]; 
     player.movieSourceType = MPMovieSourceTypeStreaming; 
     player.view.hidden = YES; 
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
     [[AVAudioSession sharedInstance]setActive:YES error:nil]; 
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
     [player prepareToPlay]; 

     [player play]; 

我使用了一個函數,我創建了一個名爲changeStatus的函數,每秒調用一次該函數來標識播放器的狀態。

-(void)changeStatus 
{ 

    if (player.loadState == MPMovieLoadStateUnknown) { 
     status.text = @"Buffering..."; 
    } 
    if(player.playbackState == MPMoviePlaybackStatePlaying) 
     { 
      status.text = @"Playing."; 
     } 
    if(player.playbackState == MPMoviePlaybackStatePaused) 
     { 
      status.text = @"Paused"; 
     } 

} 

我真的需要解決這個問題,我盡我所能解決它。希望你能幫我!提前致謝。

回答

0

我實現瞭解決問題。缺少的部分是if子句,該子句只在玩家準備玩時纔會開始播放信號。如果沒有這些,播放器在緩衝區完全加載之前就開始播放,因此它不播放音頻。這就是我的播放標籤立即顯示而不是緩衝的原因。

NSString *url = [NSString stringWithFormat:@"http://66.7.218:8816"]; 

     player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]]; 
     player.movieSourceType = MPMovieSourceTypeStreaming; 
     player.view.hidden = YES; 
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
     [[AVAudioSession sharedInstance]setActive:YES error:nil]; 
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

     [player prepareToPlay]; 

     if(player.isPreparedToPlay) 
     { 
     [player play]; 
     } 

此外,MPMoviePlaybackStateInterrupted指的是執行流時的緩衝過程。所以如果你想在緩衝過程中發生一些事情,請參考這個方法。

-(void)changeStatus 

    { 

     if(player.playbackState == MPMoviePlaybackStatePlaying) 
      { 
       status.text = @"Playing."; 
      } 
     if(player.playbackState == MPMoviePlaybackStateInterrupted) 
      { 
       status.text = @"Buffering..."; 
      } 

    } 

非常感謝用戶那個幫我解決這個問題的人。

+0

嗨,我正面臨一個問題。在我的情況下,播放器處於緩衝狀態時,播放狀態爲MPMoviePlaybackStatePaused。我從來沒有得到MPMoviePlaybackStateInterrupted狀態。請幫助我。 – HarshIT

0

其實有一個名爲

MPMediaPlaybackIsPreparedToPlayDidChangeNotification

通知觀察員將得到通知後,準備播放。它或多或少具有相同的效果,但具有不同的機制。

+0

感謝您的回答,我最近在XCode中瞭解了觀察者,當時我不知道他們的存在。 –