2012-05-16 44 views
6

我在播放本地.wav文件時遇到了一些麻煩。我的AVPlayerStatus正在打印0,我假設這意味着它尚未準備好播放。我似乎無法弄清楚,但已經說過,這是我第一次嘗試使用AVPlayer做任何事情,所以它可能很簡單。我會很感激任何幫助。代碼如下:AVPlayerStatus本地文件未準備好

我應該補充說我的聲音沒有播放!

-(void) playWordSound:(UILabel *)label 
{ 
    NSString *path; 
    switch (label.tag) 
    { 
     case 1: 
      path = [[NSBundle mainBundle] pathForResource:@"humpty" ofType:@"wav"]; 
      break; 
     case 2: 
      path = [[NSBundle mainBundle] pathForResource:@"dumpty" ofType:@"wav"]; 
      break; 
     case 3: 
      path = [[NSBundle mainBundle] pathForResource:@"saty" ofType:@"wav"]; 
      break; 
     case 4: 
      path = [[NSBundle mainBundle] pathForResource:@"on 2" ofType:@"wav"]; 
      break; 
     case 5: 
      path = [[NSBundle mainBundle] pathForResource:@"a 3" ofType:@"wav"]; 
      break; 
     case 6: 
      path = [[NSBundle mainBundle] pathForResource:@"wall" ofType:@"wav"]; 
      break; 
    } 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    AVQueuePlayer *player; 
    static const NSString *ItemStatusContext; 
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url]; 
    [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; 
    player = [AVPlayer playerWithPlayerItem:playerItem]; 

    NSLog(@"%d",player.status); 
    [player play]; 
} 

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

回答

11

下面是蘋果採用AVPlayItem -initWithURL:方法時指出了一些重要的陷阱:

特別注意事項

此方法將立即返回該項目,但 與狀態AVPlayerItemStatusUnknown。

如果URL包含播放器項目可以使用的有效數據, 狀態稍後將更改爲AVPlayerItemStatusReadyToPlay。

如果該URL不包含有效數據,或者 播放器項目不能使用該URL,則狀態將變爲AVPlayerItemStatusFailed。

我想象的情況是,因爲你發送-play消息沒多久創建AVPlayerItem對象之後,其地位仍然AVPlayerItemStatusUnknown(同樣適用於AVPlayer對象),因爲它沒有得到足夠的時間加載資源並將狀態更改爲AVPlayerItemStatusReadyToPlay

我sugestions:

  • 要麼請檢查AVPlayerItem/AVPlayer狀態變化,並在稍後階段調用-play;

,或者更簡單,

  • 負載的AVAsset或從NSURL的AVURLAsset對象,並使用AVPlayerItem –initWithAsset:方法,它應該返回一個準備打的對象,然後調用立即撥打-play之後,如果你這麼請。
+0

初始化爲AVAsset修復它!非常感謝你的幫助! –

+0

很高興我能幫到你。乾杯! – MiguelB

1

0相當於AVPlayerStatusUnknown,我想這是因爲玩家沒有嘗試加載URL,直到你實際調用play

documentation

AVPlayerStatusUnknown

指示球員的狀態還不得而知,因爲它並沒有試圖加載新的媒體資源進行播放。

+0

我切換了代碼,這樣''player play]'在'player.status'之前被調用,並且仍在輸出'0'。我應該在我原來的帖子的某處以粗體提到真正的問題是聲音不能播放! – garethdn