2016-10-20 4 views
1

私は電子ブックリーダーベースのアプリを開発しています。PDFブックは複数のPDFページで構成されています。各ページには、ビデオアセットを再生するためのビデオアイコンがあります。私はAVPlayerItemAVURLAsset(ストリームfrom URL)を使ってビデオを再生するのにAVPlayerを使いました。同じページまたは別のページから同じまたは異なるビデオを再生することの10-15回のためのその正常に動作します。しかし、しばらくしてからビデオが再生されず、代わりにクロスラインの再生アイコンが表示されます。AVPlayerビデオを複数回再生する(10-15)十字線で再生アイコンを取得

私は以下のコードを使用しました。

NSURL *videoUrl = [NSURL URLWithString:@"http://WWW.google.com/media/test_1.mp4"] 
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; 

NSDictionary *optionsDic = @{AVURLAssetHTTPCookiesKey : cookies}; 

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:optionsDic]; 

AVPlayerItem *playeritem = [AVPlayerItem playerItemWithAsset:asset]; 

AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playeritem]; 

AVPlayerViewController *moviePlayerVC = [[AVPlayerViewController alloc]init]; 

moviePlayerVC.player = player; 


dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 

    [self presentViewController:moviePlayerVC animated:NO completion:nil]; 

}); 
+0

は、なぜあなたは 'dispatch_after'をやっていますか? –

+0

ビデオプレーヤーのプレゼンテーションに時間がかかる場合があります。私は同じ問題に直面しています。 –

+0

viewDidLoadという形式で呼びますか? –

答えて

2
Finally from the Apple AVPlayerDemo sample code I have found the code changes required. And replaced this code with the existing and its worked for me. 


@interface VideoViewController : UIViewController { 

    AVPlayerViewController *playerVC; 
    AVPlayer* mPlayer; 
    AVPlayerItem * mPlayerItem; 
} 
@property (strong) AVPlayerItem* mPlayerItem; 
@property (readwrite, strong, setter=setPlayer:, getter=player) AVPlayer* mPlayer; 
@end 

@implementation VideoViewController 
@synthesize mPlayer,mPlayerItem; 


-(void)viewDidLoad{ 
    playerVC = [[AVPlayerViewController alloc]init]; 
} 


- (void)presentVideoViewController:(NSURL*)linkUrl{ 


    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; 

    NSDictionary *optionsDic = @{AVURLAssetHTTPCookiesKey : cookies}; 

    AVURLAsset * asset = [AVURLAsset URLAssetWithURL:linkUrl options:optionsDic]; 

    NSArray *requestedKeys = @[@"playable"]; 

    [asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: 
    ^{ 
     dispatch_async(dispatch_get_main_queue(), 
         ^{ 

          for (NSString *thisKey in requestedKeys) 
          { 
           NSError *error = nil; 
           AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error]; 
           if (keyStatus == AVKeyValueStatusFailed) 
           { 
            //Show failed error message without presenting AVPlayerViewController// 
            return; 
           } 

          } 


          if (!asset.playable) 
          { 
           //Show failed error message without presenting AVPlayerViewController// 
           return; 
          } 
          if (self.mPlayerItem) 
          { 

           [self.mPlayerItem removeObserver:self forKeyPath:@"status"]; 

          } 
          [self.mPlayerItem addObserver:self 
               forKeyPath:@"status" 
                options:0 
                context:nil]; 


          if (!self.mPlayer) 
          { 

           [self setPlayer:[AVPlayer playerWithPlayerItem:self.mPlayerItem]]; 

          } 

          if (self.player.currentItem != self.mPlayerItem) 
          { 
           [self.mPlayer replaceCurrentItemWithPlayerItem:self.mPlayerItem]; 

          } 


          playerVC.player = self.mPlayer; 
          [self presentViewController:playerVC animated:NO completion:nil]; 
         }); 
    }]; 


} 

- (void)dealloc{ 

if (mPlayer) { 
     [mPlayer.currentItem removeObserver:self forKeyPath:@"status"]; 
     [self.mPlayer pause]; 

     self.mPlayer = nil; 
     self.mPlayerItem = nil; 
     playerVC = nil; 
    } 


} 

問題関連参照リンク:

AVPlayerItem fails with AVStatusFailed and error code "Cannot Decode" Error Domain=AVFoundationErrorDomain Code=-11821 "Cannot Decode"

関連する問題