ios - AVPlayer 多次播放视频(10-15)得到带有十字线的播放图标

标签 ios objective-c avplayer avplayerviewcontroller

我正在开发基于电子书阅读器的应用程序,其中 PDF 书由多个 PDF 页面组成。每个页面都有视频图标来播放视频 Assets 。我用了AVPlayer使用 AVPlayerItem 播放视频和 AVURLAsset (来自 URL 的流)。它可以正常播放来自同一页面或不同页面的相同或不同视频 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];

});

最佳答案

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"

关于ios - AVPlayer 多次播放视频(10-15)得到带有十字线的播放图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150567/

相关文章:

架构 arm64 的 IOS SDK/重复符号

ios - 如何解析 iOS 中的 SOAP 响应( Objective-C )?

ios - AVPlayer 和 AVAudioPlayer 不播放 URL 中的音频

ios - 以编程方式设置约束

ios - 专门使用这行代码时无法解包值

ios - 在 UITableViewCell 中添加 AVPlayer 或 MPMoviePlayerController 的最佳位置是什么?

c# - UIAlertController 自定义字体在 iOS 上不起作用

android - 如何为 iOS 和 Android 开发应用内更新?

objective-c - 如何处理成千上万的小音频文件?

Objective-C:从 alloc/init 和类方法调用创建对象有什么区别?