2011-09-14 19 views
15

मेरे पास एक ऑडियो प्लेयर है जिसे मैं एवीप्लेयर का उपयोग कर बना रहा हूं।AVPlayer replaceCurrentItemWithPlayerItem आईओएस पर काम नहीं कर रहा है 4.3.3+

वर्तमान में, मैं चारों ओर player उदाहरण रखने के लिए और मैं एक नया AVPlayerItem बनाने जब मैं पटरियों स्वैप करने के लिए (या तो एक मैनुअल चयन से या ट्रैक अंत तक पहुँच गया है) की जरूरत है और मैं नए आइटम के साथ replaceCurrentItemWithPlayerItem कहते हैं।

दस्तावेज़ों के मुताबिक, replaceCurrentItemWithPlayerItem एक असीमित ऑपरेशन है, इसलिए मैं वर्तमान इटैम प्लेयर का मुख्य पथ भी देखता हूं। जब इसे बुलाया जाता है, तो मैं अपने खिलाड़ी को खेलने के लिए कहता हूं। यहाँ

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset]; 
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:CHStreamingAudioPlayer_PlayerItemStatusChangedContext]; 

if (!_player) { 
    _player = [[AVPlayer alloc] initWithPlayerItem:playerItem]; 
    [_player addObserver:self forKeyPath:@"status"   options:NSKeyValueObservingOptionNew context:CHStreamingAudioPlayer_PlayerStatusChangedContext]; 
    [_player addObserver:self forKeyPath:@"currentItem"  options:NSKeyValueObservingOptionNew context:CHStreamingAudioPlayer_PlayerCurrentItemChangedContext]; 
} else { 
    [_player replaceCurrentItemWithPlayerItem:playerItem]; 
} 

और कुंजी मान अवलोकन कॉलबैक है:

यहाँ प्रासंगिक कोड है

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if (context == CHStreamingAudioPlayer_PlayerCurrentItemChangedContext) { 
     NSLog(@"Player's current item changed! Change dictionary: %@", change); 
     if (_player.currentItem) { 
      [self play]; //<---- doesn't get called 
     } 
    } else { 
     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 
    } 
} 

आईओएस 4.3.3 (और iOS 5) पर मेरी कुंजी अवलोकन विधि कहा जाता है लेकिन _player.currentItem हमेशा nil है। 4.2.1 और 4.3.2 पर इस संपत्ति में वास्तविक मूल्य है। इस विधि को फिर कभी नहीं बुलाया जाता है। तो संक्षेप में, प्रतिस्थापन हमेशा असफल लगता है।

यह एक बग की तरह लगता है, लेकिन शायद मैं कुछ गलत कर रहा हूं।

+0

Dunja से नीचे इस सवाल का जवाब आपकी समस्या का समाधान हुआ? –

उत्तर

17

मुझे आईओएस 5 (5.0.1 समेत) के साथ यह समस्या थी। यह आईओएस 4.x पर ठीक काम करता था।

इसे पढ़ने के दो तरीके हैं, वांछित AVPlayer वांछित AVPlayerItem एस के साथ प्रत्येक बार आपको ट्रैक को स्वैप करने की आवश्यकता होती है। या, मुख्य धागे पर बस replaceCurrentItemWithPlayerItem: पर कॉल करें।

मैंने दोनों विकल्पों की कोशिश की और उन्होंने ठीक काम किया।

क्रेडिट करने के लिए: Apple Developer Forums

+0

मुख्य धागे पर चलने से मेरे लिए बग तय किया गया। – vaughan

1

मुझे इसी तरह की समस्याएं आ रही हैं। आप शायद AVPlayerDemoPlaybackViewController from Apple sample code से मेरे जैसे शुरू कर चुके हैं। शायद समस्या currentItemnil है क्योंकि यह अभी तक लोड नहीं है या प्लेबैक के लिए तैयार है (मेरी समस्या यह थी कि मुझे नए AVPlayerItem की अवधि नहीं मिल सका)।

currentItemReadyToPlay की स्थिति को देखते हुए आप प्लेबैक शुरू करने का प्रयास कर सकते हैं।

AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; 
    switch (status) { 
     case AVPlayerStatusUnknown: { 
      NSLog(@"PLAYER StatusUnknown"); 
     } 
      break; 
     case AVPlayerStatusReadyToPlay: { 
      NSLog(@"PLAYER ReadyToPlay"); 
      [self play]; 
     } 
      break; 
     case AVPlayerStatusFailed: { 
      AVPlayerItem *playerItem = (AVPlayerItem *)object; 
      [self handleError: [playerItem.error localizedDescription]]; 
     } 
      break; 
    } 

यदि यह आपके लिए wok होगा मैं नहीं जानता, मैं कम या 4.3.4 आईपैड की तुलना में अधिक पर इस कोशिश नहीं की है, इसलिए मुझे लगता है मैं जटिलताओं में जल्द ही चलाएंगे।

संबंधित मुद्दे