AVPlayer无法正常工作

时间:2017-02-08 02:07:24

标签: ios objective-c avplayer

当我滑动页面返回时,AVPlayer无法正常工作

-------------------------------------------

#import "TestAVPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "TestAVPlayer-Swift.h"

@interface TestAVPlayerViewController ()

@property (strong, nonatomic) AVPlayer *player; 
@property (strong, nonatomic) AVPlayerItem *playerItem;
@property (strong, nonatomic) AVPlayerLayer * playerLayer;

@end

@implementation TestAVPlayerViewController

- (void)viewDidLoad {
   [super viewDidLoad];

    [self initUI];

    [self isEnableFullScreenReturn];
}

- (void)initUI{

   UIScrollView *scrollView = [[UIScrollView alloc]  initWithFrame:self.view.bounds];
   scrollView.contentSize = CGSizeMake(self.width, self.height * 2);
   scrollView.backgroundColor = [UIColor orangeColor];
   [self.view addSubview:scrollView];

   NSString *playURL = @"http://v.jxvdy.com/sendfile/w5bgP3A8JgiQQo5l0hvoNGE2H16WbN09X-ONHPq3P3C1BISgf7C-qVs6_c8oaw3zKScO78I--b0BGFBRxlpw13sf2e54QA";

   AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:playURL] options:nil];

_playerItem = [AVPlayerItem playerItemWithAsset:asset];
_player = [[AVPlayer alloc]initWithPlayerItem:self.playerItem];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = CGRectMake(0, 150, 300, 300);
_playerLayer.backgroundColor = [UIColor blackColor].CGColor;

[scrollView.layer insertSublayer:_playerLayer atIndex:1];

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [_player play];
  });

}
- (CGFloat)width{
   return self.view.bounds.size.width;
}
- (CGFloat)height{
   return self.view.bounds.size.height;
}

------------------------------------------ 我如何解决这个问题,希望它在回来的路上运行良好。

1 个答案:

答案 0 :(得分:1)

您只在viewDidLoad上播放视频,因此当用户进入&#34;返回&#34;在你看来,视频不会开始播放。

尝试将 [_ player play] 调用放入 viewDidAppear ,例如:

body

如果你这样做,请记得在initUI方法中删除你的游戏,因为你不需要告诉玩家玩两次。