简体   繁体   中英

Avplayer not playing video in ios 8.0

I have implemented avplayer programatically.

@interface showAskUsVideo ()
{
    AVURLAsset *asset;
    AVPlayerItem *item;
}   
@property (strong, nonatomic) AVPlayer *player;

-(void)viewWillAppear:(BOOL)animated
{
    [self playVideo];
}

-(void)playVideo {
         [playerViewController.view setFrame:CGRectMake(37, 80 ,495,220)];
         asset = [AVURLAsset assetWithURL: url];
         item = [AVPlayerItem playerItemWithAsset: asset];
         _player = [[AVPlayer alloc] initWithPlayerItem: item];
         playerViewController.showsPlaybackControls = NO;
        playerViewController.player = _player;

        [item addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];

       [_player addObserver:self forKeyPath:@"rate" options:0 context:nil];

       [self.view addSubview:playerViewController.view];
}

But when I select deployment target 10.0 then its working fine. But for iOS 8.0 to 9.3 only PlaybackControls is seen on screen not video.

What need to change in my code?

Is playerViewController part of the view controller hierarchy? If not, that could explain it. Try adding it as a child view controller of your view controller:

[self addChildViewController:playerViewController];
[self.view addSubview:playerViewController.view];

Try like that: Add to .m

@property (nonatomic, retain)AVPlayer *player;

@property (weak, nonatomic) IBOutlet AVPlayerClass *playerView;

(playerView is your view where you want to show it)

NSURL *url = [[NSBundle mainBundle]URLForResource:@"video_name" withExtension:@"mp4"];
_player = [AVPlayer playerWithURL:url];


playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = _playerView.bounds;

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[_playerView.layer insertSublayer:playerLayer atIndex:0];

_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[_player currentItem]];
[_player play];



-(void)playerItemDidReachEnd:(NSNotification *)notification{

AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];

}

If you using a ViewController for your player. The best practice is presentingViewController

[self presentViewController:playerViewController animated:YES completion:nil];

if you using xib

PlayerViewController *vc = [[PlayerViewController alloc]
                          initWithNibName:@"PlayerViewController" bundle:nil];
[self.view addSubview:[vc view]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM