SnowStorm-L
SnowStorm-L

Reputation: 165

AVPlayer Not working properly

when i Slide the page to return , AVPlayer Not working properly

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

#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;
}

------------------------------------------ How do I fix this problem , I hope it works well on its way back 。

Upvotes: 0

Views: 832

Answers (1)

Shackleford
Shackleford

Reputation: 620

You're only playing the video on viewDidLoad so when the view appears after the user goes "back" to your view the video won't start playing.

Try putting your [_player play] call into viewDidAppear, for instance:

- (void)viewWillAppear:(BOOL)animated
{
    [self.player play];
}

If you do this, remember to remove your play call in the initUI method because you don't need to tell the player to play twice.

Upvotes: 1

Related Questions